Java数组在超过一定数量的字符时显示数组值

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

Java Array display array values over an amount of characters

问题

import java.util.*;

public class CategorizeStrings {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String[] array = new String[20];
        System.out.println("Please enter anything..., or press QUIT to quit.");

        for (int i = 0; i < array.length; i++) {
            array[i] = s.nextLine();
            boolean result = Arrays.stream(array).anyMatch("QUIT"::equals);
            if (result) {
                break;
            }
        }
        int len = 0;

        System.out.println("Would you like to display strings with above 10 charaters (Above) or below 10 characters (Below)? Type Above or Below:");
        String answer = s.nextLine();
        if (answer.equals("Above")) {
            for (int i = 0; i < array.length; i++) {
                if (array[i] != null && array[i].length() > 10) {
                    System.out.print(array[i] + " ");
                }
            }
        } else {
            for (int i = 0; i < array.length; i++) {
                if (array[i] != null && array[i].length() <= 10) {
                    System.out.print(array[i] + " ");
                }
            }
        }

        System.out.println();
    }
}
英文:

I am stuck. I have my array with data the user inputs. After their information is entered the program asks if they want to see items with characters above 10 or below. I can't seem to figure out this section. Below is where I am currently with the code.

import java.util.*;
import java.util.Scanner;
public class CategorizeStrings {
public static void main(String[] args) 
{
Scanner s = new Scanner(System.in);
String[] array = new String[20];
System.out.println(&quot;Please enter anything..., or press QUIT to quit.&quot;);
for (int i = 0; i &lt; array.length; i++) {
array[i] = s.nextLine();
boolean result = Arrays.stream(array).anyMatch(&quot;QUIT&quot;::equals);
if(result) 
{
break;
}
}
String str = null;
int len = -1;
System.out.println(&quot;Would you like to display strings with above 10 charaters (Above) or below 10 characters (Below)? Type Above or Below:&quot;);
String answer = s.nextLine();
if(answer == &quot;Above&quot;){
for (int i = 0; i &lt; array.length; i++) {
if (array[i] != null)
len ++;
if(array[i].charAt(i)&gt;10)
for (int x = 0; x &lt; len; x++) {
if(array[x] == str )
System.out.print(len);
System.out.print(array[x] + &quot; &quot;);
}
}
}
else
{
}
System.out.println();
}	

}

The block of code I'm struggling with is:

   for (int i = 0; i &lt; array.length; i++) {
if (array[i] != null)
len ++;
if(array[i].charAt(i)&gt;10)
for (int x = 0; x &lt; len; x++) {
if(array[x] == str )
System.out.print(len);
System.out.print(array[x] + &quot; &quot;);
}

Any help is appreciated.

答案1

得分: 1

如果您只想打印长度超过10个字符的单词

for (String value : array) {
if (value != null) {
if (value.length() > 10) {
System.out.print(value + " ");
}
}
}

这是我会这么做的。

还要注意,字符串不能使用'=='进行比较,您应该使用equals或contentEquals

例如:

if (string1.equals(string2)) {
// 在此处插入代码
}

或者

if (string1.contentEquals(string2)) {
// 在此处插入代码
}
英文:

If you just want to print the words that have above 10 characters

       for (String value : array) {
if (value != null) {
if (value.length() &gt; 10) {
System.out.print(value + &quot; &quot;);
}
}
}

This is how I would do it

Also note that Strings can't be compared using '=='
You should use equal or contentEquals

For example:

if (string1.equals(string2)) {
// Insert code here
}

or

if (string1.contentEquals(string2)) {
// Insert code here
}

huangapple
  • 本文由 发表于 2020年10月19日 23:48:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64430819.html
匿名

发表评论

匿名网友

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

确定