DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2002

    Question 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.


  2. #2
    Join Date
    Jul 2002
    Take a look at the static method, parseInt( String str ). It will take your strings and convert them to ints.


  3. #3
    Join Date
    Aug 2002
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

By using this site, you agree to the Privacy Policy