Java 如何使arrayList只读取字符串而不读取int?

Java 如何使arrayList只读取字符串而不读取int?,java,Java,编写一个名为countWords的方法,该方法接受字符串的ArrayList作为参数和 打印以“A”或“A”开头的字数(即字符串),并在一行打印所有长度超过5个字符的字数 我的解决办法是 int count=0; String[] st=null; Scanner input=new Scanner(System.in); ArrayList<String> array = new ArrayList<String>(); System.

编写一个名为countWords的方法,该方法接受字符串的ArrayList作为参数和 打印以“A”或“A”开头的字数(即字符串),并在一行打印所有长度超过5个字符的字数

我的解决办法是

int count=0;
    String[] st=null;

    Scanner input=new Scanner(System.in);
    ArrayList<String> array = new ArrayList<String>();
    System.out.println("please input something");

while(input.hasNext()) {
        String st1=input.next();
        array.add(st1);

    }


    for(int i=0; i<array.size();i++) {
        if(array.get(i).startsWith("a")||array.get(i).startsWith("A")) {
            count++;
        }
    }
        for(int j=0; j<array.size(); j++) {
            if(array.get(j).length()>5) 
                st[j]=array.get(j);

        }

        System.out.println(count);
        System.out.println(st);
}
int count=0;
字符串[]st=null;
扫描仪输入=新扫描仪(System.in);
ArrayList数组=新的ArrayList();
System.out.println(“请输入一些东西”);
while(input.hasNext()){
字符串st1=input.next();
add(st1);
}

对于(inti=0;i,正如你问题的最后一行所说

但是在字符串中输入将没有尽头

这是因为您没有提供任何方法来结束while循环

while(input.hasNext())
将永远运行并一直等待下一个用户输入。输入完成后,您必须
中断
while循环

之后

如问题所述
“打印以A或A开头的字数,并在一行打印所有长度超过5个字符的字数。”

为此,您可以在
ArrayList
中循环并检查

if(array.get(i).startsWith("A") || array.get(i).startsWith("a")) count++;

if(array.get(i).length()>5) System.out.print(array.get(i)+" ");
并在循环后打印一个或一个事件的编号

System.out.println("\n Number of word with A or a:"+count);
下面是代码的一个工作实现

public static void main(String[] args) {
    int count=0;
    String[] st=null;
    Scanner input=new Scanner(System.in);
    ArrayList<String> array = new ArrayList<String>();
    System.out.println("please input something");
    //System.out.println(input.hasNext());
while(input.hasNext()) {
        String st1=input.next();
        //System.out.println((int) st1.charAt(0));
        if(st1.equals("exit")) break;
        array.add(st1);

    }


    for(int i=0; i<array.size();i++) {
        if(array.get(i).startsWith("A") || array.get(i).startsWith("a")){
            count++;
        }
        if(array.get(i).length()>5) {
            System.out.print(array.get(i)+" ");
        }

    }
    System.out.println("\nNumber of word with A or a:"+count);
}
publicstaticvoidmain(字符串[]args){
整数计数=0;
字符串[]st=null;
扫描仪输入=新扫描仪(System.in);
ArrayList数组=新的ArrayList();
System.out.println(“请输入一些东西”);
//System.out.println(input.hasNext());
while(input.hasNext()){
字符串st1=input.next();
//System.out.println((int)st1.charAt(0));
如果(st1.等于(“退出”))中断;
add(st1);
}
对于(int i=0;i5){
System.out.print(array.get(i)+“”);
}
}
System.out.println(“\n带A或A的单词数:“+count”);
}

要结束循环,必须键入exit。
以下是解决问题的方法

import java.util.Arrays;
import java.util.List;

public class Test {

    public static void main(String... args){
        // Sample String sentence 
        String sentence = "This is the sentence with 5 words starting with 
             all like allwords alltogether also Allnotout allother and allofus.";

        // Splitting above sentence to get each word separately and storing them into List
        List<String> strings = Arrays.asList(sentence.split("\\s+"));
         // calling a method named countWord() as per your assignment question.
        Test.countWords(strings);
    }


    // implementing that method
    static void countWords(List<String> input){
        long count = input.stream().filter(word -> word.startsWith("all") || word.startsWith("All")).count();
        System.out.print("Total words starting with all/All are : "+ count +"\t");
        input.stream().filter(word -> word.length() > 5).forEach( word -> System.out.print(word + "\t"));

    }

}
导入java.util.array;
导入java.util.List;
公开课考试{
公共静态void main(字符串…参数){
//例句
String句子=“这是一个以开头的5个单词的句子
所有的词都是一样的,所有的词都是一样的;
//将上面的句子分开,分别得到每个单词,并将它们存储到列表中
列表字符串=数组.asList(句子.split(\\s+));
//根据赋值问题调用名为countWord()的方法。
测试。对单词(字符串)进行计数;
}
//实施该方法
静态void countWords(列表输入){
long count=input.stream().filter(word->word.startsWith(“all”)| word.startsWith(“all”).count();
System.out.print(“以all/all开头的总字数为:+count+”\t”);
input.stream().filter(word->word.length()>5).forEach(word->System.out.print(word+“\t”);
}
}

ArrayList
?可能与您已经完成的操作重复?该语句以“编写一个名为countWords的方法,该方法接受字符串的ArrayList作为参数”开头。你还没有写过这样的方法。如果你只需按enter键而不输入,输入循环就会结束。非常感谢你,我已经在eclipse上尝试过了,这对我这样的新手非常有帮助!如果你的问题得到解决,你也应该支持其他人,如果你感到满意,你必须将问题标记为已解决,并投票支持答案:)谢谢你的回复,非常欢迎。。!!我想你已经找到了解决办法。。但我建议,如果您使用的是Java 8,那么您必须尝试这个。。你会学到很多的!!这么多复杂的事情,用这么简单的方式….!!:)您应该打印长度超过5的所有单词,以及以“all”或“all”开头的单词数,但您的代码根本不会这样做。@JoakimDanielson感谢您举手。。看编辑,我还遗漏了什么吗??