Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to include 0s of substrings without ArrayList and array?

I finished a program where I use Math.random() to pick a number 25-50, both inclusive. This will be passed to stringAdd() as length. The method creates a string with the randomized length from numbers 0-9 (also inclusive) using Math.random() as well.

Then, if the length is above 30, "group" the string by 3 digits, otherwise, just "group" by 2 digits. Uneven grouping is allowed at the end (see the sample output I included below).

Last, the program must loop through the string, change all group of numbers to int and then add the sum to a total, so the total can be returned.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is the code I have:

public class Grouping
{
    public static int stringAdd(int length)
    {
        ArrayList<String> numbers = new ArrayList<String>();
        String randomString = "";
        for(int i = 0; i < length; i++)
    {
        int randomNumbers = (int)(Math.random()*10);
        randomString = randomString + randomNumbers;
    }
    System.out.println("Generated string: " + randomString);
        
    int group = 2;
    if(length > 30)
        group = 3;
        
    for(int i = 0; i < length; i = i + group)
    {
        numbers.add(randomString.substring(i, Math.min(length, i + group)));
            System.out.println("Group: " + randomString.substring(i, Math.min(length, i + group)));
    }
        
        int[] array = new int[numbers.size()];
        int total = 0;
        
    for(int i = 0; i < numbers.size(); i++)
    {
        array[i] = Integer.parseInt(numbers.get(i));
            total = total + array[i];
    }
    return total;
    }

    public static void main(String[] args)
    {
    int randomLength = (int)(Math.random() * (50 - 25) + 1) + 25;
    System.out.println("Length of the string is " + randomLength);
    System.out.println("Total: " + stringAdd(randomLength));
    }
}

The output is this:

Length of the string is 30
Generated string: 157768294302045453441313535692
Group: 15
Group: 77
Group: 68
Group: 29
Group: 43
Group: 02
Group: 04
Group: 54
Group: 53
Group: 44
Group: 13
Group: 13
Group: 53
Group: 56
Group: 92
Total: 616

However, I wanted to simplify this and remove the use of ArrayList and array so that I only need two for loops instead of three. So I did something similar that I already have:

int total = 0;
for(int i = 0; i < length; i = i + group)
{
   int digits = Integer.parseInt(randomString.substring(i, Math.min(length, i + group)));
   System.out.println("Group: " + digits);
   total = total + digits;
}
return total; 

And one output is this, which looks fine…

Length of the string is 47
Generated string: 26011336670044862237923585625878411711193186128
Group: 260
Group: 113
Group: 366
Group: 700
Group: 448
Group: 622
Group: 379
Group: 235
Group: 856
Group: 258
Group: 784
Group: 117
Group: 111
Group: 931
Group: 861
Group: 28
Total: 7069

But another output would be this:

Length of the string is 28
Generated string: 7581863393306645423505929903
Group: 75
Group: 81
Group: 86
Group: 33
Group: 93
Group: 30
Group: 66
Group: 45
Group: 42
Group: 35
Group: 5
Group: 92
Group: 99
Group: 3
Total: 785

5 should actually be 05 and 3 should actually be 03. I understand that the "zero" in 03 does not add to the total, but I strictly want my program to group by the digits. I also am working under constraints so unfortunately I cannot use anything advanced yet

>Solution :

Numbers don’t have leading zeros. 3 is 03 is 003. If you require leading zeros, keep a string and only convert to a number when summing:

for(int i = 0; i < length; i = i + group)
{
   final String substring = randomString.substring(i, Math.min(length, i + group));
   System.out.println("Group: " + substring);
   int digits = Integer.parseInt(substring);
   total = total + digits;
}

or

for(int i = 0; i < length; i += group)
{
   final String digits = randomString.substring(i, Math.min(length, i + group));
   System.out.println("Group: " + digits);
   total += Integer.parseInt(digits);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading