Accepting String Input in the Console

I've seen a few different ways to acceptstring input and I'd l

Page 1 of 1

5 Replies - 13181 Views - Last Post: 12 May 2009 - 11:27 PM Rate Topic: -----

#1 Ivandreamincode00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 09-October 08

Accepting String Input in the Console

Posted 10 May 2009 - 10:43 AM

I'm working on accepting string input from the console and although I understand most of what I see I'd like to know which method you prefer to use. I've seen at least two different ways. The following is code I pulled of the internet.
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);

	// read in user input 

try
{

	string = reader.readLine(); 

}
catch(Exception e){}
System.out.println("You typed: " + string);

	// wait for user to type 'Enter' so console window won't dissapear

System.out.println("Type 'Enter' to exit.");

try
{

	string = reader.readLine(); 

}
catch(Exception e){e.PrintStackTrace();}



Also, what is a try/catch statement?

	  import java.io.*;

	  public class Test
	  {
		  public static void main(String[] args)
		  {
			  try
			  {
			  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
			  String userInput = in.readLine();
			  System.out.println("\n\nUser entered -> " + userInput);
			  }
			  catch(IOException e)
			  {
			   System.out.println("IOException has been caught");
			   }
		   }
	  }



import java.util.Scanner;

public class InputExp {

   public static void main(String[] args) {

	   String name;
	   int age;
	   Scanner in = new Scanner(System.in);

	   // Reads a single line from the console 
	   // and stores into name variable
	   name = in.nextLine();

	   // Reads a integer from the console
	   // and stores into age variable
	   age=in.nextInt();
	   in.close();			

	   // Prints name and age to the console
	   System.out.println("Name :"+name);
	   System.out.println("Age :"+age);

	}
}



And in this last version do you need to include the in.close()???

I'm trying to learn all of this on my own so some help would be greatly appreciated. Thank you all in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Accepting String Input in the Console

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Accepting String Input in the Console

Posted 10 May 2009 - 11:04 AM

A try/catch structure is a structure that can execute code and look for any thrown exceptions along the way. It's used to look for exceptions where you want different things to happen based on what kind of exception is thrown.

All of those ways are valid ways of reading input from the user. However, in the last one, you need to have JDK 5 or later, because before that, the Scanner class did not exist. And the in.close() is not required.

Hope this helps! If you need any more info, just ask! :)

This post has been edited by Locke: 10 May 2009 - 11:04 AM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Accepting String Input in the Console

Posted 10 May 2009 - 06:11 PM

NEVER NEVER NEVER a good idea having a catch() close that does nothing ... at least it should write an error message displaying the Exception
Was This Post Helpful? 0
  • +
  • -

#4 Ivandreamincode00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 09-October 08

Re: Accepting String Input in the Console

Posted 12 May 2009 - 05:03 PM

Thanks guys. If anyone else wants to add more, please do so. I'm kinda learning this on my own so any input is greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Accepting String Input in the Console

Posted 12 May 2009 - 08:07 PM

Take the Scanner it is the easiest one
If it has exist in JRE 1.1 nobody wourl write code with an InputStreamReader(System.in);

Scanner will remove the need for try/catch clauses
Easiest way always read a full line then use a StringTokenizer to get all the "token" whithin and without the spaces

Scanner in = new Scanner(System.in);
while the is stuff to read {
	 String str = in.nextLine();		 // read full line
	 StringTokenizer st = new StringTokenizer(str);
	 while(st.hasMoreTokens()) {
		String token = st.nexToken();
		....
	  }
}


if user enters: " abc de 003.1456 done"
token will value:
"abc"
"de"
"003.1456"
"done"

This post has been edited by pbl: 13 May 2009 - 04:35 AM

Was This Post Helpful? 0
  • +
  • -

#6 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Accepting String Input in the Console

Posted 12 May 2009 - 11:27 PM

paul you forgot something :wink:

hint if we expect those who need help to do it, we should also remember to do it. "/" :wink:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1