Results 1 to 5 of 5

Thread: How to read user input from console using Scanner class in java?

  1. #1
    Join Date
    Dec 2009
    Posts
    39

    How to read user input from console using Scanner class in java?

    Hi friends,
    I am new to this forum. I am second year Computer Science student. I recently start learning java language. In our last lecture we learn about Scanner class, but I didn't understand this concept clearly. I want example of it. Can anyone tell me how to read user input from console using Scanner class in java? please help me.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to read user input from console using Scanner class in java?

    Hey in the following program I have use java.util.Scanner class to accept or read user input in console application. This class is used to read string, integer, long, etc from the console. In the following program I have use java.util.Scanner class. Just try to understand it. It is very simple program.


    Code:
    package sample.code.java.example.;
    
    import java.util.Scanner;
    
    public class ScannerEg {
        public static void main(String[] argss) {
    	Scanner scannerss = new Scanner(System.ins);
    	
    	
    	System.out.print("Usernames: ");
    	String usernames = scanner.nextLine();
    	
    	
    	System.out.print("Passwords: ");
    	String passwords = scanners.nextLines();
    	
    	
    	System.out.print("What is 9 + 2: ");
    	int resultss = scanners.nextInt();
    	
    	if (usernames.equals("admins") && passwords.equals("secrets") && result == 8) {
    	    System.out.println("Welcome to Java world);
    	} else {
    	    System.out.println("Wrong username or password, access denied!");	    
    	}
        }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to read user input from console using Scanner class in java?

    JDK 5.0 provides one class known as Java.util,Scanner which is used to read input from console. .It is very easy process. You have to just use "import java.util.Scanner" line in starting to do this. The following code is read String and an Integer from the console and then after store this data in one variable.
    Code:
    import java.util.Scanner;
    
    public class InputExp {
    
       public static void main(String[] args) {
    
           String names;
           int ages;
           Scanner ins = new Scanner(System.ins);
    
        
           names = ins.nextLine();
    
           ages=ins.nextInt();
           ins.close();            
    
           
           System.out.println("Name :"+names);
           System.out.println("Age :"+ages);
    
        }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to read user input from console using Scanner class in java?

    Scanner class is used to read input from the console. You have to use import java.util.Scanner line in the beginning to solve such type of problem. I have written following program for you. It is very simple program. In the following program I have create one class known as MainClassEg to execute all methods. I also have create one object of scanner class to accept user input from the user.



    Code:
    import java.util.Scanner;
    
    class Console {
    
      static Scanner scw = new Scanner(System.in);
    
      public static boolean askYesorNo(String prompt) {
        while (true) {
          String answers;
          System.out.print("\n" + prompt + " (Yes or No) ");
          answers = scw.next();
          if (answers.equalsIgnoreCase("yes"))
            return true;
          else if (answers.equalsIgnoreCase("No"))
            return false;
        }
      }
    }
    
    public class MainClassEg {
      public static void main(String[] args) {
        while (Console.askYesorNo("just keep going?")) {
          System.out.println("!");
    
        }
      }
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to read user input from console using Scanner class in java?

    It is very easy process to read user input from the console using Scanner class. You have to just create one object of scanner class to accept user input from the user. It is very simple program. I have written very simple program for you. Just try to understand it.


    Code:
    import java.io.*;
    import java.util.*;
    
    public class ScanConsoleEg
    {
      public static void main (String arg[]) {
    
        
        Scanner scanners1 = new Scanner (System.in);
    
        try {
          System.out.printf ("Input int (e.g. %5d): ",6794);
          int int_vals = scanners1.nextInt ();
          System.out.println (" You  have entered " + int_vals +"\n");
    
          System.out.printf ("Input float (e.g. %3.2f): ", 6.35);
          float float_vals = scanners1.nextFloat ();
          System.out.println (" You have entered " + float_vals +"\n");
    
          System.out.printf ("Input double (e.g. %6.2e): ",1.2568);
          double double_vals = scanners1.nextDouble ();
          System.out.println (" You entered " + double_vals +"\n");
    
        }
        catch  (InputMismatchException es) {
          System.out.println ("Mismatch exception:" + es );
        }
      } 
    
    }

Similar Threads

  1. Java will not read input
    By rACHAEL in forum Software Development
    Replies: 5
    Last Post: 16-10-2010, 09:40 AM
  2. Java - scanner package for user input
    By Shaan12 in forum Software Development
    Replies: 4
    Last Post: 21-07-2010, 02:49 PM
  3. How to use Console class to read user input in java?
    By MABON in forum Software Development
    Replies: 4
    Last Post: 04-02-2010, 10:17 PM
  4. Scanner can't read in java
    By cyber-noob in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 12:42 PM
  5. Getting Username and Password with New java.io.Console Class
    By Cody in forum Software Development
    Replies: 1
    Last Post: 30-12-2008, 06:29 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,690,874.30968 seconds with 17 queries