Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Norm

Thread: Why is the method not calling another method?

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

    Default Why is the method not calling another method?

    Hi! Why is the method readAllExams(Scanner s) giving an error on calling another method which is readExam(Scanner s )?

    Java Code:
    public static Exam[] readAllExams(Scanner s) throws ArrayIndexOutOfBoundsException
    	{
    		String firstName = "";
    		String lastName = "";
    		int ID = 0;
    		String examType = "";
    		int score = 0;
    		
    		int index = 0;
    		
    		Exam[] object = new Exam[50];
    		
    		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();
    		
    			if(s.hasNextInt())
    			{
    				score = s.nextInt();
    			}
    		 
    			object[index] = new Exam(firstName, lastName, ID, examType, score);
    			System.out.println();
    			index++;
    		}
    		return object;
    		
    		readExam(Scanner s);
    	}
    	
    	public static Exam readExam(Scanner s)
    	{
    		String firstName = "";
    		String lastName = "";
    		int ID = 0;
    		String examType = "";
    		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();
    			}
    			else 
    				s.next();
    			
    			//Returns examType which is 'M' or 'F'
    			examType = s.next();
    		
    			if(s.hasNextInt())
    			{
    				score = s.nextInt();
    			}
    		}
    		Exam temp = new Exam(firstName, lastName, ID, examType, score);
    		return temp;
    	}
    	
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Why is the method not calling another method?

    I can think of two reasons:

    1. You are not calling the method correctly (don't include the type).
    2. The method invocation will never be reached.

    Edit: Nor do I fully understand your Scanner logic in either method (but I haven't really tried too hard).

    Regards,
    Jim
    Last edited by jim829; 02-10-2015 at 06:46 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Why is the method not calling another method?

    giving an error
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Why is the method not calling another method?

    You can't execute any statements after a return.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

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

    Default Re: Why is the method not calling another method?

    It's giving me this: [LExam;@717da562 when I run a method realAllExams from the main:
    public static void main(String [] args) throws FileNotFoundException
    {
    Scanner data = new Scanner(new File("Exam.txt"));
    Exam[] tempObject = readAllExams(data);
    System.out.println(tempObject);


    }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Why is the method not calling another method?

    Quote Originally Posted by ssk2659 View Post
    It's giving me this: [LExam;@717da562 when I run a method realAllExams from the main:
    public static void main(String [] args) throws FileNotFoundException
    {
    Scanner data = new Scanner(new File("Exam.txt"));
    Exam[] tempObject = readAllExams(data);
    System.out.println(tempObject);


    }
    Your program shouldn't even compile unless you made a mistake pasting it in.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Why is the method not calling another method?

    [LExam;@717da562
    That is the String returned by an array of Exam objects' toString() method.
    If you want to see the contents of the Exam objects in the array:
    1) Add a toString() method to the Exam class that returns a String describing the objects contents
    2) Use the Arrays class's toString() method to format the array for printing. Something like this:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    ssk2659 likes this.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 17
    Last Post: 10-23-2014, 08:17 PM
  2. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  3. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 11:03 AM
  4. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 PM

Tags for this Thread

Posting Permissions

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