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++;
}
Related
I am having a problem while using a Scanner and I do not know how to solve it, kind of new in Java. I have created a menu to read an input char provided by the user through a Scanner and I read options from the menu with this loop:
/**
* Constant to exit the menu.
*/
private static final char EXIT = 'E';
public void run() {
char option = EXIT;
do {
menu.show();
option = menu.readOption();
try {
processOption(option);
} catch (RuntimeException exception) {
handleSystemError(exception);
} catch (Exception exception) {
handleUserError(exception);
}
} while (option != EXIT);
}
This is what the method readOption does :
public char readOption() {
return Console.readChar(" Option ");
}
And this is what the method readChar does :
public static char readChar(String msg) {
out.println( msg + ": ");
keyboard.useDelimiter(System.lineSeparator());
char res = keyboard.next().charAt(0);
keyboard.reset();
return res;
}
The problem is, the loop in its first performance works perfectly fine. The program works propperly. But after the first performance, when it starts again, in the line where it readsOption, it throws a NoSuchElementException and I have been many hours trying to solve it and I do not know how to. Apparently the problem is in the method readChar, when I try to do "keyboard.next()" it throws the no such element exception. Cannot explain that it works the first time and not a second one. When first performance, it waits for me to introduce a char as I expect, but in the second one,while I wish to be expected to introduce a char, when "keyboard.next()", it throws the mentioned exception. Having an issue clearly with my Scanner, I hope someone can identify the issue here. Thank you very much everyone.
I finally found how to solve it thanks to this page: Solucione el error NoSuchElementException en Java
The issue was that because of the size of this program I had to use 2 scanners at the same time, and when I closed one of them, it caused the other one to stop working, throwing that exception.
I got an run time exception in my program while I am reading a file through a Scanner.
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Day1.ReadFile.read(ReadFile.java:49)
at Day1.ParseTree.main(ParseTree.java:17)
My code is:
while((str=sc.nextLine())!=null){
i=0;
if(str.equals("Locations"))
{
size=4;
t=3;
str=sc.nextLine();
str=sc.nextLine();
}
if(str.equals("Professions"))
{
size=3;
t=2;
str=sc.nextLine();
str=sc.nextLine();
}
if(str.equals("Individuals"))
{
size=4;
t=4;
str=sc.nextLine();
str=sc.nextLine();
}
int j=0;
String loc[]=new String[size];
while(j<size){
beg=0;
end=str.indexOf(',');
if(end!=-1){
tmp=str.substring(beg, end);
beg=end+2;
}
if(end==-1)
{
tmp=str.substring(beg);
}
if(beg<str.length())
str=str.substring(beg);
loc[i]=tmp;
i++;
if(i==size ){
if(t==3)
{
location.add(loc);
}
if(t==2)
{
profession.add(loc);
}
if(t==4)
{
individual.add(loc);
}
i=0;
}
j++;
System.out.print("\n");
}
with Scanner you need to check if there is a next line with hasNextLine()
so the loop becomes
while(sc.hasNextLine()){
str=sc.nextLine();
//...
}
it's readers that return null on EOF
ofcourse in this piece of code this is dependent on whether the input is properly formatted
I also encounter with that problem.
In my case the problem was that i closed the scanner inside one of the funcs..
public class Main
{
public static void main(String[] args)
{
Scanner menu = new Scanner(System.in);
boolean exit = new Boolean(false);
while(!exit){
String choose = menu.nextLine();
Part1 t=new Part1()
t.start();
System.out.println("Noooooo Come back!!!"+choose);
}
menu.close();
}
}
public class Part1 extends Thread
{
public void run()
{
Scanner s = new Scanner(System.in);
String st = s.nextLine();
System.out.print("bllaaaaaaa\n"+st);
s.close();
}
}
The code above made the same exaption, the solution was to close the scanner only once at the main.
You're calling nextLine() and it's throwing an exception when there's no line, exactly as the javadoc describes. It will never return null
https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
For whatever reason, the Scanner class also issues this same exception if it encounters special characters it cannot read. Beyond using the hasNextLine() method before each call to nextLine(), make sure the correct encoding is passed to the Scanner constructor, e.g.:
Scanner scanner = new Scanner(new FileInputStream(filePath), "UTF-8");
Your real problem is that you are calling "sc.nextLine()" MORE TIMES than the number of lines.
For example, if you have only TEN input lines, then you can ONLY call "sc.nextLine()" TEN times.
Every time you call "sc.nextLine()", one input line will be consumed. If you call "sc.nextLine()" MORE TIMES than the number of lines, you will have an exception called
"java.util.NoSuchElementException: No line found".
If you have to call "sc.nextLine()" n times, then you have to have at least n lines.
Try to change your code to match the number of times you call "sc.nextLine()" with the number of lines, and I guarantee that your problem will be solved.
Need to use top comment but also pay attention to nextLine(). To eliminate this error only call
sc.nextLine()
Once from inside your while loop
while (sc.hasNextLine()) {sc.nextLine()...}
You are using while to look ahead only 1 line. Then using sc.nextLine() to read 2 lines ahead of the single line you asked the while loop to look ahead.
Also change the multiple IF statements to IF, ELSE to avoid reading more than one line also.
I ran into this problem, my structure was:
1 - System
2 - Registration <-> 3 - validate
I was closing Scanner on each of the 3 steps. I started to close the Scanner only in system and it solved.
I'm starting to learn JAVA and I have a very simple task to do but I have a problem
In Eclipse it's work fine, but in IDEONE is error:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at Main.main(Main.java:9)
There is my code:
Scanner tek=new Scanner(System.in);
String t2=tek.nextLine();
int t=Integer.parseInt(t2);
int tablica1[]= new int[t];
if(t>=1 && t<=100)
{
for(int i=0; i<t; i++)
{
String ciag=tek.nextLine();
String tablica[]=ciag.split(" ");
int x=Integer.parseInt(tablica[1]);
int y=Integer.parseInt(tablica[2]);
if(tablica[0].equals("+"))
{
tablica1[i]=x+y;
} else if(tablica[0].equals("-"))
{
tablica1[i]=x-y;
} else if(tablica[0].equals("*"))
{
tablica1[i]=x*y;
} else if(tablica[0].equals("/"))
{
tablica1[i]=x/y;
} else if(tablica[0].equals("%"))
{
tablica1[i]=x%y;
}
}
for(int i=0; i<t; i++)
{
System.out.println(tablica1[i]);
}
}
I know I can declare
int t=tek.nextInt(); in start but when I do this I have more errors;p
Could you tell me, please what's wrong with my code?
Can you explain this step-by-step?
I have this same error in 2 programs; in the other programs I declare tek.nextLine(); before for loop and it worked.
Before doing tek.nextLine(), in your for loop, you need to check if Scanner has nextToken
while (tek.hasNext())
Check https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
I would suggest that it isn't actually your code making the error, but IDEONE. If it works in Eclipse there's no other reason I can think of that would stop it from working in another IDE, unless of course you are using a different Java compiler.
String ciag=tek.nextLine();
String tablica[]=ciag.split(" ");
int x=Integer.parseInt(tablica[1]);
int y=Integer.parseInt(tablica[2]);
How do you know there are 3 tokens in ciag? How do you know that the inputs 1 and 2 are integers ? (You will get an Exception if the user enters "- 5 x")
Can you try running this code using command prompt? and see what errors are you getting. IDE doesn't generate any error.
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.
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.