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

  • MHB
  • Thread starter Itisjurrr
  • Start date
  • Tags
    Coding Java
In summary: 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.println("Enter interger: " + userInt);System.out.println("Enter double: " + userDouble);System.out.println("Enter character: " + letterZ);System.out.println("Enter string: " + firstWord);In summary, the user types in an integer, a double, a character, and a
  • #1
Itisjurrr
1
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
  • #2
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.
 

What is Java Coding?

Java coding is the process of writing instructions in the Java programming language to create computer programs or applications. It involves using specific syntax and logic to create functional and efficient programs.

What is the difference between Integers, Doubles, Characters, and Strings in Java?

Integers, doubles, characters, and strings are different data types in Java. Integers are whole numbers, doubles are numbers with decimal points, characters are single letters or symbols, and strings are sequences of characters. They are used to store different types of data and perform different operations on them.

How do I input Integers, Doubles, Characters, and Strings in Java?

To input integers, doubles, characters, and strings in Java, you can use the Scanner class, which allows you to read user input from the keyboard. You can create a Scanner object and use its methods to prompt the user for input and store it in a variable of the appropriate data type.

Can I convert one data type to another in Java?

Yes, you can convert one data type to another in Java using type casting. Type casting is the process of changing the data type of a variable to another data type. For example, you can convert an integer to a double by using the double data type before the variable name, such as double myDouble = (double) myInteger;

How do I handle errors when inputting data in Java?

You can handle errors when inputting data in Java by using exception handling. This involves using try-catch blocks to catch any errors that may occur during the input process. You can also use conditional statements and loops to validate user input and ensure that the program runs smoothly.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
3
Views
5K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top