Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By DarrylBurke

Thread: How to get the size of an array from textfile?

  1. #1
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default How to get the size of an array from textfile?

    Hello! Suppose I have the textfile which has lines:

    A B 2 midterm 100
    C D 1 final 90
    C D 1 midterm 90
    A B 2 final 80
    **NO ARRAYLIST IS ALLOWED!** And the textfile is passed into the method. How to get the size for the array non-randomly inside the method from the passed Scanner file?? What if you have lots of numbers of lines, so how could that be done? I have doubts about this line Exam[] object = new Exam[12];
    Java Code:
    public static Exam[] readAllExams(Scanner s) throws ArrayIndexOutOfBoundsException
    	{
    		
    		String firstName = "";
    		String lastName = "";
    		int ID = 0;
    		String examType = "";
    		char examTypeCasted;
    		int score = 0;
    		
    		int index = 0;
    		
    		Exam[] object = new Exam[12];
    		
    		while(s.hasNext())
    		{
    			//Returns firtsName and lastName 
    			firstName = s.next();
    			lastName = s.next();
    			
    			//Returns ID number
    			if(s.hasNextInt())
    			{
    				ID = s.nextInt();
    			}
    			else 
    				s.next();
    			
    			//Returns examType which is 'M' or 'F'
    			examType = s.next();
    			examTypeCasted = examType.charAt(0);
    		
    			if(s.hasNextInt())
    			{
    				score = s.nextInt();
    			}
    			//Exam[] object = new Exam[s.nextInt()];
    		 
    			object[index] = new Exam(firstName, lastName, ID, examTypeCasted, score);
    			//System.out.println();
    			index++;
    		}
    		readExam(s);
    		return object;
    		
    		
    	}
    	
    	public static Exam readExam(Scanner s)
    	{
    		String firstName = "";
    		String lastName = "";
    		int ID = 0;
    		String examType = "";
    		char examTypeCasted = 0;
    		int score = 0;
    		
    		while (s.hasNext())
    		{
    			//Returns firtsName and lastName 
    			firstName = s.next();
    			lastName = s.next();
    			
    			//Returns ID number
    			if(s.hasNextInt())
    			{
    				ID = s.nextInt();
    			}
    			//Returns examType which is 'M' or 'F'
    			examType = s.next();
    			examTypeCasted = examType.charAt(0);
    		
    			if(s.hasNextInt())
    			{
    				score = s.nextInt();
    			}
    			
    		}
    		Exam temp = new Exam(firstName, lastName, ID, examTypeCasted, score);
    		return temp;
    	}

  2. #2
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Re: How to get the size of an array from textfile?

    Nevermind, folks! I figure out !

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: How to get the size of an array from textfile?

    If the data from the file MUST go in an array and you don't know the number of objects the records in the file will create, you have some choices:
    1) estimate the number of objects from the file's size divided by the average record size and create an array that size. If that array fills, then create another larger array, copy the objects from the smaller to the larger and continue reading and filling.
    2) make two passes over the file, the first one counting the number of objects that will be created and the second to read the data and fill the array.
    ssk2659 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    27

    Default Re: How to get the size of an array from textfile?

    3) (if the file isn't huge) Use RandomAccessFile, readFully, construct a String from the byte array, replaceAll("[^\\n]", ""). The length of the returned String is the number of lines in the file, provided the file content ends with a blank line.

    The same String can be scanned to extract the lines; no need to read the file again.

    db
    Last edited by DarrylBurke; 02-15-2015 at 04:49 AM.
    ssk2659 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Array Size Error
    By pondwire in forum New To Java
    Replies: 5
    Last Post: 07-21-2011, 09:07 AM
  2. size of array
    By swathi dharmaraj in forum New To Java
    Replies: 8
    Last Post: 04-22-2011, 01:35 AM
  3. Store textfile data in an array
    By mokonji in forum New To Java
    Replies: 4
    Last Post: 02-22-2009, 06:28 PM
  4. Array Assign Values from a Textfile
    By fawadafr in forum Java Applets
    Replies: 6
    Last Post: 11-30-2008, 01:10 AM
  5. Array size declaration
    By JT4NK3D in forum New To Java
    Replies: 3
    Last Post: 01-18-2008, 11:37 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
  •