Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Capture String Input ?

Status
Not open for further replies.

darr01

Programmer
Oct 24, 2001
28
0
0
MY
Can anyone show me how to capture a string that is input by a user?

I am coding a simple console (DOS based) interface that needs some user input. For e.g. asking a user to input their name and search an array or link-list for that name.

I know I can use System.in.read() to capture integer but no success in capturing Strings.

Of course I can do that easily in C or C++ but I am confused in what to do in Java.

Thanks in advance,

Darryl
 
Very Simple. Wrap System.in with a BufferedReader and then use the readLine() method of the BufferedReader.
 
Thanks for the fast response. Will give it a try.

Regards,

Darryl
 
People usually use a class for taking inputs from user. I use a class read that has all the methods to take a console input from the user i.e. to take string, char, int, byte, float or double. I'll just give you the whole class.

// This class takes care of the input just use read.GetString() to take the input as string as vice versa

import java.io.*;

public class read {
static BufferedReader In = new BufferedReader( new
InputStreamReader(System.in));


// GetInt will ignore ANY input other than a valid integer
public static int GetInt() throws java.io.IOException {
while (true) {
try {
String s = (In.readLine()).trim();
int y = (new Integer(s)).intValue();
return y;
}
catch (java.lang.NumberFormatException e) {
}
}
}

// GetLong will ignore ANY input other than a valid long integer
public static long GetLong() throws java.io.IOException {
while (true) {
try {
String s = (In.readLine()).trim();
long y = (new Long(s)).longValue();
return y;
}
catch (java.lang.NumberFormatException e) {
}
}
}

// GetFloat will ignore ANY input other than a valid floating point number
public static float GetFloat() throws java.io.IOException {
while (true) {
try {
String s = (In.readLine()).trim();
float y = (new Float(s)).floatValue();
return y;
}
catch (java.lang.NumberFormatException e) {
}
}
}

// GetDouble will ignore ANY input other than a valid double-precision
// floating point number
public static double GetDouble() throws java.io.IOException {
while (true) {
try {
String s = (In.readLine()).trim();
double y = (new Double(s)).doubleValue();
return y;
}
catch (java.lang.NumberFormatException e) {
}
}
}

public static char GetChar() throws java.io.IOException {
char c = (char) In.read();
return c;
}

public static String GetString() throws java.io.IOException {
String s = (In.readLine()).trim();
return s;
}

public static void FlushInput() throws java.io.IOException {
String s = (In.readLine());
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top