我正在处理一个作业问题,该问题要求在同一行上将第N个字符的第N个单词打印成空格。如果第N个单词太短且没有第N个字符,则程序应打印该单词的最后一个字符。如果用户输入一个空词(简单按一下),则该词将被忽略。
(我们尚未学习方法,所以我不应该使用它们)
请参见下面的代码,如果该单词没有第N个字符,我不确定如何获取该代码以打印该单词的最后一个字符。
import java.util.Scanner;
public class Words {
public static void main(String[] args) {
final int N=5;
Scanner input = new Scanner(System.in);
System.out.print("Enter a line of words seperated by spaces ");
String userInput = input.nextLine();
String[] words = userInput.split(" ");
String nthWord = words[N];
for(int i = 0; i < nthWord.length();i++) {
if(nthWord.length()>=N) {
char nthChar = nthWord.charAt(N);
System.out.print("The " + N + "th word in the line entered is " + nthWord + "The " + N + "th charecter in the word is " + nthChar);
}
if(nthWord.length()<N) {
char nthChar2 = nthWord.charAt(nthWord.length()-1);
System.out.print("The " + N + "th word in the line entered is " + nthWord + "The " + N + "th charecter in the word is " + nthChar2);
}
input.close();
}
}
}
运行此命令时出现错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
at java.base/java.lang.String.charAt(String.java:702)
at Words.main(Words.java:24)
我希望在同一行看到第N个字和第N个字符
答案 0 :(得分:3)
用户输入还可以包含少于N个单词,对吗?首先检查应该是这样。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a line of words seperated by spaces ");
String userInput = input.nextLine();
String[] words = userInput.split(" ");
int n = words.length();
System.out.print("Enter lookup word - N");
int askedFor = input.nextInt();
if (askedFor > n) {
//your logic for this condition
return;
}
String nthWord = words[askedFor-1];
if (nthWord.length() < askedFor) print(nthWord.charAt(nthWord.length()-1));
else print(nthWord.charAt(askedFor-1));
input.close();
}
答案 1 :(得分:0)
//Considering line as your input
String[] words = line.split(" ");
//Check if Nth word exists
if(words.length < N){
System.out.println("Nth word does not exists");
return;
}
//Check if Nth character exists in Nth word
if(words[N-1].length() < N){
System.out.println("Nth character in Nth word does not exists");
return;
}
// returning Nth character from Nth word
// Here N-1 = N; as programming starts with 0th index
return words[N-1].charAt(N-1);
答案 2 :(得分:0)
Streams
在Java中可能会有点难,但是,为什么不呢?!
您可以定义适用于任何序列的通用函数:
static <T> T nthOrLastOrDefault(Collection<T> xs, int nth, T defaultValue) {
return xs.stream() // sequence as stream
.skip(nth - 1) // skip n - 1
.findFirst() // get next (the nth)
.orElse(xs.stream() // or if not available
.reduce(defaultValue, (a, b) -> b)); // replace defaultValue with the next and so on
}
如果不是默认值,则返回第n个元素(如果不是最后一个元素)。
现在,您只需将该功能应用于所有单词,然后再应用于返回的单词即可。
(不幸的是,Java对字符的管理方式不同,必须将其从整数转换为字符)
String nthWord = nthOrLastOrDefault(asList(words), N, "");
List<Character> chars = nthWord.chars().mapToObj(i -> (char) i).collect(toList()); // ugly conversion
char nthNth = nthOrLastOrDefault(chars, N, '?');
输出可能是
Enter a line of words seperated by spaces:
one two three four five six
The nth char or last or default of the nth word or last or default is 'e'
Enter a line of words seperated by spaces:
one two three four
The nth char or last or default of the nth word or last or default is 'r'
Enter a line of words seperated by spaces:
The nth char or last or default of the nth word or last or default is '?'
Enter a line of words seperated by spaces:
(完整的示例代码)
static <T> T nthOrLastOrDefault(Collection<T> xs, int nth, T defaultValue) {
return xs.stream() // sequence as stream
.skip(nth - 1) // skip n - 1
.findFirst() // get next (the nth)
.orElse(xs.stream() // or if not available
.reduce(defaultValue, (a, b) -> b)); // replace defaultValue with the next and so on
}
public static void main(String[] args) {
final int N = 5;
try (final Scanner input = new Scanner(System.in)) {
while (true) {
System.out.println("Enter a line of words seperated by spaces:");
final String userInput = input.nextLine();
final String[] words = userInput.split(" ");
String nthWord = nthOrLastOrDefault(asList(words), N, "");
List<Character> chars = nthWord.chars().mapToObj(i -> (char) i).collect(toList()); // ugly conversion
char nthNth = nthOrLastOrDefault(chars, N, '?');
System.out.printf("The nth char or last or default of the nth word or last or default is '%c'%n", nthNth);
}
}
}