Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

input from keyboard

mondenkind11mondenkind11 Posts: 22Member

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 Posts: 33Member
    :
    : 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 Posts: 22Member

    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 Posts: 33Member
    :
    : 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 Posts: 684Member
    : :
    : : 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 Posts: 22Member
    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 Posts: 684Member
    : 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 Posts: 33Member
    [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 Posts: 22Member

    thank you very much for your help.

Sign In or Register to comment.