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:
Code Java:
//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
Code Java:
/* 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
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
Re: Reading a File, using Scanner, then assigning all values in the file to an array
The reason is:
Code :
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.
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 :D