I am trying how to give this code the correct spacing. It needs to be equally sp
ID: 3781100 • Letter: I
Question
I am trying how to give this code the correct spacing. It needs to be equally spaced.
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 userChar;
String userString;
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: ");
userChar = scnr.next().charAt(0);
System.out.println(" Enter string: ");
userString = scnr.next();
System.out.print("" + userInt + " " + userDouble + " " + userChar + " " + userString);
// FIXME (2): Output the four values in reverse
System.out.println(userString + " " + userChar + " " + userDouble + " " + userInt);
// FIXME (3): Cast the double to an integer, and output that integer
int castInt = (int)userDouble;
System.out.println("" + userDouble + " cast to an integer is " + castInt);
return;
}
}
Explanation / Answer
import java.util.Scanner;
public class Conversions {
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 userChar;
String userString;
System.out.print(" 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.print("Enter double: ");
userDouble = scnr.nextDouble();
System.out.print("Enter character: ");
userChar = scnr.next().charAt(0);
System.out.print(" Enter string: ");
userString = scnr.next();
System.out.print("" + userInt + " " + userDouble + " " + userChar + " " + userString);
// FIXME (2): Output the four values in reverse
System.out.println(userString + " " + userChar + " " + userDouble + " " + userInt);
// FIXME (3): Cast the double to an integer, and output that integer
int castInt = (int)userDouble;
System.out.println("" + userDouble + " cast to an integer is " + castInt);
return;
}
}
actually your are using system.out.println and in that again"/n" new line charecter
thats why the allignment or spacing is not in a proper way So i just removed new line and ln and tested the output is as expected by you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.