Page 1 of 5 123 ... LastLast
Results 1 to 10 of 42

Thread: how to read input in java?

  1. #1
    Join Date
    Sep 2008
    Location
    Bangalore,India
    Beans
    982
    Distro
    Ubuntu 10.04 Lucid Lynx

    how to read input in java?

    so if we want read input from user,like i want to read integers from user,
    alternate for scanf/raw_input in java??howto

    public class jp3
    {
    public static void main(string args[])
    {
    int a,b,choice;

    system.out.println("enter the any one of following choices\n");
    system.out.println("1:add\n2:subtraction\n3:multip ly\n");
    ..................
    Abhilash Muthuraj-views
    I always believe that Linux,
    "In a world of no walls or fences, we don't need WINDOWS or GATES"
    New to Ubuntu??

  2. #2
    Join Date
    Jun 2008
    Location
    Narnia
    Beans
    784
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: how to read input in java?

    Quote Originally Posted by abhilashm86 View Post
    so if we want read input from user,like i want to read integers from user,
    alternate for scanf/raw_input in java??howto
    Code:
    public class jp3
    {
    public static void main(string args[])
    {
    int a,b,choice;
    Scanner scan = new Scanner(System.in);
    
    system.out.println("enter the any one of following choices\n");
    system.out.print("1:add\n2:subtraction\n3:multiply:");
    int userInput = scan.nextInt();
    ..................
    Read the API on the Scanner class in general (for String input and such)...
    Blog
    #!
    Chan eil mi a Gàidhlig agam...pero si hablo español: soy catracho.
    Proud to be a Browncoat!

  3. #3
    Join Date
    May 2005
    Beans
    274

    Re: how to read input in java?


  4. #4
    Join Date
    Jul 2008
    Beans
    1,706

    Re: how to read input in java?

    imo java fails at reading input

  5. #5
    Join Date
    Sep 2008
    Location
    Bangalore,India
    Beans
    982
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: how to read input in java?

    public class jp3
    {
    public static void main (String args[])
    {
    int a,b,choice;
    // Scanner scan=new Scanner(System.in);

    System.out.println("enter the any one of following choices\n");
    System.out.println("2:add\n2:subtraction\n3:multip ly\n");

    choice=System.in.read();

    System.out.println(+choice);
    }
    }
    so following error was given,

    abhilash@abhi:~$ javac jp3.java
    ----------
    1. ERROR in jp3.java (at line 11)
    choice=System.in.read();
    ^^^^^^^^^^^^^^^^
    Unhandled exception type IOException
    ----------
    1 problem (1 error)

    how to check version of java being currently used??
    java -v din't work
    Abhilash Muthuraj-views
    I always believe that Linux,
    "In a world of no walls or fences, we don't need WINDOWS or GATES"
    New to Ubuntu??

  6. #6
    Join Date
    Feb 2009
    Beans
    789
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: how to read input in java?

    Code:
    public static void main(String[] args) {
    		try {
    			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    			String line = in.readLine();
    			
    			System.out.println("You just entered:  " + line);
    			
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Beans
    2,185
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: how to read input in java?

    Quote Originally Posted by jimi_hendrix View Post
    imo java fails at reading input
    What do you mean by this?
    Ubuntu 12.04

  8. #8
    Join Date
    Apr 2006
    Location
    Hamilton, New Zealand
    Beans
    198
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: how to read input in java?

    Quote Originally Posted by jimi_hendrix View Post
    imo java fails at reading input
    +1 - The amount of cruft you have to add to read command line input in java is just crazy.

  9. #9
    Join Date
    Jul 2008
    Beans
    1,706

    Re: how to read input in java?

    Quote Originally Posted by jespdj View Post
    What do you mean by this?
    python: str = raw_input("input please");

    C#: str = Console.ReadLine();

    C++: cin.getline(str, 256);

    C: str = gets(); /*depricated i know but i am too lazy to google the format for fgets*/

    Erlang: str = io:get_line("input please");

    java requires instantiation and other stuff...must i go on?

  10. #10
    Join Date
    Jun 2008
    Location
    Narnia
    Beans
    784
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: how to read input in java?

    Quote Originally Posted by jimi_hendrix View Post
    python: str = raw_input("input please");

    C#: str = Console.ReadLine();

    C++: cin.getline(str, 256);

    C: str = gets(); /*depracated i know but i am too lazy to google the format for fgets*/

    Erlang: str = io:get_line("input please");

    java requires instantiation and other stuff...must i go on?
    When I first learned about Scanner I had one of the biggest wtf moments of my programming life. Never mind about the difference between scan.next(), scan.nextLine(), scan.nextInt(), etc.
    Python wins the prize, as usual, raw_input() gives you a string, and you can just wrap it up in an int() function (or _insert-datatype-here_ function) if you want something else.
    Blog
    #!
    Chan eil mi a Gàidhlig agam...pero si hablo español: soy catracho.
    Proud to be a Browncoat!

Page 1 of 5 123 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •