Results 1 to 2 of 2
Thread: Problem with Scanner
- 12-01-2011, 07:08 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
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:
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.Enter your email : Enter your Program (BSCE, BSTE, BSCS) :
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(); } }
- 12-01-2011, 07:22 PM #2
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.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
-
Problem with the Scanner
By Maretaga in forum New To JavaReplies: 6Last Post: 07-14-2011, 09:14 AM -
Scanner problem.
By keo in forum New To JavaReplies: 6Last Post: 04-07-2011, 08:30 AM -
Scanner problem
By mkay in forum New To JavaReplies: 2Last Post: 10-28-2010, 12:09 AM -
Problem with scanner
By JavaJ in forum New To JavaReplies: 6Last Post: 03-16-2010, 08:51 PM -
Problem with scanner
By Kangaroo128 in forum New To JavaReplies: 11Last Post: 09-01-2009, 08:07 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks