March 9th, 2013, 05:52 AM
Whats Wrong With My Code???
public class Main
public static void main(String args)
Scanner S=new Scanner(System.in);
System.out.println("Enter the first integer ");
System.out.println("Enter the second integer ");
System.out.println("Enter the first character ");
System.out.println("Enter the second character ");
System.out.println("Enter the first string ");
System.out.println("Enter the second string");
OverloadCompare object=new OverloadCompare();
When I execute the program, it asks for first, second integers and characters, but the first string asking is not asked, its just printed but I don't get to input it, only the second string can be inputed. Whats wrong ??
March 9th, 2013, 07:48 AM
You are having a problem with the Scanner class. When nextInt() is called and the Enter key is pressed, a newline character is left in the Scanner's buffer. The next call to nextLine() returns that newline character. To see print out what is read by the call to nextLine() following a call to nextInt().
A fix is to call the nextLine() method after calling nextInt() to clear the newline character from the Scanner's buffer
March 14th, 2013, 09:46 PM
Originally Posted by NormR
thank you so much d00d. cheers