I have an assignment where we have to make a Java program that will read 4 inputs from the user:
- Integer
- Double
- Character
- String
Then it outputs them in the above order in one line, and in reverse order in another line.
The inputs are
- Integer: 99
- Double: 3.77
- Character: z
- String: Howdy
However when they are outputted, the String is just blank
Expected results:
Enter integer:
Enter double:
Enter character:
Enter string:
99 3.77 z Howdy
Howdy z 3.77 99
3.77 cast to an integer is 3
Actual results:
Enter integer:
Enter double:
Enter character:
Enter string:
99 3.77 z
z 3.77 99
3.77 cast to an integer is 3
Here is the code that I have
import java.util.Scanner;
public class BasicInput {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int userInt = 0;
double userDouble = 0.0;
// FIXME Define char and string variables similarly
char myChar;
String myString = "";
System.out.println("Enter integer: ");
userInt = scnr.nextInt();
// FIXME (1): Finish reading other items into variables, then output the four values on a single line separated by a space
System.out.println("Enter double: ");
userDouble = scnr.nextDouble();
System.out.println("Enter character: ");
myChar = scnr.next().charAt(0);
System.out.println("Enter string: ");
myString = scnr.nextLine();
System.out.println(userInt + " " + userDouble + " " + myChar + " " + myString);
// FIXME (2): Output the four values in reverse
System.out.println(myString + " " + myChar + " " + userDouble + " " + userInt);
// FIXME (3): Cast the double to an integer, and output that integer
System.out.println(userDouble + " cast to an integer is " + (int)userDouble);
return;
}
}
How do I get the String to show in the output? It is just displaying as an empty string. I've even tried to initialize the myString variable to "Hello", but it still displays empty.