Results 1 to 2 of 2
  1. #1
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Rep Power

    Default 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:

    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:
    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
    any clues would be greatly appreciated as always!

    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 fillArray
    Last edited by hiei_yasha; 02-17-2011 at 03:42 AM.

  2. #2
    mr_guy is offline Member
    Join Date
    Feb 2011
    Rep Power


    use one scanner to draw each separate line into a string, and then create a scanner for each string to sort each line into separate arrays

Similar Threads

  1. Filling Arrays??
    By Bgreen7887 in forum New To Java
    Replies: 8
    Last Post: 11-04-2010, 07:51 AM
  2. Replies: 8
    Last Post: 05-12-2010, 08:19 PM
  3. retain value of input type file in a jsp file while being dynamically generated
    By nidhi c in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2009, 03:21 AM
  4. Input data from file to arrays problem
    By PVL268 in forum New To Java
    Replies: 18
    Last Post: 03-16-2009, 06:05 AM
  5. Making arrays by reading user input
    By apfroggy0408 in forum New To Java
    Replies: 23
    Last Post: 04-30-2008, 02:23 AM

Posting Permissions

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