英文:
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("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;
}
}
String str = null;
int len = -1;
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 == "Above"){
for (int i = 0; i < array.length; i++) {
if (array[i] != null)
len ++;
if(array[i].charAt(i)>10)
for (int x = 0; x < len; x++) {
if(array[x] == str )
System.out.print(len);
System.out.print(array[x] + " ");
}
}
}
else
{
}
System.out.println();
}
}
The block of code I'm struggling with is:
for (int i = 0; i < array.length; i++) {
if (array[i] != null)
len ++;
if(array[i].charAt(i)>10)
for (int x = 0; x < len; x++) {
if(array[x] == str )
System.out.print(len);
System.out.print(array[x] + " ");
}
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() > 10) {
System.out.print(value + " ");
}
}
}
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论