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

  • Thread starter Thread starter Itisjurrr
  • Start date Start date
  • Tags Tags
    Coding Java
AI Thread Summary
The discussion focuses on a Java coding issue where a user is trying to prompt for and read an integer, double, character, and string, but encounters an extra character in the output. The main problem arises from not properly reading input and managing newlines after reading numeric values, which leads to incorrect character input. Suggestions include using `scnr.nextLine()` to handle the newline after reading a double and using `charAt(0)` to extract the character from a string. Additionally, it's recommended to read the integer and double as strings first and then parse them, and to streamline the output by using a single print statement. Properly addressing these issues will resolve the extra character problem and improve the code's functionality.
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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top