从ArrayList中移除空格。

huangapple go评论85阅读模式
英文:

Remove blank spaces from arraylist

问题

我刚开始学习计算机科学,我们刚开始做我们的第一个Java项目。这是一个信息系统,应该可以跟踪我们的朋友。

我已经创建了一个ArrayList,可以在里面添加一些朋友的兴趣。问题是我可以轻松地输入空格,然后按Enter键将一个“空”的元素插入到ArrayList中。我尝试过使用不同的方法从ArrayList中移除它们,但没有得到答案。下面是我卡在那里的代码。

我设置了当我输入“exit”时程序会退出,这个功能运行得非常好。在开始时,当我键入“exit”时,程序会将“exit”包含在ArrayList中,但在我添加了checklist()方法之后,我能够将“exit”排除在ArrayList之外。但是我似乎无法处理空格和Enter键。

package testing;

import java.util.ArrayList;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        ArrayList<String> interests = new ArrayList<String>();
        String interest;
        String quit = "exit";
        do {
            Scanner userInput = new Scanner(System.in);
            System.out.println("Write interest: ");
            interest = userInput.nextLine();
            if (interest.isEmpty()) { // 确保您不能按Enter键。
                System.out.println("You just entered SPACE, please write an interest: ");
                interest = userInput.nextLine();
            }  else if (interest.isBlank()) { // 确保您不能输入纯空格。
                System.out.println("You just entered a plain space, please write an interest: ");
                interest = userInput.nextLine();
            }
            interests.add(interest);
        } while (!interest.equals(quit));
        checkList(interests);
        System.out.println(interests);
    }

    public static void checkList(ArrayList<String> interests) {
        String quit = "exit";
        for (int i = 0; i < interests.size(); i++) {
            if(interests.get(i).isBlank()) {
                interests.remove(i);
                System.out.println("Position: " + i + " is a blank space");
                System.out.println("Removed blank space: " + interests.get(i));
            } else if (interests.get(i).isEmpty()) {
                interests.remove(i);
                System.out.println("Position: " + i + " is an ENTER");
                System.out.println("Removed ENTER: " + interests.get(i));
            } else if (interests.get(i).equals(quit)) {
                interests.remove(i);
            }
        }
    }

}
英文:

I have just started studying Computer Science, and we have just started our first Java project. It is a information system that should keep tracks of our friends.

I have made an arraylist where I can put in some interests that our friends have. The problem is that I can easily enter blank spaces and just press enter to insert an "empty" element into the array list. I have tried using different methods to remove them from the array list, and haven't come to an answer. Under here is the code that I am stuck with.

I have set the program to exit when I enter "exit", and that works perfectly fine. At the start when I typed in "exit", then the program would include the "exit" into the array, but after I added the checklist() method, I was able to keep "exit" out of the array list. I just can't seem to make it work with spaces and Enters.

package testing;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
ArrayList&lt;String&gt; interests = new ArrayList&lt;String&gt;();
String interest;
String quit = &quot;exit&quot;;
do {
Scanner userInput =  new Scanner(System.in);
System.out.println(&quot;Write interest: &quot;);
interest = userInput.nextLine();
if (interest.isEmpty()) { // Makes sure you cant press ENTER.
System.out.println(&quot;You just entered SPACE, please write an interest: &quot;);
interest = userInput.nextLine();
}  else if (interest.isBlank()) { // Makes sure you cant enter a plain space.
System.out.println(&quot;You just entered a plain space, please write an interest: &quot;);
interest = userInput.nextLine();
}
interests.add(interest);
} while (!interest.equals(quit));
checkList(interests);
System.out.println(interests);
}
public static void checkList(ArrayList&lt;String&gt; interests) {
String quit = &quot;exit&quot;;
for (int i = 0; i &lt; interests.size(); i++) {
if(interests.get(i).isBlank()) {				
interests.remove(i);
System.out.println(&quot;Position: &quot; + i + &quot; is a blank space&quot;);
System.out.println(&quot;Removed blank space: &quot; + interests.get(i));
} else if (interests.get(i).isEmpty()) {
interests.remove(i);
System.out.println(&quot;Position: &quot; + i + &quot; is a ENTER&quot;);
System.out.println(&quot;Removed ENTER: &quot; + interests.get(i));
} else if (interests.get(i).equals(quit)) {
interests.remove(i);
}
}
}
}

答案1

得分: 1

我已经成功找到了我的问题的答案。解决方案是使用这个,而不是使用方法 checkList()

interests.removeAll(Arrays.asList("", null, " ", "exit"));
英文:

I already managed to find the answer to my question. The solution was to use this instead of using the method checkList().

interests.removeAll(Arrays.asList(&quot;&quot;, null, &quot; &quot;, &quot;exit&quot;));

答案2

得分: -1

我不熟悉Java。
但我认为最好的概念是阻止空格输入。
因此,用户需要将数据输入到数组中。
只需检查字符串是否为null || 空。

英文:

I am not familier with Java.
But i think the best concept would be to block an input from empty space.
So a user needs to enter data into an array.
Just check if string is null || empty

huangapple
  • 本文由 发表于 2020年10月3日 05:29:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64178456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定