I wrote the below code in order to read some integers from a text file and then print in a new txt every integer after I added the value ten to each of them. The "-1" int is as a pointer to show the end.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class SmallChallengeCh {
public static void main(String[] args){
Scanner in = null;
PrintWriter pw = null;
try{
File f = new File("C:\\Users\\paioa\\primlnl.txt");
in = new Scanner(f);
pw = new PrintWriter(new FileOutputStream("C:\\Users\\paioa\\primOut.txt"),true);
int num = in.nextInt();
while(num != -1 ){
num = num + 10;
System.out.print(num + " ");
pw.print(num + " ");
num = in.nextInt();
}
}catch(FileNotFoundException e1){
System.out.println("The file does not exist");
}finally {
try{
if(in != null) in.close(); else throw new Exception("NULL");
if(pw != null) pw.close();else throw new Exception("NULL");
}catch (Exception e){
System.out.println(e.getMessage());
}
}
}
}
However I am getting the below error
**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 gr.aueb.elearn.ch6.SmallChallengeCh.main(SmallChallengeCh.java:18)
Process finished with exit code 1**
But I cannot understand why. The inputmismatchexception is due to the fact that probably my file doesn't contain ints but that's not true here is the txt.
Input of txt file, also saved with coding UTF-8
Solved
ANSWER: The issue was with coding UTF-8. I should left the file ANSI. I don't know why the example I was reading/exercising on advocated this ..