Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By wsaryada

Thread: read string and int from console

  1. #1
    cherrychives is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default read string and int from console

    So I really have no idea how to use the System.in thing..

    How can it be used so that when this is typed into the console:

    java myClass hello 20

    .."hello" and 20 are stored as a string and an int?

    I've tried a number of ways, but I just can't figure out how System.in works.

    Thanks in advance.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: read string and int from console

    Why do you want to use System.in?
    "java myClass hello 20" looks to me more like system/command-line arguments!
    hello and 20 will be passed to the main method (the string array), if you want the 20 as integer, you could use the Integer.parseInt method!
    Please read: Command-Line Arguments (The Java™ Tutorials > Essential Classes > The Platform Environment)

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: read string and int from console

    When you do:

    java MyClass hello 20

    You'll pass the hello and 20 as parameters to your MyClass main() method. Where in method this parameters will be accepted as an array of String. You can see this in the main() method signature of your MyClass.

    Java Code:
    public static void main(String[] args) {
        ...
        String hello = args[0]; // give you "hello"
        String number = args[1]; // give you 20
        ...
    }
    cherrychives likes this.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: read string and int from console

    "hello" and "20" are both command line arguments and they are both Strings and they have nothing to do with the System.in object. Read the tutorials on IO for input streams and friends.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    cherrychives is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: read string and int from console

    Our lecturer was telling us about System.in some time ago so I thought it would apply here as well. Didn't know about using args.

    Thanks guys!

  6. #6
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: read string and int from console

    Quote Originally Posted by cherrychives View Post
    Our lecturer was telling us about System.in some time ago so I thought it would apply here as well. Didn't know about using args.

    Thanks guys!
    You would use System.in for interactive input mainly. Read up on it as recommended above.

Similar Threads

  1. Replies: 4
    Last Post: 04-14-2010, 04:01 AM
  2. Replies: 3
    Last Post: 12-07-2008, 04:24 AM
  3. How to read input from console
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-16-2008, 10:57 PM
  4. How to read input from the console
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-06-2008, 07:41 PM
  5. Read from console (Scanner Class)
    By hey in forum New To Java
    Replies: 10
    Last Post: 12-11-2007, 10:11 PM

Tags for this Thread

Posting Permissions

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