Results 1 to 8 of 8
- 01-23-2010, 06:02 AM #1
Need help wit reading txt file into input.
im trying to read a txt file into an array so i can process
in the txt file it has a name followed by a space then a number
eg ben 21364
every thing complies it when i run the code it throw in arrayOUtOFBOuNDS
exceptions
here is the code
this is the line it doesnt likeJava Code:public class RunMethods { //attributes Boy [] boys = new Boy[0]; private Girl [] girls; Scanner keyboard = new Scanner(System.in); //constructor public RunMethods() { } //get set methods public void fileReadIn() { int k = 0; try { Scanner input = new Scanner(new FileInputStream("boynames.txt")); while(input.hasNext()) { String line = input.nextLine(); String [] parts = line.split(" "); boys[k] = new Boy(parts[0],Integer.parseInt(parts[1])); k++; } input.close(); } catch(FileNotFoundException fnfe) { System.out.printf("Problem opening file....\n"); System.exit(0); } for(int i = 0;i < boys.length;i++) { System.out.printf("%s\n",boys[i].toString()); } } }
any help would be much appreciated and i like toJava Code:boys[k] = new Boy(parts[0],Integer.parseInt(parts[1]));
thank every one in advance who does help
:)
-
Based on this code here:
Question: How many Boy objects will this array be allowed to hold?Java Code:Boy[] boys = new Boy[0];
- 01-24-2010, 11:19 AM #3
it has to hold 1000 boy objects but im trying to make the code
so that it could hold more than that (eg i might want to add more names and numbers
to the txt file in the future) so i dont want to give the array a set value.i have a theory that it could be the reading the space in the text file inbetween the name of the boy and the number as a value but i want the code to skip over that space and just
read the name and the number
:confused:
- 01-24-2010, 11:26 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Your array cannot hold any object at all.
Do you know the exact meaning of the above line of code? What's the word new do and what's mean by the Boy[0] do?Java Code:Boy[] boys = new Boy[0];
- 01-25-2010, 12:42 AM #5
yeah i understand what new
does it creates a new object of the boy class
but even if i do comment out the end of that line
like this
it throws a nullPointerException instead of an array out of bounds exception and it still points to that line same line mention in the 1st postJava Code:Boy [] boys;// = new Boy[0];
i did a google search on it and it say i havent
given all the variable a default value of null or something!
:confused:
-
So either use an ArrayList which is like an array with a variable size, or use an array initialized to something other than 0, to a number that is guaranteed to be at least as big or bigger than the number of items it will need to hold.
- 01-25-2010, 02:00 AM #7
yeahhhhhhaaaaaaaaaaa figure it out
needed to use a static method
eg add to increase the array Size
which i made it its own class class BabyUtility
here the code
and here the revised version of run methods codeJava Code://This class contains array utility used in the BoyGirl program. //that are static public class BabyUtility { //methods //------------------- public static Boy [] add(Boy [] origArray,Boy thingToAdd) { Boy [] newBoy = increase(origArray); newBoy[newBoy.length -1] = thingToAdd; return newBoy; } //--------------------- private static Boy [] increase(Boy [] boyToIncrease) { Boy [] newBoy = new Boy[boyToIncrease.length + 1]; for(int i =0;i < boyToIncrease.length;i++) { newBoy[i] = boyToIncrease[i]; } return newBoy; } }//end of class BabyUtility
thanks for the help everyone really appreciate it.Java Code:boys = BabyUtility.add(boys,new Boy(parts[0],Integer.parseInt(parts[1])));
D
- 02-02-2010, 04:40 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
ShinTec, please mark the thread solved if you've already find the solution. Hope you know how to do that...
Similar Threads
-
reading from input file and then write on it
By sara12345 in forum New To JavaReplies: 9Last Post: 01-19-2010, 11:41 AM -
Error Message when reading an input file.
By Deluyxe in forum New To JavaReplies: 8Last Post: 04-26-2009, 04:02 PM -
Reading data from csv file based on specific input
By jaiminparikh in forum Advanced JavaReplies: 14Last Post: 02-13-2009, 09:07 PM -
Problem in reading HTML input field while uploading file
By sudipanand in forum Java ServletReplies: 1Last Post: 11-27-2008, 09:26 AM -
Reading input file into an array
By littlefire in forum New To JavaReplies: 6Last Post: 10-18-2008, 11:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks