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.
>Solution :
The following should work.
You can request the input on the same line using System.out.print
and you can use String.format
to output the variables. Don’t forget to close the Scanner
!
Note: When grabbing partial string input, you need to call scanner.nextLine()
to dump the buffer, before requesting more string values.
import java.util.Scanner;
public class BasicInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Inputs
int userInt;
double userDouble;
char myChar;
String myString;
System.out.print("Enter integer: ");
userInt = scanner.nextInt();
System.out.print("Enter double: ");
userDouble = scanner.nextDouble();
System.out.print("Enter character: ");
myChar = scanner.next().charAt(0);
scanner.nextLine(); // Clear the buffer
System.out.print("Enter string: ");
myString = scanner.nextLine();
// Output the four values in order
System.out.printf("%d %.2f %s %s%n", userInt, userDouble, myChar, myString);
// Output the four values in reverse order
System.out.printf("%s %s %.2f %d%n", myString, myChar, userDouble, userInt);
// Cast the double to an integer, and output that integer
System.out.println(userDouble + " cast to an integer is " + (int) userDouble);
scanner.close();
}
}
Output
Enter integer: 99
Enter double: 3.77
Enter character: z
Enter string: Howdy
99 3.77 z Howdy
Howdy z 3.77 99
3.77 cast to an integer is 3
If you want to take all the inputs at once, you can do the following:
import java.util.Scanner;
public class BasicInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Inputs
int userInt;
double userDouble;
char myChar;
String myString;
System.out.print("Enter an integer, double, character, and a string: ");
userInt = scanner.nextInt();
userDouble = scanner.nextDouble();
myChar = scanner.next(".").charAt(0); // See: https://stackoverflow.com/a/13942707/1762224
myString = scanner.nextLine().trim();
// Output the four values in order
System.out.printf("%d %.2f %s %s%n", userInt, userDouble, myChar, myString);
// Output the four values in reverse order
System.out.printf("%s %s %.2f %d%n", myString, myChar, userDouble, userInt);
// Cast the double to an integer, and output that integer
System.out.println(userDouble + " cast to an integer is " + (int) userDouble);
scanner.close();
}
}
Output
Enter an integer, double, character, and a string: 99 3.77 z Howdy
99 3.77 z Howdy
Howdy z 3.77 99
3.77 cast to an integer is 3
If you want to be strict about which charcter can be entered, you can use a pattern:
myChar = scanner.next("[A-Za-z]").charAt(0); // Only alpha