Results 1 to 7 of 7
  1. #1
    j_rod722 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default passing a .txt into an array

    i am having issues. i don't know what to do or what is wrong. any advice?

    Java Code:
    package people;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    /**
     *
     * @author Jarrod Dowell
     */
    public class Main {
    
        final static int LIMIT = 20;
    
        public static void main(String[] args) throws FileNotFoundException{
    
            final String FILENAME = "applicants.txt";
            File inputFile = new File(FILENAME);
            
            
            if(inputFile.exists()){
                System.out.println("By Jove! The file " + FILENAME + " does exists!");
            } else{
                System.out.println("Bloody Hell! The file " + FILENAME + " is missing!");
                System.exit(1);
            }
    
            Person[] applicants = new Person[LIMIT];
    
            Scanner fileScan= new Scanner(inputFile);
    
            int count = 0;
            int age;
    
            for(int i = 0; i < LIMIT; i++){
                while(fileScan.hasNext()){
                    age = fileScan.nextInt();
                    count++;
                }
            }
            fileScan.close();
    
            fileScan= new Scanner(inputFile);
            int ages[] = new int[count];
    
            for(int j = 0; j < LIMIT; j++){
                if(fileScan.hasNext()){
                   String[] input = Scanner.nextLine().split("");
                   applicants[j] = new Person(input[0], Integer.parseInt(input[1]));
    
                }
            }
            System.out.println("Starting...");
            printListString(applicants, LIMIT);
            System.out.println(getAVG(applicants, LIMIT));
            System.out.println(getMedian(applicants, LIMIT));
            System.out.println("...Finished");
    
        }
       
        private static double getAVG(Person[] ages, int n){
            double sum = 0;
            double avg;
            
            for(int i = 0; i < ages.length; i++){
                sum = sum + ages[i];
            }
            avg = sum/n;
    
            return avg;
        }
        private static double getMedian(Person[] ages, int n){
            double middle;
            n = 0;
    
            middle = n/2;
    
            return middle;
        }
        private static void printListString(Person[] list, int n){
            for(int i = 0; i < n; i++){
                System.out.println("["+i+"] "+  list[i]);
            }
        }
        private static void printList(Person[] list, int n){
            for(int i = 0 ; i < n; i++){
                System.out.println(list[i]);
            }
        }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    You don't tell us what you want the code to do, what you expect it to do, or what it does. You don't tell us whether you're seeing unexpected behavior, exceptions, or something else.

    Read the link in my signature on asking smart questions, then ask a specific question, and we'll go from there.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    j_rod722 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    ok. i am writing a program to read strings(name and age) into an array of objects. i need the program to read in the info (this is where i am stuck) then sort, average the ages, and find the median age. i cannot figure out how to do this and i am only frustrating myself by not understanding it. please advise.

  4. #4
    codesmuggler is offline Member
    Join Date
    Feb 2011
    Posts
    30
    Rep Power
    0

    Default

    What is the format of text file?
    What is wrong when you reading it?
    Maybe if you are not debugging your code with debugger, you can put more sysouts and find out where the problem is.

  5. #5
    j_rod722 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    the format of data on the .txt is string name and int age.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Are you using delimiters? spaces? new lines? can you give us an example of the text file?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    for(int i = 0; i < LIMIT; i++){
        while(fileScan.hasNext()){
            age = fileScan.nextInt();
            count++;
        }
    }
    there are at least 3 problems with this code.

    1. The outer for loop iterates 20 times, i = 0 to 19. Inner while loop iterates until input is exhausted. So after one iteration of the outer for loop, the inner while loop exhausts input. then the outer for loop iterates for the second time but since the input is exhausted the inner while loop does not and it goes back to the outer for loop which iterates for the third time and the inner while loop doesn't because input is still exhausted, etc etc etc.

    2. You mentioned that the format of the text file is String name int age but all you code does is read an int. So when it reads the name and it is not an int it throws an exception.

    3. If all above is fixed and the code does actually read the age you do nothing with it. Read the first int and store in the age variable. Read the next int, throw away the first int and replace it with the second int. Read the next int, throw away the second int and replace it with the third int, etc etc etc

Similar Threads

  1. Passing Data From One Array To Another
    By Sly Cooper in forum New To Java
    Replies: 17
    Last Post: 01-24-2011, 06:07 AM
  2. Passing an array to a method.
    By twcast in forum New To Java
    Replies: 9
    Last Post: 02-10-2010, 09:13 AM
  3. Passing a 2D array
    By toymachiner62 in forum New To Java
    Replies: 6
    Last Post: 10-23-2009, 04:05 PM
  4. passing an array between classes
    By gisler in forum New To Java
    Replies: 10
    Last Post: 04-10-2009, 10:31 PM
  5. array passing dynamically
    By jazz2k8 in forum Advanced Java
    Replies: 2
    Last Post: 10-16-2008, 10:29 PM

Posting Permissions

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