Staredit Network

Staredit Network -> Computers and Technical -> Input for Java
Report, edit, etc...Posted by Kow on 2006-08-27 at 15:02:11
I've been looking for how to do simple character/numerical input in Java. I want to be able to be prompted for an input (in the run part of it) and then input it and have that be the assigned value for a specific variable. Can I get an explanation and/or example of this? I've looked on google etc but can't find what I'm looking for.
Report, edit, etc...Posted by CheeZe on 2006-08-27 at 22:55:29
You need to create a BufferedReader object and use readLine().

CODE
import java.io.*;

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str1 = reader.readLine()
Report, edit, etc...Posted by Kow on 2006-08-28 at 15:07:19
can it be a char (or other) var type such as int or something?

And when is it prompted for the variable?

And where should this be placed? Inside class but outside the public static void main(String args[]), or does it matter?
Report, edit, etc...Posted by Voyager7456(MM) on 2006-08-28 at 15:16:21
Inside the public static etc... I believe.

You can parse the string back into a int, etc.

CODE

String str1 = reader.readLine();
int1 = Integer.parseInt(str1);
Report, edit, etc...Posted by Kow on 2006-08-28 at 19:34:51
*wishes he had the means to test this out*

Thanks. I don't think I'll have further questions, but I wouldn't put it past me.
Report, edit, etc...Posted by CheeZe on 2006-08-28 at 21:22:40
You should probably use "try" if you're trying to change data types. The placement doesn't matter, as long as the code is reached. So you could just create a new method like:

CODE
public String typed()
{
return reader.readLine()
}

Assuming reader was already created before.
Report, edit, etc...Posted by DT_Battlekruser on 2006-08-28 at 23:06:40
Yet another reason Python pwns.

raw_input tongue.gif
Report, edit, etc...Posted by CheeZe on 2006-08-28 at 23:09:10
He asked for Java. No need to start a which is better debate here. sad.gif

Closed. Don't unlock unless you're the creator needing more assistance.
Report, edit, etc...Posted by Kow on 2006-08-29 at 10:41:29
It's still not working for me. Could you create a simple ask and display the line program so I can see the format etc?
Report, edit, etc...Posted by CheeZe on 2006-08-29 at 18:32:03
Yeah, I was right. You need to have a try statement. Save this as Read.java:

CODE
import java.io.*;

public class Read
{
public static void main(String[] args)
{
 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
 String str1 = "";
 
 try
 {
  str1 = reader.readLine();
 }
 catch(Exception e){}
 
 System.out.println(str1);
}
}
Report, edit, etc...Posted by Kow on 2006-08-29 at 19:52:09
QUOTE(CheeZe @ Aug 29 2006, 06:31 PM)
import java.io.*;

public class Read
{
public static void main(String[] args)
{
  BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  String str1 = "";
 
  try
  {
  str1 = reader.readLine();
  }
  catch(Exception e){} //What's this do?
 
  System.out.println(str1);
}
}
[right][snapback]553198[/snapback][/right]
Report, edit, etc...Posted by Vibrator on 2006-08-29 at 20:07:00
I don't know too much about java but the catch part of a try catch statement returns a exception (the e variable there). It includes the type and other unformation about the error. Any error handling code would be done inbetween the {}
Report, edit, etc...Posted by DT_Battlekruser on 2006-08-29 at 23:29:28
I think it's the equivalent of an except Exception in Python.
Report, edit, etc...Posted by Rantent on 2006-08-29 at 23:57:08
When your reading the string, your asking it to do something, if it cannot do this for some reason, then what action should it take.

You could also use JOptionPanes for it, which are quite a bit more user friendly, (They have a nice little box that pops up and can ask you questions.) but it might be over your head as of now. (Just considering. wink.gif )
Although that requires extending javax.swing which if your not sure how to implement things, best leave alone.
Report, edit, etc...Posted by Kow on 2006-08-30 at 14:57:37
That worked for strings, and I found one for integers (changes string to int) so thanks to all of you! <3
Next Page (1)