System.in.read (newbie)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: System.in.read (newbie)

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Posts
    2

    System.in.read (newbie)

    i was reading up on this syntax and it said that this automatically stores everything as int and ot convert it to char you would need to do something like - ch = (char) System.in.read();. well i was wondering if there is a way to convert to and store a string. i am basically looking to store a string value (name) through user input.

  2. #2
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Quote Originally Posted by someone
    i was reading up on this syntax and it said that this automatically stores everything as int and ot convert it to char you would need to do something like - ch = (char) System.in.read();. well i was wondering if there is a way to convert to and store a string. i am basically looking to store a string value (name) through user input.
    Then don't use System.in.read but InputStreamReader and BufferedReader. Here's an example:

    Code:
    import java.io.*;
    
    public class GetName {
        public static void main(String[] args) {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String name = "";
    
            // continue when something is entered.
            while(name.equals("")) {
                System.out.print("Hi there, enter your name: ");
                try {
                    name = br.readLine();
                }
                catch(Exception e) { e.printStackTrace(); }
            }
            
            // print the name
            System.out.println("\nWelcome "+name+"!\n");
        }
    }
    Good luck.

  3. #3
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    10
    this is the code we were given to read input from the command prompt window. it'll take the whole thing as a string until the enter key is pushed.
    Code:
    import java.io.*;
    public class Keyboard {
    	private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    	public static String readInput() {
    		try {
    			return in.readLine();
    		}
    		catch (IOException e) {
    			System.out.println("An error has occurred in the Keyboard.readInput() method");
    			System.out.println(e.toString());
    			System.exit(-1);
    		}
    		return null;
    	}
    }
    Basically it creates a buffered reader using System.in as the argument. then it'll read a line of text as return it.
    and if an IO exception is caught then you'll know about it.
    I prefer to live in my own shadow. At least then I can be compared to a false past I'm more familiar with

  4. #4
    Join Date
    Aug 2004
    Posts
    46
    Tiger lovin':
    Code:
    Scanner in  = new Scanner(System.in);
    System.out.print("Enter your name: ");
    String input = in.next();
    System.out.println("Hello, " + input);

Similar Threads

  1. newbie. Really Newbie
    By Itai Raz in forum Open Source
    Replies: 2
    Last Post: 09-01-2003, 05:22 PM
  2. Newbie getting started in programming
    By Benno in forum Careers
    Replies: 10
    Last Post: 02-10-2002, 07:07 PM
  3. Is VB.Not still a BEGINNERS' language?
    By Mark Burns in forum .NET
    Replies: 164
    Last Post: 03-13-2001, 12:43 PM
  4. Replies: 1
    Last Post: 12-07-2000, 06:11 PM
  5. Newbie Question about Classes...
    By Anthony Saffer in forum Java
    Replies: 1
    Last Post: 09-22-2000, 11:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center