Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: Problem with Scanner

  1. #1
    zarak is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Problem with Scanner

    There's a problem occurring in the following code. Basically, it's supposed to ask for your name, age, email id, registered courses, etc. All of it is working fine except for the SetEmail function. You can see that there are currently two inputs in that function; one is being executed and the other is in comments. If there is only one input i.e. with the second one in the comments, it refuses to run that input and jumps straight to the next function and executes System.out.print("Enter your Program (BSCE, BSTE, BSCS) : ");. The output is as such:
    Enter your email : Enter your Program (BSCE, BSTE, BSCS) :
    However, when I add the second input, it works perfectly fine. What I want to ask is that why doesn't it execute the first input? It's executing it the very first time in all the other functions. The problem occurs only in this specific SetEmail function.

    Java Code:
    import java.lang.*;
    import java.util.*;
    
    
    interface Functions
    {
    	
    	void SetName();
    	void SetAge();
    	void SetEmail();
    	void SetProgram();
    	void SetSubjects();
    	void SetMarks();
    	void SetGrade();
    	
    	void GetName();
    	void GetAge();
    	void GetEmail();
    	void GetProgram();
    	void GetSubjects();
    	void GetMarks();
    	void GetGrade();
    	
    	
    }
    
    class SIS implements Functions
    {
    	
    	String name;
    	String email;
    	String program;
    	String grade;
    	String[] subjects = new String[3];
    	
    	double[] marks = new double[3];
    	double average = 0.0;
    	
    	int age;
    	
    	Scanner input = new Scanner(System.in);
    	
    	public void SetName()
    	{
    		
    		System.out.print("Enter your name : ");
    		name = input.nextLine();
    
    	}
    	
    	public void SetEmail()
    	{
    		
    		System.out.print("Enter your email : ");
    		email = input.nextLine();
    		//******************email = input.nextLine();**********************
    				
    	}
    	
    	public void SetProgram()
    	{
    		
    		System.out.print("Enter your Program (BSCE, BSTE, BSCS) : ");
    		program = input.nextLine();	
    				
    	}
    	
    	public void SetSubjects()
    	{
    		
    		for(int i = 0 ; i < subjects.length ; i++)
    		{
    			
    		System.out.print("Enter subject number " + (i+1) + " : ");
    		subjects[i] =input.nextLine();			
    			
    		}
    				
    	}
    	
    	public void SetMarks()
    	{
    		
    		for(int j = 0 ; j < subjects.length ; j++)
    		{
    			
    		System.out.print("Enter marks obtained in " + subjects[j] + " : ");
    		marks[j] = input.nextDouble();
    		
    			while(marks[j] < 0 || marks[j] > 100)
    			{
    				
    				System.out.print("Invalid data; enter again : ");
    				marks[j] = input.nextDouble();
    						
    			}
    					
    		}
    		
    	}
    	
    	public void SetGrade()
    	{
    		
    		
    		for(int k = 0 ; k < subjects.length ; k++)
    		{
    			
    			average = average + marks[k];
    			
    		}
    		
    		average = average / 3;
    		
    		if(average < 50)
    		{
    			
    			grade = "F";
    			
    		}
    		
    		if(average >= 50.0 && average < 60.0)
    		{
    			
    			grade = "E";
    			
    		}
    		
    		if(average >= 60.0 && average < 70.0)
    		{
    			
    			grade = "D";
    			
    		}
    		
    		if(average >= 70.0 && average < 80.0)
    		{
    			
    			grade = "C";
    			
    		}
    		
    		if(average >= 80.0 && average < 90.0)
    		{
    			
    			grade = "B";
    			
    		}
    		
    		if(average >= 90.0 && average <= 100.0)
    		{
    			
    			grade = "A";
    			
    		}
    
    	}
    	
    	public void SetAge()
    	{
    		
    		System.out.print("Enter your age : ");
    		age = input.nextInt();	
    		
    	}
    	
    	
    	
    	
    	public void GetName()
    	{
    		
    		System.out.println("Your name : " + name);
    		
    	}
    	
    	public void GetEmail()
    	{
    		
    		System.out.println("Your email : " + email);
    		
    	}
    	
    	public void GetProgram()
    	{
    		
    		System.out.println("Your program : " + program);
    		
    	}
    	
    	public void GetSubjects()
    	{
    		for(int a = 0 ; a < subjects.length ; a++)
    		{
    			
    			System.out.println("Subject number " + (a+1) + " : " + subjects[a]);
    		
    		}
    	
    	}
    	
    	public void GetMarks()
    	{
    		
    		for(int b = 0 ; b < subjects.length ; b++)
    		{
    			
    			System.out.println("Obtained marks in subject number " + (b+1) + " : " + marks[b]);
    		
    		}
    		
    	}
    	
    	public void GetGrade()
    	{
    		
    		System.out.println("Obtained grade : " + grade);
    		
    	}
    	
    	public void GetAge()
    	{
    		
    		System.out.println("Your age : " + age);
    		
    	}
    	
    }
    
    class Test2
    {
    	
    	public static void main(String [] args)
    	{
    		
    		Functions obj = new SIS();
    		obj.SetName();
    		obj.SetAge();
    		obj.SetEmail();
    		obj.SetProgram();
    		obj.SetSubjects();
    		obj.SetMarks();
    		obj.SetGrade();
    		
    		obj.GetName();
    		obj.GetAge();
    		obj.GetEmail();
    		obj.GetProgram();
    		obj.GetSubjects();
    		obj.GetMarks();
    		obj.GetGrade();
    		
    		
    	}
    	
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Problem with Scanner

    First of all, that's way too much code. You should post an SSCCE that demonstrates the problem instead.

    Secondly, have you taken a look at the Scanner API? Pay special attention to when a new line is eaten up by the Scanner.
    Dark likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Problem with the Scanner
    By Maretaga in forum New To Java
    Replies: 6
    Last Post: 07-14-2011, 09:14 AM
  2. Scanner problem.
    By keo in forum New To Java
    Replies: 6
    Last Post: 04-07-2011, 08:30 AM
  3. Scanner problem
    By mkay in forum New To Java
    Replies: 2
    Last Post: 10-28-2010, 12:09 AM
  4. Problem with scanner
    By JavaJ in forum New To Java
    Replies: 6
    Last Post: 03-16-2010, 08:51 PM
  5. Problem with scanner
    By Kangaroo128 in forum New To Java
    Replies: 11
    Last Post: 09-01-2009, 08:07 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
  •