Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

input from keyboard

mondenkind11mondenkind11 Member Posts: 22

Hi
Can anyone help me write an instruction that allows the user to enter an integer from the keyboard? I would like the integer to be read from the keyboard. I am looking for the simplest way possible. I am using netbeans. I would appreciate any help.
Thank you.

Comments

  • knbelknbel Member Posts: 33
    :
    : Hi
    : Can anyone help me write an instruction that allows the user to enter an integer from the keyboard? I would like the integer to be read from the keyboard. I am looking for the simplest way possible. I am using netbeans. I would appreciate any help.
    : Thank you.
    :
    Try this:
    [code]
    import java.util.Scanner;
    ...
    Scanner scin = new Scanner(System.in);
    int in = scin.nextInt();
    [/code]
  • mondenkind11mondenkind11 Member Posts: 22

    Thank you for your reply. I did what you suggested. Why do I get the message: Cannot resolve symbol(symbol Scanner, class util)?

    >>>>
    Try this:
    [code]
    import java.util.Scanner;
    ...
    Scanner scin = new Scanner(System.in);
    int in = scin.nextInt();
    [/code]


  • knbelknbel Member Posts: 33
    :
    : Thank you for your reply. I did what you suggested. Why do I get the message: Cannot resolve symbol(symbol Scanner, class util)?
    :
    : >>>>
    : Try this:
    : [code]
    : import java.util.Scanner;
    : ...
    : Scanner scin = new Scanner(System.in);
    : int in = scin.nextInt();
    : [/code]
    :
    :
    :
    I'm sorry. The Scanner class is only avalable in Java 5. I didn't even think of that. The method of reading input from the keyboard in ealier versions is quite a bit more complicated. You would do this for versions earlier then Java 5:
    [code]
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    ...
    int temp = 0;
    try
    {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    temp = Integer.parseInt(in.readLine());
    }
    catch(IOException e)
    {
    System.out.println("error");
    }
    [/code]
    I'm sorry that this version is not so clean.
    Feel free the write back if you have any more questons.
  • VilanyeVilanye Member Posts: 684
    : :
    : : Thank you for your reply. I did what you suggested. Why do I get the message: Cannot resolve symbol(symbol Scanner, class util)?
    : :
    : : >>>>
    : : Try this:
    : : [code]
    : : import java.util.Scanner;
    : : ...
    : : Scanner scin = new Scanner(System.in);
    : : int in = scin.nextInt();
    : : [/code]
    : :
    : :
    : :
    : I'm sorry. The Scanner class is only avalable in Java 5. I didn't even think of that. The method of reading input from the keyboard in ealier versions is quite a bit more complicated. You would do this for versions earlier then Java 5:
    : [code]
    : import java.io.BufferedReader;
    : import java.io.InputStreamReader;
    : import java.io.IOException;
    : ...
    : int temp = 0;
    : try
    : {
    : BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    : temp = Integer.parseInt(in.readLine());
    : }
    : catch(IOException e)
    : {
    : System.out.println("error");
    : }
    : [/code]
    : I'm sorry that this version is not so clean.
    : Feel free the write back if you have any more questons.
    :

    Did you import java.util.Scanner?

    The Scanner class is simple, which is nice, but the the older IO classes are a bit more powerful IMO.


    [italic][blue]Just my 2 bits[/blue][italic]

  • mondenkind11mondenkind11 Member Posts: 22
    Thank you very much. I appreciate your help.
    It is working now.
    How could I do the same thing(read from the keyboard) if I had a text instead? For example if I wanted the input "this is a text" read from the keyboard, how should I do that?
    I read the other post that has to do with string input, though I am having difficulties with
    [code]
    temp = Integer.parseInt(in.readLine());
    [/code]
    which is for integers. How would it look like for strings?
    Thank you again.

    >>>>>>
    I'm sorry. The Scanner class is only avalable in Java 5. I didn't even think of that. The method of reading input from the keyboard in ealier versions is quite a bit more complicated. You would do this for versions earlier then Java 5:
    [code]
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    ...
    int temp = 0;
    try
    {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    temp = Integer.parseInt(in.readLine());
    }
    catch(IOException e)
    {
    System.out.println("error");
    }
    [/code]
    I'm sorry that this version is not so clean.
    Feel free the write back if you have any more questons.


    Did you import java.util.Scanner?

    The Scanner class is simple, which is nice, but the the older IO classes are a bit more powerful IMO.





  • VilanyeVilanye Member Posts: 684
    : Thank you very much. I appreciate your help.
    : It is working now.
    : How could I do the same thing(read from the keyboard) if I had a text instead? For example if I wanted the input "this is a text" read from the keyboard, how should I do that?
    : I read the other post that has to do with string input, though I am having difficulties with
    : [code]
    : temp = Integer.parseInt(in.readLine());
    : [/code]
    : which is for integers. How would it look like for strings?
    : Thank you again.

    readLine() returns a string. If you need the string you need to omit the Integer.parseInt method call.

    Before using any class it is a good idea to read the API docs to get an understanding of what it can do, and what methods are available.
    [italic][blue]Just my 2 bits[/blue][italic]

  • knbelknbel Member Posts: 33
    [code]
    String str = in.readLine();
    [/code]

    : : Thank you very much. I appreciate your help.
    : : It is working now.
    : : How could I do the same thing(read from the keyboard) if I had a text instead? For example if I wanted the input "this is a text" read from the keyboard, how should I do that?
    : : I read the other post that has to do with string input, though I am having difficulties with
    : : [code]
    : : temp = Integer.parseInt(in.readLine());
    : : [/code]
    : : which is for integers. How would it look like for strings?
    : : Thank you again.
    :
    : readLine() returns a string. If you need the string you need to omit the Integer.parseInt method call.
    :
    : Before using any class it is a good idea to read the API docs to get an understanding of what it can do, and what methods are available.
    : [italic][blue]Just my 2 bits[/blue][italic]
    :
    :
  • mondenkind11mondenkind11 Member Posts: 22

    thank you very much for your help.

Sign In or Register to comment.