Results 1 to 4 of 4
  1. #1
    hayjk is offline Member
    Join Date
    Mar 2008
    Location
    Greenville, SC
    Posts
    2
    Rep Power
    0

    Default [SOLVED] Problems with arrays

    I am new to Java and I am having an issue with an array problem. I need to check the first digit in a String array and if it has a value of 0 or 9, I need to change it to a 1. Below is my code that is trying to do this. What I am having trouble with is populating the new array. Any help would be greatly appreciated.
    Java Code:
    for (int i = 0; i < nums.length; i++)
    {
    prinum[i] = nums[i].substring(5);
    char[] temp = prinum[i].toCharArray();
    for (int x = 0; x < prinum[i].length(); x++)
    {
    if (prinum[i].charAt(x) == '9')
    {
    temp[x] = 1;
    }
    else if (prinum[i].charAt(x) == '0')
    {
    temp[x] = 1;
    }
    }
    String newprinum[i] = new String(temp);
    Last edited by DonCash; 04-07-2008 at 05:49 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    for (int i = 0; i < nums.length; i++)
    {
        prinum[i] = nums[i].substring(5);
        // if the first character is a
        // digit [0 - 9] change it to a "1".
        // This loop will examine every letter
        // in the string "prinum[i]"
        char[] temp = prinum[i].toCharArray();
        for (int x = 0; x < temp.length; x++)
        {
            char c = temp[x];
            if (c == '9' || c == '8' || c == '7' ...)
            {
                temp[x] = 1;
            }
        }
        // Assign the corrected value of temp[x]
        // to "newprinum[i]"
        newprinum[i] = temp[x];
    Java Code:
        // Another possible way to look at it:
        String digits = "0123456789";
        ...
        prinum[i] = nums[i].substring(5);
        String first = prinum[i].substring(0, 1);
        // Is the first letter a digit?
        if(digits.indexOf(first) != -1) {
            // change it to a "1"
            prinum[i] = "1" + prinum[1].substring(1);
        }

  3. #3
    hayjk is offline Member
    Join Date
    Mar 2008
    Location
    Greenville, SC
    Posts
    2
    Rep Power
    0

    Default

    Thanks hardwired. I found an easier way to do it. Here is the code snippet i used.
    Java Code:
    for (int i = 0; i < nums.length; i++)
    {
    prinum[i] = nums[i].substring(6);
    firstnum[i] = nums[i].substring(5, 6);
    firstnum[i] = firstnum[i].replaceAll("9", "1");
    firstnum[i] = firstnum[i].replaceAll("0", "1");
    I appreciate your help.
    Last edited by DonCash; 04-07-2008 at 05:51 PM.

  4. #4
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    I edited to put the [CODE] [/ code] tags round the code to make it easier to read.
    Did this post help you? Please me! :cool:

Similar Threads

  1. Some problems with arrays and loops
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 12:24 PM
  2. new to arrays
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 02:45 PM
  3. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 10:08 PM
  4. arrays help
    By Warren in forum New To Java
    Replies: 6
    Last Post: 11-23-2007, 07:23 PM
  5. Problems with arrays
    By Marcus in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:10 AM

Posting Permissions

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