This isn't a what is wrong with my code thread. It is a thread about why my program does what it does.
package example1; import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner keyboard = new Scanner (System.in); float x; int y; char ch1, ch2; String name; String input; System.out.print("Enter a character: "); input = keyboard.next(); ch1 = input.charAt(0); System.out.print("Enter a number: "); y = keyboard.nextInt(); System.out.print("Enter another character: "); input = keyboard.next(); ch2 = input.charAt(0); System.out.print("Enter a name: "); name = keyboard.next(); System.out.print("Enter a floating point value: "); x = keyboard.nextFloat(); System.out.println("\nch1 = " + ch1); System.out.println("y = " + y); System.out.println("ch2 = " + ch2); System.out.println("Name is " + name); System.out.println("x = " + x); System.exit(0); } }
There are two parts to this I am just not sure why it does what it does when certain things are input.
Question 1: Enter x 1234 y john 10.50 (Note: enter all the characters on a single
press the enter key line and press the enter key). Why did all the prompts after the first
one run together and not stop for you to enter data?
Output for Question 1:
Enter a number: Enter another character: Enter a name: Enter a floating point value: ch1 = x y = 1234 ch2 = y Name is john x = 10.5
Question 2: Enter1 89 j 110.50
press the enter key
What is the computer waiting for?
Enter a 1, then explain why the output is the way it is.
Output for Question 2:
Enter a number: Enter another character: Enter a name: Enter a floating point value: 1 (I entered the 1 to get what's below) ch1 = 1 y = 89 ch2 = j Name is 110.50 x = 1.0