Help with Java Coding: Inputting Integers, Doubles, Characters & Strings

  • Context: Java 
  • Thread starter Thread starter Itisjurrr
  • Start date Start date
  • Tags Tags
    Coding Java
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 replies · 8K views
Itisjurrr
Messages
1
Reaction score
0
Hows it going MHB. This is my first post and probably will not be my last. I am not going to come here just asking for answers, but to look for help in my coding adventure. I am currently enrolled into a entry level coding course that kind of just jumps right into things and does give some help with books, but not very useful at times. The problem that I am currently stuck on is this part
(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
99 3.77 z Howdy

The output of my code is showing me this
Enter interger: 99
Enter double: 3.77
Enter character: Z
Enter string: Z Howdy
99 3.77 Z Z Howdy

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 letterZ = '0';
      String firstWord = "";
      // FIXME Define char and string variables similarly
      
      userInt = scnr.nextInt();
      userDouble = scnr.nextDouble();
      letterZ = 'Z';
      firstWord = scnr.nextLine();
      System.out.println("Enter interger: " + userInt);
      System.out.println("Enter double: " + userDouble);
      System.out.println("Enter character: " + letterZ);
      System.out.println("Enter string: " + firstWord);
      System.out.print(userInt);
      System.out.print(" ");
      System.out.print(userDouble);
      System.out.print(" ");
      System.out.print(letterZ);
      System.out.print(firstWord);

      // FIXME (1): Finish reading other items into variables, then output the four values on a single line separated by a space
      // FIXME (2): Output the four values in reverse
      // FIXME (3): Cast the double to an integer, and output that integer
      return;
   }
}

That is what I need to accomplish in my code. I am some how getting an extra z when I put in my user inputs.
Thanks,
Jerry
 
on Phys.org
Hi, and welcome to the forum!

As far as I understand the problem statement, the program should print a prompt "Enter integer: ", then read an integer, and similarly for other types. Your program first reads values ([m]scnr.nextInt()[/m], etc.) without printing any prompts and then prints "Enter integer: " together with the integer that it read earlier.

Also, currently you are not reading a character but assigning 'Z' unconditionally. There is no method in the Scanner class to read a character, but in this problem I believe you can read the whole string and pick the first character:

[m]userChar = scnr.nextLine().charAt(0);[/m]

Don't forget to declare [m]userChar[/m]. There is one subtlety, though. After you read a double, the position of the scanner is before the newline. So when you say [m]scnr.nextLine()[/m] it reads until the following newline, which comes immediately, and this call therefore returns an empty string. In this case, calling [m]charAt(0)[/m] on it results in an exception because the string has no character with index 0. The solution is to call [m]scnr.nextLine()[/m] between reading a double and a character to consume the newline.

Your code calls the string variable that is read last [m]firstWord[/m]. If it is indeed supposed to store only the first word of the string typed by the user, then use [m]next()[/m] instead of [m]nextLine()[/m]. The [m]next()[/m] method reads the next token, and tokens are divided by whitespace by default, so it will only read the first word.

An alternative to reading a double and calling [m]nextLine()[/m] to consume the newline is to read the whole line using [m]nextLine()[/m] and then convert it to double as follows.

[m]userDouble = Double.parseDouble(scnr.nextLine());[/m]

But then the first integer should be read similarly.

[m]userInt = Integer.parseInt(scnr.nextLine());[/m]

Finally, printing the final line can be done using just one [m]print()[/m] statement since strings can be concatenated using the [m]+[/m] operator.