for循环只读取最后一个值

时间:2015-02-08 18:35:24

标签: java string for-loop

我的程序中存在一个错误,它使我的循环只读取输入字符串的最后一部分。我希望程序计算字符串中每个字符的数量。我不希望答案告诉我如何更有效地存储这些值,而不是重复所有26次。请回答我在此处陈述的问题。对不起,如果这是重复的!我只是不知道要指定什么,因为没有明显的错误。

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();
    }
}

4 个答案:

答案 0 :(得分:1)

substring方法将从指定的索引返回一个新字符串到字符串的结尾。你想要的是String.charAt(index),它将返回指定位置的char

答案 1 :(得分:1)

substring将字符串中从给定索引返回到它的结尾。由于长于单个字符的字符串永远不能等于单个字符,因此满足任何条件的唯一位置是最后一个字符。您可以使用substring(i),或者更好,substring(i, i+1),而不是使用charAt(i) - 只需注意它返回char原语,而不是java.lang.String对象。< / p>

话虽如此,整个构造有点麻烦。使用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);
    }
}

或者更优化的表单可以使用一个数组,其中第一个元素表示'a'的数量,第二个元素表示'b'的数量等:

// 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]++;
    }
}

答案 2 :(得分:0)

 randaf = strs.substring(i);

将此行更改为

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

将for循环更改为i<strs.length()并且不等于。

如果你想缩短程序,可以这样做。

int[] counts=new int[26];

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

假设你只想数一个&#39;到&#39; z&#39;

答案 3 :(得分:-1)

好的,所以我在发布之后就把它弄好了。不过,谢谢你的帮助!我只是改变了substring的范围来读取一个字符,而不是之后的所有字符。

我仍然没有解决我的长代码问题,但是嘿,它有效。

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();
    }
}