Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Reading user input from the console with the Scanner class

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Reading user input from the console with the Scanner class

    import java.util.Scanner;
     
    public class Class1 {
     
     public static void main(String[] args) {
     
      System.out.println("Enter your name: ");
     
      Scanner sc = new Scanner(System.in);
     
      String name = sc.next(); 
     
      System.out.println("Hello " + name);
     
      sc.close();
     
     }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  2. The Following User Says Thank You to JavaPF For This Useful Post:

    binhyuky (November 3rd, 2012)


  3. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Read user input from the console with the Scanner class

    hi what if its an integer?

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to Read user input from the console with the Scanner class

    Read the API doc for Scanner class for the methods it has for reading different types of data.

  5. #4
    Junior Member
    Join Date
    Sep 2011
    Location
    Malaysia
    Posts
    6
    My Mood
    Hungover
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to Read user input from the console with the Scanner class

    import java.util.Scanner;
     
    public class Class1 {
     
     public static void main(String[] args) {
     
      System.out.println("Enter integer: ");
     
      Scanner sc = new Scanner(System.in);
     
      int num = sc.nextInt(); 
     
      System.out.println("Integer you entered is : " + num);
     
      sc.close();
     
     }

    this should help
    Last edited by sam2730; September 7th, 2011 at 03:14 AM.

Similar Threads

  1. Replies: 1
    Last Post: November 2nd, 2012, 02:21 PM
  2. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  3. [SOLVED] Problem in Coin-counter with scanner class
    By coccoster in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: March 25th, 2009, 08:46 AM
  4. Replies: 1
    Last Post: May 13th, 2008, 08:08 AM