Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Reading a File, using Scanner, then assigning all values in the file to an array

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Reading a File, using Scanner, then assigning all values in the file to an array

    Hi all,

    Let me start off by saying this is my first post, and it comes in a time of dire need (not really), as this code is driving me to madness, slowly but surely.

    I'm taking a computer science course in school, and there is an assignment due. Normally, I have no problems with these assignments, seeing as little restrictions are put on them. However, this one is different, in the fact that I MUST take a file containing 1)on the first line, the number of number(years) in the file, and 2)followed by a list of birth years. Following that, I must gather all the years and arrange them based on whichever Chinese zodiac sign they are (chicken, rooster, ox, etc). I am not looking for answers, nor a free ride, but rather help.

    The main portion of the code I am having trouble with is:

    //reads a file and stores it in an array
    public static int[] readFile(File aFile)
        {
            int[] yearsInFile = new int[1];
            int arrayLength = 0;
            int j = 0;
            try
            {
                Scanner fileReader = new Scanner(aFile);
                while (fileReader.hasNextLine())
                {
                    arrayLength++;
                }  
                yearsInFile = new int[arrayLength];
                fileReader = new Scanner(aFile);
                while(fileReader.hasNextLine())
                {
                    yearsInFile[j] = Integer.parseInt(fileReader.next());
                    j++;
                }    
     
            } 
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
            return yearsInFile; 
        }//close readFile

    I think that it is entering an infinite loop, how I do not know. I originally encountered issues when trying to initialize yearsInFile to the correct size, seeing as I cannot initialize to null, without a null pointer exception. We are not allowed to use ArrayList (because they havent taught us yet, even though I've used it before, evidence of the restrictions I was talking about before).

    Not sure if you need it, but here is the rest of my code, just to be safe

    /* ClassName
     * course name
     * Instructor:
     * @authors:
     * @version 2011-Nov-24
     * 
     * Purpose: gets a file filled with births years and prints a histogram based on 
     *          the distribution of zodiac signs of those years
     */
    import java.io.File;
    import java.io.FileNotFoundException;
    import javax.swing.JFileChooser;
    import java.util.*;
     
    public class ClassName
    {
        //Stores zodiac symbols
        public static String[] getZodiacSigns()
        {
            String[] signs = new String[12];
            signs[0]="Rat";
            signs[1]="Ox";
            signs[2]="Tiger";
            signs[3]="Rabbit";
            signs[4]="Dragon";
            signs[5]="Snake";
            signs[6]="Horse";
            signs[7]="Goat";
            signs[8]="Monkey";
            signs[9]="Rooster";
            signs[10]="Dog";
            signs[11]="Pig";
            return signs;
        }//close getZodiacSigns()
     
        //gets the years from a file
        public static int[] getYears(File yearsFile)
        {
            int[] years = readFile(yearsFile);
            return years;
        }//close getYears    
     
        //gets a file
        public static File getFile()
        {
            JFileChooser chooser = new JFileChooser();
            File yearsFile = null;
            int returnValue;
     
            // show the open file dialog
            returnValue = chooser.showOpenDialog(null);
     
            // check to see that the user clicked the OK button
            if (returnValue == JFileChooser.APPROVE_OPTION) 
            {
                // get the selected file
                yearsFile = chooser.getSelectedFile();
            }
     
            return yearsFile;
        }//close getFile
     
        //reads a file and stores it in an array
        public static int[] readFile(File aFile)
        {
            int[] yearsInFile = new int[1];
            int arrayLength = 0;
            int j = 0;
            try
            {
                Scanner fileReader = new Scanner(aFile);
                while (fileReader.hasNextLine())
                {
                    arrayLength++;
                }  
                yearsInFile = new int[arrayLength];
                fileReader = new Scanner(aFile);
                while(fileReader.hasNextLine())
                {
                    yearsInFile[j] = Integer.parseInt(fileReader.next());
                    j++;
                }    
     
            } 
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
            return yearsInFile; 
        }//close readFile
     
        //converts years[] to all be within 0-11, one number for each symbol
        public static int convertYearToIndex(int year)
        {
           int index = -1;
           int newYear = year-1924;
           if (newYear>=12) newYear = newYear%12;
           for (int i=0;i<12;i++)
           {
                if(year>1924)//so it doesn't include the number of years at the beginning of the list
                {
                    if(newYear==i) index = newYear;
                }    
           }
           return index;
        }//close convertYearToIndex   
     
        //fills an array with numbers between 0-11
        public static void populateCounts(int[] counts, int[] years)
        {
            int[] index = new int[years.length];
            for(int i=1;i<years.length;i++)//starts at 1 so it skips the number of years in the list
            {
                index[i] = convertYearToIndex(years[i]);
            }  
     
            for (int i=1;i<index.length;i++)//again starts at 1
            {
                for (int g=0;g<12;g++)//checks for which of the zodiac signs it is
                {
                    if (index[i]==g) counts[g]++;
                }
            }   
        }//close populateCounts
     
        //gets largest name length, for use in printBar
        public static int getMaxNameLength(String[] zodiacNames)
        {
            String longest = "";
            for(int i=0;i<zodiacNames.length;i++)
            {
                if (zodiacNames[i].length()>longest.length()) longest=zodiacNames[i];
            }
            int maxLength = longest.length();
            return maxLength;
        }//close getMaxNameLength    
     
        //prints out a bar of the histogram
        public static void printBar(String label, int percent, char barSymbol, int maxLabelLength)
        {
            String spaces = "";
            String newLabel = "";
            String bar = "";
            if(label.length()<maxLabelLength)
            {
                for (int i=label.length();i<maxLabelLength;i++)//might have to be<=
                {
                    spaces+=" ";
                }
                newLabel+=spaces;
            }
            newLabel+=label;
     
            for(int i=0;i<percent;i++)
            {
               bar+=Character.toString(barSymbol);
            }   
     
            System.out.println(newLabel + ": " + bar + " (" + percent + "%)");
     
        }//close printBar
     
        //prints out the histogram
        public static void printHistogram(int totalYears, int[] counts, String[] zodiacNames)
        {
            System.out.println("For " + totalYears + " birth years, the distribution is: ");
            int percent = 0; 
     
            for (int i=0;i<zodiacNames.length;i++)
            {
                percent = counts[i]/totalYears; 
                printBar(zodiacNames[i], percent, '*', getMaxNameLength(zodiacNames));
            }    
     
        }//close printHistogram   
     
        //Main
        public static void main(String[] args)
        {
            String[] zodiacSigns = getZodiacSigns();
            File yearFile = getFile();
            int[] years = getYears(yearFile);
            int[] counts = new int[12];
            populateCounts(counts, years);
            printHistogram(years.length, counts, zodiacSigns);
     
        }//close main            
    }//close program

    I know, its really messy, I'm sure that there are 101 things wrong with this, but I tried to keep it as clean as possible.

    The files I need to be able to read and store look like this:

    5 <number of years in list>
    1995
    1924
    2008
    1952
    1977

    Thanks,
    er1111


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    china
    Posts
    12
    My Mood
    Sad
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Reading a File, using Scanner, then assigning all values in the file to an array

    first, you use fileReader.hasNextLine() to determine whether there are data
    but fileReader.hasNextLine() always true;

    second, in first while() you have not through fileReader.next to extract data result to fileReader.hasNextLine() or fileReader.hasNext() always true

  3. The Following User Says Thank You to jcloud For This Useful Post:

    er1111 (December 2nd, 2011)

  4. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Reading a File, using Scanner, then assigning all values in the file to an array

    The reason is:
    while (fileReader.hasNextLine())
                {
                    arrayLength++;
                }
    Your code is not iterating the fileReader object, so let's say if it has value 1 initially, it will stay on 1 and this causes the infinite loop. Try to iterate it.
    fileReader.next()

  5. The Following User Says Thank You to Mr.777 For This Useful Post:

    er1111 (December 2nd, 2011)

  6. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Reading a File, using Scanner, then assigning all values in the file to an array

    Wow, thanks very much guys! Was pulling my hair out last night trying to figure it out. Saved me another day of frustration

Similar Threads

  1. [SOLVED] Reading file and adding it to an array through a method
    By PeskyToaster in forum Collections and Generics
    Replies: 20
    Last Post: November 10th, 2011, 01:16 AM
  2. Reading from a sequential file to an array
    By Xrrak in forum File I/O & Other I/O Streams
    Replies: 15
    Last Post: August 20th, 2011, 01:33 PM
  3. Help with Scanner class - Reading Data from a file
    By billias in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 28th, 2011, 12:07 PM
  4. [SOLVED] Assigning Values to Multi Array
    By dredjohn in forum Collections and Generics
    Replies: 3
    Last Post: April 18th, 2011, 07:43 PM
  5. Reading a file, then putting it in an array
    By Gondee in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 16th, 2010, 11:11 AM