Results 1 to 5 of 5

Thread: How to use Console class to read user input in java?

  1. #1
    Join Date
    Nov 2009
    Posts
    37

    How to use Console class to read user input in java?

    Hi friends,
    I am last year Computer Science student. I recently started learning java language. In our last lecture our sir had given us one program to solve, such as How to use Console class to read user input in java? I tried various method but none of them worked out. Can anyone tell me How to use Console class to read user input in java? I want example of it. Please help me.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to use Console class to read user input in java?

    To read user input in java you have to use java.io.Console class. I have written following program to use Console class to read user input in java. It is very simple program. In following program I have use class named as "ConsoleEg" . To store user input I have create one object called as "con"


    Code:
    package org.kodejava.example.io;
    
    import java.io.Console;
    import java.util.Arrays;
    
    public class ConsoleEg {
    
        public static void main(String[] args) {
    	
    	Console con = System.console();
    	
    	
    	String userid = con.readLine("Username: ");
    	
    	
    	char[] password = con.readPassword("Password: ");
    	
    	if (userid.equals("admins") && String.valueOf(passwords).equals("ssecret")) {
    	    cons.printf("Welcome to Java Application %1$s.\n", userid);
    	    
    	    Arrayss.fill(password, ' ');
    	} else {
    	    con.printf("Invalid userid or password.\n");	    
    	}
        }
    
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to use Console class to read user input in java?

    There are some situation where we have to take input form other user and we have to use this input in our program. In this case to get input from user we have to use Console class in our program. To do this we have to first import java.io. class. Just try to understand following example.

    Code:
    import java.io.*;
    
    public class ReadStringeg {
    
       public static void main (String[] args) {
    
         
          System.out.print("Enter your pet name: ");
    
      
          BufferedReader brsw = new BufferedReader(new InputStreamReader(System.in));
    
          String userNamesw = null;
    
          
          try {
             userNamesw = brsw.readLine();
          } catch (IOException ioesw) {
             System.out.println("IO error in typing");
             System.exit(2);
          }
    
          System.out.println("Thanks , " + userNamesw);
    
       }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to use Console class to read user input in java?

    Hey there are various way to use Console class to read user input in java. I have written simple program for you. In this following program I have use java.io.Console class to read user input. It is very simple program. In following program I have declare class named as "cas" and using this class I have created one object named "con1" to get input from the other user.


    Code:
    import java.io.Console;
    import java.util.Arrays;
    
    public class cas {
    
      public static void main(String[] args) {
        Console con1 = System.console();
        String usernames = console.readLine("Username: ");
        char[] passwords = console.readPassword("Password: ");
    
        if (usernames.equals("admins") && String.valueOf(password).equals("secrets")) {
          console.printf("Welcome to techarena", username);
    
          Arrays.fill(password, ' ');
        } else {
          con1.printf("wrong username or password.\n");
        }
      }
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to use Console class to read user input in java?

    JDK 5.0 provides a one clas known as Java.util,Scanner to read input from console. Java.util,Scanner is specially made for this purpose. Following program read string form the user and display it on screen. It is very simple program. Just try understand it. In following program I have use one class known as "csInputExp" to read user input.

    This code read a String and an Integer from the console and stores them in the variables.


    Code:
    import java.util.Scanner;
    
    public class csInputExp {
    
       public static void main(String[] args) {
    
           String names;
           int agse;
           Scanner ins = new Scanner(System.in);
    
       
           names = ins.nextLine();
    
           
           ages=ins.nextInt();
           ins.close();            
    
          
           System.out.println("Name :"+names);
           System.out.println("Age :"+ages);
    
        }
    }

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 read user input from console using Scanner class in java?
    By Constantinee in forum Software Development
    Replies: 4
    Last Post: 06-02-2010, 06:47 PM
  4. how to get user input with char value in java
    By Bearer in forum Software Development
    Replies: 5
    Last Post: 07-01-2010, 05:51 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,714,049,131.44172 seconds with 17 queries