Welcome to Gaia! ::

Twilight_Dwagon's avatar

Anxious Poster

7,800 Points
  • Forum Explorer 100
  • Forum Dabbler 200
  • Forum Sophomore 300
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?

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() //this is the line with the issue

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.")
}
}
Sitwon's avatar

8,500 Points
  • Gaian 50
  • Member 100
  • Contributor 150
In the future, please use gist.github.com or pastebin.com to share your code with us. Gaia destroys the formatting making it more difficult to analyze your source.

According to the docs, nextInt() throws InputMismatchException "if the next token does not match the Integer regular expression, or is out of range". How about you post your 'integers.txt' file up on gist or pastebin for us?

Another trick you could do, add a
System.err.println("DEBUG: " + oneLine) ;
right after line 16.


Edit: when you figure that part out, you have some other major problems in that code. For example, you're abusing the Sorting constructor in a pretty horrible way. There's absolutely no reason for it either. If I was your teacher I'd give you half credit at best.
CuAnnan's avatar

Dapper Genius

4,550 Points
  • Person of Interest 200
  • Autobiographer 200
  • Dressed Up 200
Twilight_Dwagon
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?


Firstly, here ya go. Assuming that Bubble, Insert and Select Sorts all extend a Sort type, the debug method should turn that sysoing into one line that you can use ad nauseam.

Secondly, the try catch block will throw a single error and continue afterwards when something goes wrong. IF it isn't immediately apparent, there is a non-zero chance that you have a character in there that's not showing up (happened to me a couple of times, in particular with the beep UTF symbol) so you may have to print out the int char values of each line and if it doesn't work as expected, you should have your problem there.

If you post the file itself, it'd help.
Sitwon's avatar

8,500 Points
  • Gaian 50
  • Member 100
  • Contributor 150
CuAnnan
First of all, we have policy in C&T/TT of not doing people's homework for them. If you do the work for them, they won't learn anything. (Also, it's often considered cheating or academic dishonesty in most schools.) I would appreciate if you would respect the rules and policies for this forum.

Second, you've still not addressed any of the deeper architectural problems in the code sample. For example, the terrible scoping of variables. Or the use of a constructor for the sole purpose of populating a static array. Or the use of instance methods where static methods would have not only sufficed but made a lot more sense.

A programs design or architecture is just as important as whether or not it eventually arrives at the correct answer or passes some superficial interface tests.
CuAnnan's avatar

Dapper Genius

4,550 Points
  • Person of Interest 200
  • Autobiographer 200
  • Dressed Up 200
Sitwon
First of all, we have policy in C&T/TT of not doing people's homework for them. If you do the work for them, they won't learn anything. (Also, it's often considered cheating or academic dishonesty in most schools.) I would appreciate if you would respect the rules and policies for this forum.

I didn't do his/her homework though.
I added a try catch block so he could see what was causing the problem and explained one of the things that might not be immediately apparent even if s/he sysos it.
I will, however, refrain from posting code in future.

Sitwon
Second, you've still not addressed any of the deeper architectural problems in the code sample. For example, the terrible scoping of variables.

That's because I don't know that it's a full code sample. There may be perfectly reasonable reasons that s/he's using a constructor based approach, rather than a static method of an abstract class which returns an array, but is not present.
It's also entirely possible that s/he's being taught it sequentially and has not been taught about scoping or signatures yet and is, therefore, reproducing verbatim from a given demonstration (I had pretty bad lecturers and mixed TAs).

Sitwon
A programs design or architecture is just as important as whether or not it eventually arrives at the correct answer or passes some superficial interface tests.

That depends entirely on the purpose of the program.
If it is to teach data structures and algorhitms, then it is not important that his object orientation is broken to hell.
You can teach DSA in procedural programming languages as well (quantatively) as you can teach them in OO ones.
It may be that the course takes the "teaching procedural programming through java and teaching oo later" approach like mine did (in 2000).
Yes, it's a fundamentally bad approach, but it's still taken.

Quick Reply

Submit
Manage Your Items
Other Stuff
Get GCash
Offers
Get Items
More Items
Where Everyone Hangs Out
Other Community Areas
Virtual Spaces
Fun Stuff
Gaia's Games