Computer Science Canada

java user input

Author:  HeavenAgain [ Mon Feb 26, 2007 8:15 pm ]
Post subject:  java user input

hi, I have a quick question on how to get user's input; byte, int, short, long, floating points too.
you see i use this In.class at school, but i really want to learn the things behind it.
would someone explain it to me quick? thanks Razz

Author:  Aleks [ Tue Feb 27, 2007 12:05 am ]
Post subject:  Re: java user input

You do not need any extra classes in your directory to get user input (for example, your in.class is unnecessary). Some classes make it real easy, but Java already has a built in way of handling simple user input.

This is a nice way of getting user input via command console type programs. Of course I am assuming you just started Java Cool

At the beginning of your code type:

code:
import java.io.*;


Then somewhere in your main method (before you take in any user input) add the line:

code:
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));


To get input, simply add the line:

code:
myVariable = userInput.readLine ();


However, doing this only yields a String as an input. We must use a wrapper class if we are to get different types of input. Here is how you do it.

code:
// To read a double.
myDoubleVariable = Double.parseDouble(userInput.readLine());

// To read an integer.
myIntegerVariable = Integer.parseInt(userInput.readLine());

// etc.


And this my friend, is simple user input. I hope it helped. ^_^

Author:  klopyrev [ Tue Feb 27, 2007 12:13 am ]
Post subject:  Re: java user input

I am going to explain how to read from Standard Input. If you want to use BufferedReader and other things like that, look at the post above mine. If you are talking about Standard Input, then System.in.read() is how you read input. It returns the next character. If you want to read an int, read everything until you get a space or an enter and then parse the input. The same for Byte, Float, etc. Use Integer.parseInt( ... ) or Double.parseDouble( ... ). If you have any more questions or I didn't explain everything, feel free to ask.

Example (This code will read an int from standard input. It doesn't take care of Errors and Exceptions):

String result="";
int ch = System.in.read();
while(ch>='0'&&ch<='9')
{
result+=(char)ch;
ch = System.in.read();
}
int number = Integer.parseInt(result);

Author:  HeavenAgain [ Wed Feb 28, 2007 5:44 pm ]
Post subject:  Re: java user input

ok.. i get it
but i got a few questions now,
1st, there is something about throwing exception, that i dont get it, but i'm just doing this
public static void main (String [] args) throws Exception

will that affect my program?

and also there is some more complex input, such as creating a new class to do the input? i heard that will be much better, and you get to have more "control" over it? i'm just curious about the input, maybe i'm wrong. but thanks! Very Happy

Author:  [Gandalf] [ Wed Feb 28, 2007 6:49 pm ]
Post subject:  Re: java user input

Java I/O has become a lot easier in Java 1.5 using the Scanner class:
Java:
import java.util.*;
public class TestInput {
        public static void main(String[] args) {
                Scanner in = new Scanner(System.in);
                System.out.println("Foo bar fazdfa?");
                String fooBar = in.nextLine();   
        }
}

That's off the top of my head, so not sure if it compiles, it should. Smile

Author:  HeavenAgain [ Wed Feb 28, 2007 7:07 pm ]
Post subject:  Re: java user input

well, they both worked, and i found that scanner is easier to use
but what is the difference between the two input? ( BufferedReader and scanner)
which one is better? why use them?
sorry i know i got a lot of questions, but it would be nice if soeone explain them to me Embarassed


: