So here is my code:
public static void getArmor(String treasure)
throws FileNotFoundException{
Random rand=new Random();
Scanner file=new Scanner(new File ("armor.txt"));
while(!file.next().equals(treasure)){
file.next(); //stack trace error here
}
int min=file.nextInt();
int max=file.nextInt();
int defense=min + (int)(Math.random() * ((max - min) + 1));
treasure=treasure.replace("_", " ");
System.out.println(treasure);
System.out.println("Defense: "+defense);
System.out.println("=====");
System.out.println();
}
public static void getTreasureClass(Monster monGet)
throws FileNotFoundException{
Random rand = new Random();
String tc=monGet.getTreasureClass();
while (tc.startsWith("tc:")){
Scanner scan=new Scanner(new File ("TreasureClassEx.txt"));
String eachLine=scan.nextLine();
while(!tc.equals(scan.next())){
eachLine=scan.nextLine();
}
for (int i=0;i<=rand.nextInt(3);i++){
tc=scan.next();
}
getArmor(tc); //stack trace error here
}
}
For some reason I get a No Such Element Exception
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at LootGenerator.getArmor(LootGenerator.java:43)
at LootGenerator.getTreasureClass(LootGenerator.java:68)
at LootGenerator.getMonster(LootGenerator.java:127)
at LootGenerator.theGame(LootGenerator.java:19)
at LootGenerator.main(LootGenerator.java:11)
I'm not sure why though. Basically my program is searching through two text files - armor.txt and TreasureClassEx.txt. getTreasureClass receives a treasure class from a monster and searches through the txt until it reaches a base armor item (a string that does not start with tc:.) It then searches getArmor for an armor that matches the name of the base armor it got in treasure class. Any advice would be appreciated! Thanks!
The link to the txt files is here: http://www.cis.upenn.edu/~cis110/hw/hw06/large_data.zip
It looks like you are calling next even if the scanner no longer has a next element to provide... throwing the exception.
while(!file.next().equals(treasure)){
file.next();
}
Should be something like
boolean foundTreasure = false;
while(file.hasNext()){
if(file.next().equals(treasure)){
foundTreasure = true;
break; // found treasure, if you need to use it, assign to variable beforehand
}
}
// out here, either we never found treasure at all, or the last element we looked as was treasure... act accordingly
I had run into the same issue while I was dealing with large dataset. One thing I've noticed was the NoSuchElementException is thrown when the Scanner reaches the endOfFile, where it is not going to affect our data.
Here, I've placed my code in try block and catch block handles the exception. You can also leave it empty, if you don't want to perform any task.
For the above question, because you are using file.next() both in the condition and in the while loop you can handle the exception as
while(!file.next().equals(treasure)){
try{
file.next(); //stack trace error here
}catch(NoSuchElementException e) { }
}
This worked perfectly for me, if there are any corner cases for my approach, do let me know through comments.
Another situation which issues the same problem,
map.entrySet().iterator().next()
If there is no element in the Map object, then the above code will return NoSuchElementException. Make sure to call hasNext() first.
Looks like your file.next() line in the while loop is throwing the NoSuchElementException since the scanner reached the end of file. Read the next() java API here
Also you should not call next() in the loop and also in the while condition. In the while condition you should check if next token is available and inside the while loop check if its equal to treasure.
I Know this question was aked 3 years ago, but I just had the same problem, and what solved it was instead of putting:
while (i.hasNext()) {
// code goes here
}
I did one iteration at the start, and then checked for condition using:
do {
// code goes here
} while (i.hasNext());
I hope this will help some people at some stage.
Related
Following is the program for reversing a word using a stack. There is no problem in implementation of stack. It is executing finely. When ctrl-c is pressed, it prints the content of try-catch block twice or even thrice. What causes this?
import java.util.NoSuchElementException;
import java.util.Scanner;
class WordReversal {
public static void main(String args[]) {
StackIFace sfWord; // interface reference
Stack s = new Stack();
sfWord = s;
Scanner sc = new Scanner(System.in);
System.out.println("Control-C to stop.");
for(;;) {
String senWord = "", revWord = "";
try {
System.out.println("Enter a word: ");
senWord = sc.next();
for(int i=0;i<senWord.length();i++)
sfWord.push(senWord.charAt(i));
for(int i=0;i<senWord.length();i++)
revWord+=sfWord.pop();
System.out.println("Original Word::"+senWord+"\nReversed Word::"+revWord);
} catch(NoSuchElementException NSEe) {
System.out.println("Control-C command accepted.\nQuitting...");
//break;
}
}
}
}
One thing to notice here is if I put break (just remove two forward slashes in above code), the problem is solved but exactly what is causing this? Is Scanner causing this or is it a language problem?
NoSuchElementException is not actually "intercepting ctrl/C". So what's likely happening is you're terminating input, the next() call throws an exception because there is no input, you print a message, and loop round to do it again.
Input is still terminated, so the next() call throws an exception....
What surprises me is thus not that it loops, but that it stops looping after 2 or 3 times. Maybe some other exception gets thrown that you don't catch; hard to say without adding a handler to check.
But the root issue is: if you want that particular exception to quit the program, you need to write code that quits the program. You in fact did that when you wrote the 'break' out of the loop.
You're using an infinite for loop and not telling it when to stop.
Adding break statement is terminating the for loop. Hence, it is working.
Java newbie here.
I made a function to simply return an int given by user through Scanner.
The goal is to avoid an error if the user does not type an integer, notify them and let them retry.
It works fine if the value is an integer on the first try, but if I type a char (get an error, function "restart") then the function will return the "default" zero.
Tried different things but I definitly don't get the logic here.
Here is the code :
//Initialize the Scanner earlier
public static Scanner keyBoardRead = new Scanner(System.in);
public static int intEntry()
{
int entry;
keyBoardRead = new Scanner(System.in);
if (keyBoardRead.hasNextInt() == true)
{
entry = keyBoardRead.nextInt();
System.out.println("entry variable = "+entry);
// Here the correct entry prints but doesn't seem to return
return entry;
}
else
{
System.out.println("Invalid entry.\n");
intEntry();
}
return 0;
}
Sample output :
z
Invalid entry.
2
entry variable = 2
// Function exits and output = 0 (?!)
Thanks for your help and please criticize my code :)
I reworked your code a bit as there were a few flaws with it:
public static Scanner keyBoardRead = new Scanner(System.in);
public static int intEntry()
{
int entry;
if (keyBoardRead.hasNextInt())
{
entry = keyBoardRead.nextInt();
System.out.println("entry variable = "+entry);
// Here the correct entry prints but doesn't seem to return
return entry;
}
keyBoardRead.next();
System.out.println("Invalid entry.\n");
return intEntry();
}
Here are the changes explained below:
You do not need to redeclare new Scanner(System.in) with every call
to the method, you state you initially declare it as a class field,
so it should be accessible from inside the method each time.
Your else statement in general is completely unnecessary because you have a return in your if. If your if is executed, it will never enter the code afterward anyway.
You need to return the value from intEntry() with return intEntry() as currently you are just discarding the return value and simply returning 0 unconditionally if else is executed even a single time.
You need to use keyBoardRead.next() if an invalid entry is entered in order to move to the next value and discard the previously entered result. You can also use keyBoardRead.nextLine() instead if you wish to discard the entire line instead.
Using == true on a boolean value is redundant as you can simply check the boolean directly, so if (keyBoardRead.hasNextInt()) instead of if (keyBoardRead.hasNextInt() == true). Credit to #Joop Eggen for catching it.
Example Run:
hi
Invalid entry.
wrong
Invalid entry.
entries
Invalid entry.
55
entry variable = 55
Note: You have a lot of blank space in the output because you are use println and also using \n in the print so it will move to the next line twice.
Also you could easily create a variant to this solution that utilizes a while loop instead of recursion, which is probably a better way to do it so you cannot possibly run into a stack overflow (and is typically easier to read), but I kept the recursion in to keep it similar to your solution.
I have an assignment that requires me to write a program in JAVA with 10 different methods including the main. It obtains the input from a file in order to extract data from a second file through the various methods. Lastly it prints the results to a third file. THis is an intro class and we were insturcted to use the hasNext method. THe second file where the data is retrieved from has 10 rows and 5 columns, each column representing something different. I used sc1.nextInt() since our professor warned us that the programs will read every piece of data and we havent learned how to extract data from just one column. I am stuck on an error I keep receiving. I have included a snippet of my code if anyone can help me. Thank you.
this is the error I keep receiving:
Exception in thread "main"
java.util.InputMismatchException at
java.util.Scanner.throwFor(Scanner.java:864) at
java.util.Scanner.next(Scanner.java:1485) at
java.util.Scanner.nextInt(Scanner.java:2117) at
java.util.Scanner.nextInt(Scanner.java:2076) at
homework4.HomeWork4.checkNumber(HomeWork4.java:47) at
homework4.HomeWork4.main(HomeWork4.java:26)
/Users/xiomarahenriquez/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53:
Java returned: 1 BUILD FAILED (total time: 0 seconds)"
public static PrintStream ps;
public static void main(String[] args) throws Exception {
ps = new PrintStream("elementsResults.txt");
Scanner sc1 = new Scanner(new File("input.txt"));
int atomicNumber, valid=0, invalid=0, totalProcessed=0;
while (sc1.hasNext()) {
atomicNumber = sc1.nextInt();
checkNumber(atomicNumber);
if(checkNumber(atomicNumber)== true){
++valid;
} else {
++invalid;
}
++totalProcessed;
}
}
public static boolean checkNumber (int atomicNumber) throws Exception {
Scanner sc2 = new Scanner (new File("PeriodicTable.txt"));
int columnA = sc2.nextInt();
String columnB;
int columnC,columnD,columnE;
while (sc2.hasNext() && (columnA > -1 || columnA < 118)) {
columnA=sc2.nextInt();
columnB=sc2.next();
columnC=sc2.nextInt();
columnD=sc2.nextInt();
columnE=sc2.nextInt();
if (atomicNumber==columnA) {
return true;
}
}
sc2.close();
return false;
}
I think that the cause of your problem is in the first line of your exception stack trace:
Exception in thread "main" java.util.InputMismatchException
Here's a link to the documentation for the InputMismatchException. I can't say for sure since I don't know what your input files look like but I'm pretty sure that when you're calling nextInt(), the next token read isn't something that can be cast to an int. My guess is that the Scanner is encountering some text or something else preventing it from returning an int. To figure out which token is causing the problem, I'd try wrapping your invocations of nextInt() in try/catch blocks. When the Scanner throws an InputMismatchException, it will not pass the token that caused the exception so that after the exception is thrown you can get the value of the token (like with the next() method) or skip the token altogether. Here's an example (I don't have access to an IDE right now so this isn't tested but hopefully you can get the idea):
//Some initialization code here...
Scanner myScanner = new Scanner(new File("myFile.txt"));
while(myScanner.hasNext()) {
try {
int myIntVariable = myScanner.nextInt();
} catch (InputMismatchException ex) {
System.out.println("Here's the token that caused the problem: " + myScanner.next());
}
}
//The rest of your code here...
By the way, if you're not absolutely sure that the token that you're retrieving is the type that you think it's going to be (in your case an int), it's probably a good idea to wrap that portion of the code in a try/catch block so that you can handle cases where the token isn't what you think it is.
This is my first answer. Hope it helps.
In your while loop you are running the checkNumber method twice. That's unnecessary. Do it just once like below. Also there is a slight difference between ++i and i++ so check this link: what is the difference between i++ & ++i in for loop (Java)?
while (sc1.hasNext()) {
atomicNumber = sc1.nextInt();
if(checkNumber(atomicNumber)== true){
valid++;
} else {
invalid++;
}
totalProcessed++;
}
This question already has answers here:
Endless while loop problem with try/catch
(2 answers)
Closed 7 years ago.
Below code,
import java.util.Scanner;
public class Dummy {
static Scanner sc = new Scanner(System.in);
public static int getIntegerInput(String prompt){
int choice = 0;
for(;;){
System.out.print(prompt);
try{
choice = sc.nextInt();
break;
}catch(java.util.InputMismatchException ex){
System.out.print("What??? ");
}
}
return choice;
}
public static void main(String[] args) {
int choice = getIntegerInput("Enter a number: ");
} //end main
}
does not stop for next user input, if the first user input raised an exception.
How do I understand this problem in the above code? placing sc.next() in catch resolves the problem. But I'm still not clear what is going on under the hood? What is the right approach to resolve this problem?
When nextXYZ() fails to consume a token it leaves it in the InputStream. Here, you are looping over the same input endlessly - each iteration, you attempt to consume this token, throw an exception if it isn't an integer, catch it, and try reading it again - forever.
EDIT:
In order to work around this, you could use next() to consume that token and move on to the next one:
for(;;) {
System.out.print(prompt);
try{
choice = sc.nextInt();
break;
} catch(java.util.InputMismatchException ex) {
sc.next(); // here
}
}
The problem with Scanner next() are they will not advances if the match is not found. And the character for which it failed remain in the stream. Hence its very important to advance the scanner if you found non intended character.
You can use next() method which actually consumes any character or you can use skip method passing skip pattern.
Use hasNext() to know whether a valid match is present or not. If not then consume that character using above said methods.
If it doesnt find an int on the next like, it throws an error. This error is then caught by your program, so the break is never hit because the error jumps over it whenever a non-int (including nothing) is found.
this is my first post so forgive me if i have posted incorrectly. I have a task that i need to complete but i cant get it to work properly. the compiler that i use is bluej. what i need to do is to use scanner to read a text file and compare a user input to the text file. if the input string compares then it should print out that ""The word is on the text file". Unfortunately i cant get this to work. My code reads the file because it prints out to the console but no comparison it s happening. please have a look at my code and give me some pointers. i have been trying to use .equals():
private boolean searchFromRecord(String recordName, String word) throws IOException
{
// Please write your code after this line
File file = new File(recordName);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
for(int i = 0; scanner.hasNextLine(); i++){
String compare = scanner.nextLine();
IO.outputln("word#" + i + ":" + compare);
}
scanner.close();
if (scanner.equals(word)){
return true;
} else{
return false;
}
}
return true;
}
this is what i get output in the console:
Input a word: IRON
AA 888
word#0:BULLET
word#1:1
word#2:AE 1688
word#3:CHEERS
word#4:GAMES
word#5:IRON MAN
word#6:WOLF
word#7:Testing
word#8:Wonderful
The word "IRON" is not in the record.
Here are some problems, along with why they are problems & a suggestion on how they could be fixed:
Problem: closing a scanner within the a loop that uses it will cause an exception. Reason: after we go through the loop once, the scanner will be closed. when we loop through again, an error will occur since the loop uses the scanner, which means the scanner should be "open". Possible solution: move scanner.close() to after the while loop.
Problem: we shouldn't return true at the end of this method. Reason: I'm guessing that this method is supposed to return true if the word is found, and false otherwise. Now, the only way to get to this return statement is if our word doesn't exist in the recordFile; it should return false. Possible solution: return false at the end of the method instead.
Problem: the first line in recordFile will never be checked for equality with word Reason: each method call of scanner.nextLine() will return each line from the recordFile as a String once and only once. In your code, it is called once in the beginning of the while loop's body, but not used to compare with word, then after, it is used in the for loop for comparison Possible solution: remove the line: System.out.println(scanner.nextLine());.
Problem: scanner.equals(word) will probably always return false. Reason: scanner is a Scanner, and word is a String, they should never be equal. Possible solution: replace scanner.equals(word) with compare.equals(word)
Problem: word is not actually compared with each compare. Reason: it is outside the for loop. Possible solution: move the if else block into the end of the for loop's body.
I don't think the while loop is really needed. I strongly recommend that the while loop, is removed, but keep the body.
Problem: Moving the if else block into the for loop, and above the scanner.close() means that the scanner.close() will never be run. Reason: once a return statement is executed, the flow of control immediatly exits the method, and returns to where the method was invoked which makes code after return statements useless. Possible solution: instead of returning right away, declare some sort of boolean variable that will store the return value. have the return value be modified throughout the method, then return the variable at the very end, after scaner.close()
There are many many other ways to fix each of these problems other than the ones suggested here.
I hope you find this helpful! :)
your code, refactored to implement the suggested solutions above:
private boolean searchFromRecord(String recordName, String word) throws IOException {
// Please write your code after this line
Boolean wordFound = false; // indicates if word exists in recordFile.
File file = new File(recordName); // file at path "recordName"
Scanner scanner = new Scanner(file); // reads records from "file"
// iterate through the recordFile, to see if "word" already exists
// within recordFile.
for(int i = 0; scanner.hasNextLine(); i++) {
// read the record from the file
String compare = scanner.nextLine();
IO.outputln("word#" + i + ":" + compare);
// compare the record with our word
if (compare.equals(word)){
wordFound = true;
break; // bail out of loop, our work here is done
}
}
// clean up, and return...
scanner.close();
return wordFound;
}
First, scanner is not a String and it will not equal a String. Second, you are dropping lines - scanner.nextLine() gets the next line, and you print it (but don't save it or compare it). I think you wanted something more like this,
// eats and tosses input.
// System.out.println(scanner.nextLine());
String line = scanner.nextLine();
for(int i = 0; scanner.hasNextLine(); i++){
String compare = scanner.nextLine();
IO.outputln("word#" + i + ": " + compare + " to line: " + line);
if (line.contains(compare)){ // "IRON MAN" starts with "IRON", it doesn't equal IRON.
return true;
}
}
scanner.close();
return false; // <-- default.
Another flavor is to read the whole file into a String variable and look for specified String inside the String.
Code:
File file = new File("C:\\Users\\KICK\\Documents\\NetBeansProjects"
+ "\\SearchWordinFile\\src\\searchwordinfile\\words.txt");
String s="";
try(Scanner input = new Scanner(file)){
input.useDelimiter("\\A");
if (input.hasNext()) {
s = input.next();
}
}catch(Exception e){
System.out.println(e);
}
if(s.contains("IRON"))
System.out.println("I found IRON");
}
Output:
I found IRON
My File content
BULLET
1
AE 1688
CHEERS
GAMES
IRON MAN
WOLF
Testing
Wonderful