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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Input ways

Status
Not open for further replies.

haiderabbas

Programmer
Apr 1, 2001
1
PK
hi sir
tell me the ways to input in java
except System.in.read
and how many values can be entered at a time in java
bye
Haider
 
use the Swing JOptionPane.showInputDialog component method.

/ BufferedReader resides in the java.io package
import java.io.*;

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter A Value: "); // this is a cout
System.out.flush(); // flushing the out buffer
String theValue = in.readLine(); // mocking cin


If you want to read in a int from commandline, look at the following codes :

try
{
int temp = (new Integer((String)in.readLine())).intValue();
}
catch (NumberFormatException e)
{
System.out.println("Input is not a number.");
}

For JOptionPane, it could be used in both UI applications and applets. So which you are
going to use will depend on whether you are coding a UI or not.




Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
After reading in the line you could tokenize (java.util.StringTokenizer) the String to get all the inputs on that line.
From there you can cast the Strings values to the datatypes your program requires for execution.

I hope this helped! ;-)
- Casey Winans
 
hi sir
tell me easy rules for casting
from int to byte or etc..
easy formulas plz
thanx
bye
haider
 
I'm just offering some insight. I don't garantee this code... I'm just giving you some creative advice and/or java pseudo-code.

import java.io.*;

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter A Value: ");
System.out.flush();

// by default spaces are used as delimiter
StringTokenizer st = new StringTokenizer(in.readLine());

// temp variable to aid in casting
Integer i_temp = new Integer(0);
Double d_temp = new Double(0.0);
Byte b_temp = new Byte("s");

int int_val;
double dbl_val;
String str_val;
byte byt_val;

while(st.hasMoreTokens()) {
// nextToken returns an object so a
// specific cast is required such as...

// for casting to primitive type int
i_temp = (Integer)st.nextToken();
int_val = i_temp.intValue();


// for casting to primitive type double
d_temp = (Double)st.nextToken();
dbl_val = d_temp.doubleValue();


// for casting to a String
str_val = (String)st.nextToken();


// for casting to a byte (??)
b_temp = (Byte)st.nextToken();
byt_val = b_temp.byteValue();

} I hope this helped! ;-)
- Casey Winans
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top