Results 1 to 8 of 8
  1. #1
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Exclamation 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
    Java 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());
    		}
    		
    			
    	}	
    		
    }
    this is the line it doesnt like
    Java Code:
    boys[k] = new Boy(parts[0],Integer.parseInt(parts[1]));
    any help would be much appreciated and i like to
    thank every one in advance who does help
    :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Based on this code here:
    Java Code:
    Boy[] boys = new Boy[0];
    Question: How many Boy objects will this array be allowed to hold?

  3. #3
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Default

    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:

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Your array cannot hold any object at all.

    Java Code:
    Boy[] boys = new Boy[0];
    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?

  5. #5
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Default

    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
    Java Code:
     Boy [] boys;// = new Boy[0];
    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 post
    i did a google search on it and it say i havent
    given all the variable a default value of null or something!
    :confused:

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Default

    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
    Java 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
    and here the revised version of run methods code
    Java Code:
    boys = BabyUtility.add(boys,new Boy(parts[0],Integer.parseInt(parts[1])));
    thanks for the help everyone really appreciate it.
    D

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. reading from input file and then write on it
    By sara12345 in forum New To Java
    Replies: 9
    Last Post: 01-19-2010, 11:41 AM
  2. Error Message when reading an input file.
    By Deluyxe in forum New To Java
    Replies: 8
    Last Post: 04-26-2009, 04:02 PM
  3. Reading data from csv file based on specific input
    By jaiminparikh in forum Advanced Java
    Replies: 14
    Last Post: 02-13-2009, 09:07 PM
  4. Problem in reading HTML input field while uploading file
    By sudipanand in forum Java Servlet
    Replies: 1
    Last Post: 11-27-2008, 09:26 AM
  5. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-18-2008, 11:51 PM

Posting Permissions

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