Scanner input data types

15,648

First, there's no need to wrap System.out in a PrintStream because out already supports formatting with format() or printf() methods.

Next, you need to understand that when you input a line of data you also terminate it with a new line \n. The next<Type>() methods only consume the <Type> and nothing else. So, if a next<Type>() call may match \n, you need to skip over any extra new lines \n with another nextLine() before.

Here's your code with fixes:

  int num;
  double num2;
  String name;
  char c;

  Scanner sc = new Scanner(System.in);

  //for integer
  System.out.print("Enter a number: ");
  num = sc.nextInt();
  System.out.printf("%d\n", num);

  //for float
  System.out.print("Enter a float value: ");
  num2 = sc.nextDouble();
  System.out.printf("%.2f\n", num2);

  //for name w/o white space
  System.out.print("Enter your first name: ");
  name = sc.next();
  System.out.printf("Hello %s, welcome to Scanner\n", name);

  //for character
  System.out.print("Enter a character: ");
  c = sc.findWithinHorizon(".", 0).charAt(0);
  System.out.printf("%c\n", c);

  sc.nextLine(); // skip

  //for name w/ white space
  System.out.print("Enter your full name: ");
  name = sc.nextLine();
  System.out.printf("%s", name);
Share:
15,648

Related videos on Youtube

Claude Rhay
Author by

Claude Rhay

Hello, i'm actually new to this community, more likely, new to the Programming World. I've been trying to study the basics of it, but it seems like I have a lot of questions to ask. I've been reading different tutorials and other stuffs. Right now, i'm learning the basics and fundamentals of Java I hope this community would be able to help me with different other stuffs.

Updated on September 17, 2022

Comments

  • Claude Rhay
    Claude Rhay over 1 year

    I need to write a test class that will do the following:

    • a. Let the user input an integer and display it.
    • b. Let the user input a float value and display it.
    • c. Let the user input his/her name (no white spaces) and display the name as: “Hello <name>, welcome to Scanner!”
    • d. Let the user input a character and display it.
    • e. Let the user input any string (with white spaces) and display it.

    My questions is, how can I simply scan just a Character and display it? And in number 2, How can I input a String with white spaces and display it? (letters "d" and "e")

    I've searched around, but I cannot find the simplest solution (since I'm new to Java and programming).

    Here is my code so far:

    package aw;
    
    import java.io.PrintStream;
    import java.util.Scanner;
    
    public class NewClass1
    {
      public static void main(String[] args)
      {
          int num;
          double num2;
          String name;
          char c;
              Scanner sc = new Scanner(System.in);
              PrintStream ps = new PrintStream(System.out);
    
          //for integer
          System.out.println("Enter a number: ");
          num = sc.nextInt();
          ps.printf("%d\n", num);
    
          //for float
          System.out.println("Enter a float value: ");
          num2 = sc.nextDouble();
          ps.printf("%.2f\n", num2);
    
          //for name w/o white space
          System.out.print("Enter your first name: ");
          name = sc.next();
          ps.printf("Hello %s, welcome to Scanner\n", name);
    
    
          //for character
          System.out.print("Enter a character: ");
          c = sc.findWithinHorizon(".", 0).charAt(0);
          System.out.print(“%c”, c);
    
          //for name w/ white space
          System.out.print("Enter your full name: ");
          name = sc.nextLine();
          System.out.print(“%s”, name);
      }
    }
    

    I hope you can help me. Thanks!

  • Claude Rhay
    Claude Rhay over 10 years
    when I run the program, sc.nextLine doesn't allow me to scan, it ends the program right away.