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

  • Context: Java 
  • Thread starter Thread starter Itisjurrr
  • Start date Start date
  • Tags Tags
    Coding Java
Click For Summary
SUMMARY

The discussion focuses on resolving issues in a Java program that prompts users to input an integer, a double, a character, and a string, and outputs these values. The user, Jerry, encounters an issue where an extra character appears in the output. The solution involves correctly reading inputs using the Scanner class, specifically addressing the need to consume newlines after reading numeric values. Key corrections include using scnr.nextLine().charAt(0) for character input and Integer.parseInt(scnr.nextLine()) for integer input to avoid reading empty strings.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of the Scanner class in Java
  • Basic knowledge of data types: integers, doubles, characters, and strings
  • String manipulation techniques in Java
NEXT STEPS
  • Learn about Java Scanner class methods, specifically nextInt(), nextDouble(), and nextLine()
  • Explore Java string manipulation, including charAt() and substring()
  • Investigate error handling in Java to manage exceptions like StringIndexOutOfBoundsException
  • Practice reading and converting user input in Java using Integer.parseInt() and Double.parseDouble()
USEFUL FOR

Beginner Java programmers, coding students, and anyone looking to improve their skills in handling user input and data types in Java applications.

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
 
Technology news 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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K
Replies
4
Views
2K
Replies
3
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K