Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Parsing many Ints from a user input String.

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Parsing many Ints from a user input String.

    I'm writing a java program that takes an input from a user as a string. Then has to take that random string and parse all of the ints in order and put them into an array. I have to have a method with the only input as the string that returns the int array, which is where I'm having trouble.


    In the method I have a string array set up first to the length of the array. And I have a counter. I have a for loop that has (int a = 0; a <= string.length; a++) I check if charAt(a) = a number, if so I make stringArray[count] = stringArray[count]+charAt(a), if not i check if a = 0, and if so do nothing, last I check if the charAt(a-1) is a number, if so I increase the counter. After I have the string array completed I make my int array with length of my counter then I have a for loop one more time that has (int b = 0; b <= cnt; b++) and for intArray[b] I parseInt StringArray[b]. what am I doing wrong it keeps giving me errors like stringindex out of bounds or numberformat exception.


    Here is my method code;


    public static int[] intParse(String s)
    {

    String[] placeKeeper = new String[s.length()];

    for (int a = 0; a < s.length(); a++)
    {
    placeKeeper[a] = "";
    }


    int cnt = 0;
    for (int a = 0; a <= s.length(); a++)
    {
    if (s.charAt(a) == '1' || s.charAt(a) == '2' || s.charAt(a) == '3' || s.charAt(a) == '4' || s.charAt(a) == '5' || s.charAt(a) == '6' || s.charAt(a) == '7' || s.charAt(a) == '8' || s.charAt(a) == '9' || s.charAt(a) == '0')
    {
    placeKeeper[cnt] = placeKeeper[cnt] + s.charAt(a);
    }

    else if (a == 0)
    {
    }
    else if (s.charAt(a-1) == '1' || s.charAt(a-1) == '2' || s.charAt(a-1) == '3' || s.charAt(a-1) == '4' || s.charAt(a-1) == '5' || s.charAt(a-1) == '6' || s.charAt(a-1) == '7' || s.charAt(a-1) == '8' || s.charAt(a-1) == '9' || s.charAt(a-1) == '0')

    {
    cnt ++;
    }


    } //end for loop


    int[] array = new int[cnt];
    for (int b = 0; b <= cnt; b++)
    {
    array[b] = Integer.parseInt(placeKeeper[b]);
    }


    return array;
    }


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Parsing many Ints from a user input String.

    I'm not quite sure what your goal is, the original goal seems to just turn a string of numbers into an array of ints. The code you provided looks like you are counting frequency.

    I'd suggest you look at using split and integer.parseInt to trivialize the process.

Similar Threads

  1. Conversion from multiple ints to single string
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 12th, 2011, 10:08 PM
  2. JTable Updating String Values from User Input
    By aussiemcgr in forum AWT / Java Swing
    Replies: 5
    Last Post: August 3rd, 2010, 01:48 PM
  3. adding multiple ints into a string
    By straw in forum Java Theory & Questions
    Replies: 1
    Last Post: March 18th, 2010, 06:02 PM
  4. Parsing input based on grammar rules
    By Winterfresh in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 11th, 2010, 07:54 PM
  5. Input file parsing to insert into DB
    By IDForums in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 30th, 2009, 02:29 AM