-
How do I read ints into an array from cmd line?
I'm writing a program where I prompt the user to enter a string of numbers. Then I use the StringTokenizer to break up the string and enter the tokens into a String[]. However, I need the numbers to be seen as ints so I can compare the elements in the array.
I've managed to: 1) prompt the user. 2) read in the string of numbers. 3) break the string into tokens.
4) enter those tokens into an array. 5) use a for loop as debugging code to print out each element of the array.
The problem is that the elements of the array aren't seen as integers, and therefore, I can't compare their values as such. How can I accomplish this task?
The gist of the whole program is to take the user's input, copy it into an array, duplicate the array, and send the copies to two different recursive methods which then determine which number is the largest in the array. At the same time, I'm tracking the recursive calls of each method to determine which algorithm is the most efficient.
Thanks,
Brandon
-
Take a look at the static method, parseInt( String str ). It will take your strings and convert them to ints.
-gc
-
Hi,
as godcomplex pointed out you have to cast the String tokens into integer.
Why? Take a look at the syntax of main mathod.
public static void main(String arg[]){
//what are you are reading from cmdline is stored as arg[] array which is String while what you need is an integer.
}
Hope this helps.
Pankaj
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks