import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.Arrays;
import java.util.NoSuchElementException;
public class ReservationBook
{
private Reservation[] data;
public Reservation[] readFile(String filename) throws IOException
{
FileReader reader = new FileReader(filename);
try
{
Scanner in = new Scanner(reader);
boolean done = false;
while(!done)
{
try
{
int numberofR = in.nextInt();
data = new Reservation[numberofR];
String line = in.nextLine();
Scanner lineScan = new Scanner(line);
for(int i =0;i < numberofR;i++)
{
String f = lineScan.nextLine();
String l = lineScan.nextLine();
String a = lineScan.nextLine();
int lpn = lineScan.nextInt();
String dt = lineScan.nextLine();
int nd = lineScan.nextInt();
String tp = lineScan.nextLine();
data[i] = new Reservation(f,l,a,lpn,dt,nd,tp);
}
}
catch(NoSuchElementException exception)
{
done = true;
}
}
}
finally
{
reader.close();
}
return data;
}
//sort the data into correct order using the Insertion sort
public void Sort()
{
for(int i =1;i<data.length;i++)
{
String next = data[i].getName();
int j = i;
while(j>0 && data[j-1].getName().compareTo(next) > 0)
{
data[j].getName().equals(data[j-1].getName());
j--;
}
data[j].getName().equals(next);
}
}
public String search(String n)
{
int low = 0;
int high = data.length -1;
while(low <= high)
{
int mid = (low+high) / 2;
if(data[mid].getName().compareTo(n)==0)
return "Customer info: " + data[mid].toString();
else if(data[mid].getName().compareTo(n) < 0)
low = mid +1;
else
high = mid -1;
}
return "Customer information not found";
}
}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class ReservationReader
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
ReservationBook book = new ReservationBook();
boolean done = false;
while(!done)
{
try
{
System.out.println("Please enter the file name: ");
String filename = in.next();
book.readFile(filename);
book.Sort();
System.out.println("Enter the name you want to search: ");
String n = in.nextLine();
book.search(n);
done = true;
}
catch(FileNotFoundException exception)
{
System.out.println("File not found");
}
catch(IOException exception)
{
exception.printStackTrace();
}
}
}
}
There are some problems .
typescript:
Please enter the file name:
reservationlist
Exception in thread "main" java.lang.NullPointerException
at ReservationBook.Sort(ReservationBook.java:54)
at ReservationReader.main(ReservationReader.java:19)
reservationlist textfile:
1
ertert
sdfsdf
sdf
123
sf
123
service
THanks.