Results 1 to 2 of 2
- 02-17-2011, 02:20 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 34
- Rep Power
- 0
Filling arrays from an input file?
Hi, on my previous homework we were to read a given file and fill an array with the entire contents, which was fine.
However now we need to create separate arrays from each line in the text file. For example, my input file, teams.txt is:
4
Mariners RedSox Yankees Cubs
Lakers Knicks Bulls Cavaliers
Cowboys Falcons Vikings Packers
4 corresponds to the number of teams
Line one consists of baseball teams
Line two of basketball teams
Line three of football teams
How can I create baseballArray of the baseball teams, basketArray of basketball teams, and so forth?
In the previous homework we used this code to fill the entire array, working with students and ID#s:
any clues would be greatly appreciated as always!Java Code:public static void fillArray(Student [] myClass, Scanner fin) { for(int i = 0; i < myClass.length; i++) { String n = fin.nextLine(); int id = Integer.parseInt(fin.nextLine()); Student temp = new Student (n, id); myClass[i] = temp; } }// end fillArray
this is what I have for the time being, emulating what we did in the previous assignment, but gives me a no line found error:
Java Code:public static void fillArray(String [] myArray, Scanner fin) { for(int i = 0; i < myArray.length; i++) { int num = Integer.parseInt(fin.nextLine()); String bb = fin.nextLine(); String basetemp = new String (bb); myArray[i] = basetemp; String bkb = fin.nextLine(); String baskettemp = new String (bkb); myArray[i] = baskettemp; String f = fin.nextLine(); String foottemp = new String (f); myArray[i] = foottemp; } }// end fillArrayLast edited by hiei_yasha; 02-17-2011 at 02:42 AM.
- 02-26-2011, 05:01 AM #2
Member
- Join Date
- Feb 2011
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Filling Arrays??
By Bgreen7887 in forum New To JavaReplies: 8Last Post: 11-04-2010, 06:51 AM -
how to change the layout of an input file and write to an output file
By renu in forum New To JavaReplies: 8Last Post: 05-12-2010, 07:19 PM -
retain value of input type file in a jsp file while being dynamically generated
By nidhi c in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-27-2009, 02:21 AM -
Input data from file to arrays problem
By PVL268 in forum New To JavaReplies: 18Last Post: 03-16-2009, 05:05 AM -
Making arrays by reading user input
By apfroggy0408 in forum New To JavaReplies: 23Last Post: 04-30-2008, 01:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks