Answered step by step
Verified Expert Solution
Link Copied!

Question

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 input: (not sure if this might be the problem)
99
3.77
z
Howdy
Here's my code:
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.println("Enter integer:");
userInt = scnr.nextInt();
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.println(userInt +""+ userDouble +""+ userChar +""+ userString);
System.out.println(userString +""+ userChar +""+ userDouble +""+ userInt);
int castedInt =(int) userDouble;
System.out.println("Casting "+ userDouble +" to an integer is "+ castedInt);
scnr.close();
}
}
Here's the error that I am receiving:
Output differs. See highlights below.
Special character legend
Input
99
3.77
z
Howdy
1:Compare output
Your output starts with
Enter integer:
Enter double:
Enter character:
Enter string:
993.77 z Howdy
Howd
Expected output starts with
Enter integer:
Enter double:
Enter character:
Enter string:
993.77 z Howdy
2:Compare output
Output differs. See highlights below.
Special character legend
Input
98765555
0.01
A
Plethora
Your output starts with
Enter integer:
Enter double:
Enter character:
Enter string:
987655550.01 A Plethora
Plet
Expected output starts with
Enter integer:
Enter double:
Enter character:
Enter string:
987655550.01 A Plethora
3:Compare output
Output differs. See highlights below.
Special character legend
Input
99
3.77
z
Howdy
Your output starts with
Enter integer:
Enter double:
Enter character:
Enter string:
993.77 z Howdy
Howdy z 3.7799
Cast
Expected output starts with
Enter integer:
Enter double:
Enter character:
Enter string:
993.77 z Howdy
Howdy z 3.7799
4:Compare output
Output differs. See highlights below.
Special character legend
Input
99
3.77
z
Howdy
Your output
Enter integer:
Enter double:
Enter character:
Enter string:
993.77 z Howdy
Howdy z 3.7799
Casting 3.77 to an integer is 3
Expected output
Enter integer:
Enter double:
Enter character:
Enter string:
993.77 z Howdy
Howdy z 3.7799
3.77 cast to an integer is 3
5:Compare output
Output differs. See highlights below.
Special character legend
Input
54
6.02
m
Bye
Your output
Enter integer:
Enter double:
Enter character:
Enter string:
546.02 m Bye
Bye m 6.0254
Casting 6.02 to an integer is 6
Expected output
Enter integer:
Enter double:
Enter character:
Enter string:
546.02 m Bye
Bye m 6.0254
6.02 cast to an integer is 6

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