Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

I am working on this lab and no matter what I do I can't get this coding to work correctly. Please help!! Here's the assignment.

I am working on this lab and no matter what I do I can't get this coding to work correctly. Please help!!
Here's the assignment.
1.38 Variables, input, and casting (Java)
(1) Prompt the user to input an integer, a double, a character, and a string, storing each into separate variables. Then, output those four values on a single line separated by a space. (Submit for 2 points).
Enter integer: 99
Enter double: 3.77
Enter character: z
Enter string: Howdy
993.77 z Howdy
(2) Extend to also output in reverse. (Submit for 1 point, so 3 points total).
Enter integer: 99
Enter double: 3.77
Enter character: z
Enter string: Howdy
993.77 z Howdy
Howdy z 3.7799
(3) Extend to cast the double to an integer, and output that integer. (Submit for 2 points, so 5 points total).
Enter integer: 99
Enter double: 3.77
Enter character: z
Enter string: Howdy
993.77 z Howdy
Howdy z 3.7799
3.77 cast to an integer is 3
Here's my coding:
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;
char userChar;
String userString;
System.out.print("Enter integer: ");
userInt = scnr.nextInt();
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.println(userInt +""+ userDouble +""+ userChar +""+ userString);
System.out.println(userString +""+ userChar +""+ userDouble +""+ userInt);
System.out.println(userDouble +" cast to an integer is "+(int)userDouble);
return;
}
}
Here's the output I'm getting which is incorrect:
Program output displayed here
Enter integer: Enter double: Enter character: Enter string: 993.77 z Howdy
Howdy z 3.7799
3.77 cast to an integer is 3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+ (b) If F is continuous, then E[F(X)) =;.

Answered: 1 week ago