1

There's a bug in my program that makes my loop read only the last part of the inputted string. I want the program to count how many of each character is in a string. I do not want answers telling me how to more efficiently store these values, without repeating everything 26 times. Please answer the question I have stated here instead. Sorry if this is a duplicate! I just don't know what to specify since there is no apparent error.

import java.util.Scanner;

public class stringprogram {
    public static void stringinputmethod()
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a String");
        String strs = scan.nextLine();

        int numa = 0;
        int numb = 0;
        int numc = 0;
        int numd = 0;
        int nume = 0;
        int numf = 0;
        int numg = 0;
        int numh = 0;
        int numi = 0;
        int numj = 0;
        int numk = 0;
        int numl = 0;
        int numm = 0;
        int numn = 0;
        int numo = 0;
        int nump = 0;
        int numq = 0;
        int numr = 0;
        int nums = 0;
        int numt = 0;
        int numu = 0;
        int numv = 0;
        int numw = 0;
        int numx = 0;
        int numy = 0;
        int numz = 0;

        String randaf;

        for(int i=0; i<=strs.length();i++)
        {
            randaf = strs.substring(i);

            if(randaf.equals("a"))
            {
                numa = numa + 1;
            }
            else if(randaf.equals("b"))
            {
                numb = numb + 1;
            }
            else if(randaf.equals("c"))
            {
                numc = numc + 1;
            }
            else if(randaf.equals("d"))
            {
                numd = numd + 1;
            }
            else if(randaf.equals("e"))
            {
                nume = nume + 1;
            }
            else if(randaf.equals("f"))
            {
                numf = numf + 1;
            }
            else if(randaf.equals("g"))
            {
                numg = numg + 1;
            }
            else if(randaf.equals("h"))
            {
                numh = numh + 1;
            }
            else if(randaf.equals("i"))
            {
                numi = numi + 1;
            }
            else if(randaf.equals("j"))
            {
                numj = numj + 1;
            }
            else if(randaf.equals("k"))
            {
                numk = numk + 1;
            }
            else if(randaf.equals("l"))
            {
                numl = numl + 1;
            }
            else if(randaf.equals("m"))
            {
                numm = numm + 1;
            }
            else if(randaf.equals("n"))
            {
                numn = numn + 1;
            }
            else if(randaf.equals("o"))
            {
                numo = numo + 1;
            }
            else if(randaf.equals("p"))
            {
                nump = nump + 1;
            }
            else if(randaf.equals("q"))
            {
                numq = numq + 1;
            }
            else if(randaf.equals("r"))
            {
                numr = numr + 1;
            }
            else if(randaf.equals("s"))
            {
                nums = nums + 1;
            }
            else if(randaf.equals("t"))
            {
                numt = numt + 1;
            }
            else if(randaf.equals("u"))
            {
                numu = numu + 1;
            }
            else if(randaf.equals("v"))
            {
                numv = numv + 1;
            }
            else if(randaf.equals("w"))
            {
                numw = numw + 1;
            }
            else if(randaf.equals("x"))
            {
                numx = numx + 1;
            }
            else if(randaf.equals("y"))
            {
                numy = numy + 1;
            }
            else if(randaf.equals("z"))
            {
                numz = numz + 1;
            }
        }
        System.out.println("a: "+numa +"\nb: "+ numb +"\nc: "+ numc +"\nd: "+ numd +"\ne: "+ nume +"\nf: "+ numf +"\ng: "+ numg +"\nh: "+ numh +"\ni: "+ numi +"\nj: "+ numj +"\nk: "+ numk +"\nl: "+ numl +"\nm: "+ numm +"\nn: "+ numn +"\no: "+ numo +"\np: "+ nump +"\nq: "+ numq +"\nr: "+ numr +"\ns: "+ nums +"\nt: "+ numt +"\nu: "+ numu +"\nv: "+ numv +"\nw: "+ numw +"\nx: "+ numx +"\ny: "+ numy +"\nz: "+ numz);
    }

    public static void main(String[] args)
    {
        stringinputmethod();
    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Young Deezie
  • 145
  • 2
  • 12

4 Answers4

1

The substring method will return a new string from the index specified to the end of the string. What you want is String.charAt(index) which will return the char at the specified location

Moddl
  • 360
  • 3
  • 13
1

substring returns the part of the string from the given index to it's end. Since a string longer than a single character can never be equal to a single character, the only place where any of the conditions are met is the last character. Instead of using substring(i) you could use substring(i, i+1), or better yet, charAt(i) - just note that it returns a char primitive, not a java.lang.String object.

Having said that, this entire construct is a bit cumbersome. It would be much easier to use a Map<Character,Integer>:

// Initialization:
Map<Character,Integer> map = new HashMap<>();
for (char c = 'a'; c <= 'z'; ++c) {
    map.put (c, 0);
}

// Going over the string:
for(char c : strs.toCharArray()) {
    Integer i = map.get(c);
    if (i != 0) {
        map.put (c, i + 1);
    }
}

Or an even more optimized form could use an array where the first element indicates the number of 'a's, the second the number of 'b's, etc.:

// Initialization:
int[] chars = new int[26];
for (i = 0; i < chars.length; ++i) {
    chars[i] = 0;
}

// Going over the string:
for(char c : strs.toCharArray()) {
    int i = c - 'a';
    if (i >= 0 && i < chars.length) {
        chars[i]++;
    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0
 randaf = strs.substring(i);

Change this line to

randaf = strs.substring(i,i+1);

And change the for loop to i<strs.length() and not equal to.

If you want to shorten the program you can do this.

int[] counts=new int[26];

for(int i=0;i<strs.length();i++)
counts[strs.charAt(i)-'a']++;

Assuming you only want to count 'a' to 'z'

Anil
  • 578
  • 4
  • 12
-1

Okay, so I got it right after I posted this. Thanks for helping anyways, though! I just changed the range of my substring to read one character, not all the ones after it as well.

I still did not fix the issue with my long code, but hey, it works.

import java.util.Scanner;

public class stringprogram {
    public static void stringinputmethod()
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a String");
        String strs = scan.nextLine();

        int numa = 0;
        int numb = 0;
        int numc = 0;
        int numd = 0;
        int nume = 0;
        int numf = 0;
        int numg = 0;
        int numh = 0;
        int numi = 0;
        int numj = 0;
        int numk = 0;
        int numl = 0;
        int numm = 0;
        int numn = 0;
        int numo = 0;
        int nump = 0;
        int numq = 0;
        int numr = 0;
        int nums = 0;
        int numt = 0;
        int numu = 0;
        int numv = 0;
        int numw = 0;
        int numx = 0;
        int numy = 0;
        int numz = 0;

        String randaf;

        for(int i=0; i<=strs.length()-1;i++)
        {
            randaf = strs.substring(i, i+1);

            if(randaf.equals("a"))
            {
                numa = numa + 1;
            }
            else if(randaf.equals("b"))
            {
                numb = numb + 1;
            }
            else if(randaf.equals("c"))
            {
                numc = numc + 1;
            }
            else if(randaf.equals("d"))
            {
                numd = numd + 1;
            }
            else if(randaf.equals("e"))
            {
                nume = nume + 1;
            }
            else if(randaf.equals("f"))
            {
                numf = numf + 1;
            }
            else if(randaf.equals("g"))
            {
                numg = numg + 1;
            }
            else if(randaf.equals("h"))
            {
                numh = numh + 1;
            }
            else if(randaf.equals("i"))
            {
                numi = numi + 1;
            }
            else if(randaf.equals("j"))
            {
                numj = numj + 1;
            }
            else if(randaf.equals("k"))
            {
                numk = numk + 1;
            }
            else if(randaf.equals("l"))
            {
                numl = numl + 1;
            }
            else if(randaf.equals("m"))
            {
                numm = numm + 1;
            }
            else if(randaf.equals("n"))
            {
                numn = numn + 1;
            }
            else if(randaf.equals("o"))
            {
                numo = numo + 1;
            }
            else if(randaf.equals("p"))
            {
                nump = nump + 1;
            }
            else if(randaf.equals("q"))
            {
                numq = numq + 1;
            }
            else if(randaf.equals("r"))
            {
                numr = numr + 1;
            }
            else if(randaf.equals("s"))
            {
                nums = nums + 1;
            }
            else if(randaf.equals("t"))
            {
                numt = numt + 1;
            }
            else if(randaf.equals("u"))
            {
                numu = numu + 1;
            }
            else if(randaf.equals("v"))
            {
                numv = numv + 1;
            }
            else if(randaf.equals("w"))
            {
                numw = numw + 1;
            }
            else if(randaf.equals("x"))
            {
                numx = numx + 1;
            }
            else if(randaf.equals("y"))
            {
                numy = numy + 1;
            }
            else if(randaf.equals("z"))
            {
                numz = numz + 1;
            }
        }
        System.out.println("a: "+numa +"\nb: "+ numb +"\nc: "+ numc +"\nd: "+ numd +"\ne: "+ nume +"\nf: "+ numf +"\ng: "+ numg +"\nh: "+ numh +"\ni: "+ numi +"\nj: "+ numj +"\nk: "+ numk +"\nl: "+ numl +"\nm: "+ numm +"\nn: "+ numn +"\no: "+ numo +"\np: "+ nump +"\nq: "+ numq +"\nr: "+ numr +"\ns: "+ nums +"\nt: "+ numt +"\nu: "+ numu +"\nv: "+ numv +"\nw: "+ numw +"\nx: "+ numx +"\ny: "+ numy +"\nz: "+ numz);
    }

    public static void main(String[] args)
    {
        stringinputmethod();
    }
}
Young Deezie
  • 145
  • 2
  • 12