Reputation: 41
I get an error from automated JUnit 4 unit testing software that my course instructor uses to automatically check assignments.
We upload our draft code to a cloud program tester which automatically checks and grades the assignments.
This is week 1 of first semester. I've never coded in Java before. And the lessons haven't taught if/else, while, loops, switch etc. so I don't think it's okay to use those (they appear next module.)
I followed a class video tutorial, which doesn't throw any exceptions in the tutorial — so I don't know what's going on.
The unit was on variables, input, printing out, Booleans, operators, and a few things like contains, replace, equals etc.
Note: I am allowed to ask questions on StackOverflow per instructor.
The error is: java.util.NoSuchElementException: No line found
No line number is specified in the testing software where the exception occurs. And since it's not happening in my IDE, I don't know where it is.
Other solutions I've found require while and other methods I'm not supposed to use.
My code (below) runs okay in the VSCODE IDE cloud the school provided.
import java.util.Scanner;
public class ContainsAnyCase {
public static void main(String[] args) {
System.out.println("Type a word:\n");
Scanner userInputWord = new Scanner(System.in);
String word = userInputWord.nextLine();
String wordlc = word.toLowerCase();
System.out.println("Type a sentence:\n");
Scanner userInputSentence = new Scanner(System.in);
String sentence = userInputSentence.nextLine();
String sentencelc = sentence.toLowerCase();
boolean isContains = sentencelc.contains(wordlc);
System.out.println(isContains);
}
}
I'm wondering if anyone can explain why there is an error and if there are ways to fix it or avoid the error without going into the territory of future modules? Also, I'm still learning how to use Stack Overflow appropriately. If I asked my question wrong, please advise how I can improve my question or post. Thank you.
I expected for the program to tell me true/false if the word was in the sentence. In my IDE, it worked. When I uploaded it to the JUnit4, it said java.util.NoSuchElementException: No line found
Upvotes: 2
Views: 109
Reputation: 39142
Here's an example of using just ONE SCANNER as suggested in the comments.
I've also eliminated the second set of variables for lower case; just convert the input directly to lower case and store it in the original variable:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type a word:\n");
String word = sc.nextLine().toLowerCase();
System.out.println("Type a sentence:\n");
String sentence = sc.nextLine().toLowerCase();
boolean isContains = sentence.contains(word);
System.out.println(isContains);
}
If you need to preserve the exact user input for later, then you can convert to lower case only when you call contains() like this:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type a word:\n");
String word = sc.nextLine();
System.out.println("Type a sentence:\n");
String sentence = sc.nextLine();
boolean isContains = sentence.toLowerCase().contains(word.toLowerCase());
System.out.println(isContains);
}
Upvotes: 1