[Solved]FileScan and Scanner issue! HELP
So, I keep getting a java.util.InputMismatchException null(in java.util.Scanner). I have used the same logic in a different program with a list of integers. This program takes from a list of numbers from 1-2000. I've made sure that "integers.txt" is an actual text file in the project. I am using BlueJ to code this. Can anyone shed some light on this?
Code :
import java.util.Scanner;
import java.io.*;
public class Sorting
{
private static int[] a = new int[2000];
private int v;
private Scanner fileScan, dataScan;
private static String oneLine;
public Sorting() throws IOException
{
int place = 0;
fileScan = new Scanner(new File("integers.txt"));
while(fileScan.hasNext())
{
oneLine = fileScan.nextLine();
dataScan = new Scanner(oneLine);
v = dataScan.nextInt(); //it errors here
a[place] = v;
place++;
}
}
public static void main(String args[]) throws IOException
{
Sorting s = new Sorting();
BubbleSort bubble = new BubbleSort();
InsertionSort insert = new InsertionSort();
SelectionSort select = new SelectionSort();
bubble.sort(a);
insert.sort(a);
select.sort(a);
System.out.println("Bubble Sort had " + bubble.getCompare()
+ " comparisons and " + bubble.getExchange() + " exchanges.");
System.out.println("Insertion Sort had " + insert.getCompare()
+" comparisons and " + insert.getExchange() + " exchanges.");
System.out.println("Selection Sort had " + select.getCompare()
+" comparisons and " + select.getExchange() + " exchanges.");
}
}
Re: FileScan and Scanner issue! HELP
Add a println() to the code to print out the value of the line read from the file so you can see what the Scanner method is trying to read with nextInt().
Re: FileScan and Scanner issue! HELP
I did a println(oneLine) and received this: "{\rtf1\ansi\ansicpg1252\cocoartf1187"
Re: FileScan and Scanner issue! HELP
I fixed it. The txt file was messed up. Thanks for your help.
Re: FileScan and Scanner issue! HELP
printlns are useful for seeing what the computer is seeing.
Re: FileScan and Scanner issue! HELP
Quote:
Originally Posted by
mm4474
I did a println(oneLine) and received this: "{\rtf1\ansi\ansicpg1252\cocoartf1187"
The text file is not "messed up", but rather it's not a text file at all but instead an rtf or "rich text format" file, the default file type created by Window's WordPad program. You need to save it as text for it to be text.