Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Making an array from an input file

    So I'm partially lost on arrays atm. What I want to do is be able to create and array using a file called file.dat. I then want to use this file and make it an array and be able to implement it into several methods which can calculuate the mean and deviation of the numbers. I would like to know if anyone can help me get started on importing the file with the numbers and also creating a new array. I believe it will be something like this:

    Code:
    import.java.io.*;
    
    public class Array
    {
    
    int [] = new int[];
    file.dat = int[];
    
    }
    Thats all I can think of for right now...please blow my mind lol
    Last edited by dubsjw; 12-02-2010 at 11:35 PM.

  • #2
    Regular Coder cs_student's Avatar
    Join Date
    Oct 2009
    Location
    ~/
    Posts
    195
    Thanks
    2
    Thanked 22 Times in 22 Posts
    That's not quite. Obviously you have some fundemental misunderstandings aboutthe language that you might want to clear up. You may want to look here for a good tutorial on the language. More specifically you can find information about arrays in java here. However, you may want to go over variables (which is a section or two before arrays), as you may not have gotten a concrete understanding of how they work yet.


    As for how you could do the program, you have to understand that you just can't turn a file into an array. What you want to do is read data from a file, then put it into an array. You may want to look into something like ArrayList, as I feel that you may not be sure how many entries are going to be in the file. Arrays have to be a specified size. If you use an ArrayList you will not have to worry about resizing the array.

    So to recap, you want to read data from a file (one entry at a time), then put it into an array). How you read it from the file depends on how the file is formated. Generally you will be reading just ascii text. In which case you can easily use the Scanner class to parese through your data. In order to create a scanner which reads from the file you can do something along the lines of.

    Code:
    try {
        Scanner reader = new Scanner( new File("file.dat") );
    catch(FileNotFoundException e) {
        System.out.println("File was not found");
    }

    See if you can't come up with something after looking over this new information. Then post back if you have any problems.


    cs_student

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So I have this and as of right now I have imported the file and inserted it into the array using scanner class...I go to compile it and but in bluej there is no main class to run...I'm not sure whats up...as of right now I'm trying to print the array out so I can view if it reads the numbers correctly.

    Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class hw11
    {
        public int scores[];
               
        public void main(String[] args)
        {
        
            
                scores = new int[35];
                
            try {
                    Scanner reader = new Scanner( new File("hw11in") );
                    for (int j = 0; j < scores.length; j++)
                    {
                        scores[j] = reader.nextInt();
                    }
                }
            catch(FileNotFoundException e) 
                {
                    System.out.println("File was not found");
                }    
                
                
                System.out.println(scores);
        
        
        }
        
        
    }

  • #4
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When I try to invoke my method calcMean I get the error:

    calcMean(int) in hw11 cannot be applied to()

    anyone know why?

    Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class hw11
    {
        public int scores[] = {96, 78, 76, 65, 91, 42, 89, 83, 77, 83, 93, 54, 61, 84, 79,
             39, 92, 72, 74, 82, 64, 65, 92, 72, 74, 82, 64, 65, 92, 45, 37, 69,
             72, 91, 64};
             
             int mean = 0;
             int standDev = 0;
               
        public void main(String[] args)
        {
        
            
            //scores = new int[35];
            
                
            //try {
                    //Scanner reader = new Scanner( new File("hw11in") );
                    //for (int j = 0; j < scores.length; j++)
                    //{
                      //  scores[j] = reader.nextInt();
                    //}
                //}
            //catch(FileNotFoundException e) 
                //{
                   // System.out.println("File was not found");
                //}    
                
                hw11.calcMean();
                System.out.println(mean);
                
                         
        } 
        
        public int calcMean(int mean)
        {
            int total = 0;
            for (int i = 0; i < scores.length; i++)
            {
                total += scores[i];
            }   
            
            mean = total / scores.length;
           
            return mean;
        } 
        
        public int stanDev(int stanDev)
        {
            for (int i = 0; i < scores.length; i++)
            {
                
            }
            return stanDev;
        }
        
    
    }

  • #5
    Regular Coder cs_student's Avatar
    Join Date
    Oct 2009
    Location
    ~/
    Posts
    195
    Thanks
    2
    Thanked 22 Times in 22 Posts
    The main method which java invokes is static.
    It's signature goes as
    Code:
    public static void main(String[] args);
    In your code you have neglected to make it static, which is what I assum bluej is giving the error about.

    Also, note that you can't access non-static members or methods from static methods.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •