Results 1 to 6 of 6
Thread: read string and int from console
- 05-10-2012, 06:59 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 25
- Rep Power
- 0
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.
- 05-10-2012, 07:04 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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)
- 05-10-2012, 07:05 AM #3
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
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 ... }Website: Learn Java by Examples
- 05-10-2012, 07:06 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-10-2012, 07:28 AM #5
Member
- Join Date
- Apr 2012
- Posts
- 25
- Rep Power
- 0
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!
- 05-10-2012, 07:52 AM #6
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Similar Threads
-
BigIntegers syntax.. How to read in from console program
By sonny in forum New To JavaReplies: 4Last Post: 04-14-2010, 04:01 AM -
readint() in Console cannot be apllied to (java.lang.String)
By bige15 in forum New To JavaReplies: 3Last Post: 12-07-2008, 04:24 AM -
How to read input from console
By Java Tip in forum java.ioReplies: 0Last Post: 04-16-2008, 10:57 PM -
How to read input from the console
By Java Tip in forum java.ioReplies: 0Last Post: 04-06-2008, 07:41 PM -
Read from console (Scanner Class)
By hey in forum New To JavaReplies: 10Last Post: 12-11-2007, 10:11 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks