I have a homework assignment for Java and I must read a file that has a list of license plates. If the car goes in, it will have a + before the first letter and if it leaves, it will have a - before. I must print out all the cars left in the garage. The license plate numbers are on different lines. This is what I have:
I get these exceptions:
Exception in thread "main" java.util.InputMismatchException: For input string: "+63092"
at java.util.Scanner.nextByte(Scanner.java:1812)
at java.util.Scanner.nextByte(Scanner.java:1766)
at garage.Garage.main(Garage.java:39)
Please help! Keep in mind I am a NEW programmer, so fancy lingo will not work.
Code:
package garage;
import java.io.*;
import java.util.Scanner;
public class Garage {
static String[] garagearray = new String[1000];
static int maxslot = 0;
static String readlicense(Scanner scan) {
String license = "";
license = scan.next();
return(license);
}
public static void main(String[] args) {
// Declare the filename we will be looking for
String filename = "/Users/crawfordcrenshaw/Documents/garage.txt";
FileInputStream stream; // the stream we will read from
try {
// Open the file. Reading right to left, we
// (1) create a FileInput Stream, pointing to the file;
// (2) assign that stream to the name 'stream'
stream = new FileInputStream(filename);
} catch (IOException e) {
System.out.println("File not found: " + filename);
return; // abort on error
}
Scanner scan = new Scanner(stream);
// Here the file was opened. Read in all the characters
// Connect a stream reader to the stream.
// read in all the characters until we hit end-of-file, which is
// indicated by a negative return value
while(scan.hasNext()){ //looks at every character
if (scan.nextByte() == 43) { //the plus character
garagearray[maxslot] = readlicense(scan);
maxslot++;
scan.nextLine();
} else if (scan.nextByte() == 45) {
String removal = readlicense(scan);
for (int i = 0; i < maxslot; ++i) {
if (garagearray[i].equals(removal)) {
garagearray[i] = "";
}
}
scan.nextLine();
} else {
scan.nextLine();
}
}
System.out.println("Here is your garage. Enjoy!");
for (int i = 0; i < maxslot; ++i) {
if (!garagearray[i].equals("")) {
System.out.println(garagearray[i]);
}
}
// close the input file
try {
stream.close();
} catch (IOException e) {
System.out.println("Error closing file: " + filename);
return; // abort on error
}
}
}
I get these exceptions:
Exception in thread "main" java.util.InputMismatchException: For input string: "+63092"
at java.util.Scanner.nextByte(Scanner.java:1812)
at java.util.Scanner.nextByte(Scanner.java:1766)
at garage.Garage.main(Garage.java:39)
Please help! Keep in mind I am a NEW programmer, so fancy lingo will not work.