Hello, I am stuck with this code when I enter the option number 1 and it gives m
ID: 3864466 • Letter: H
Question
Hello, I am stuck with this code when I enter the option number 1 and it gives me this error
Exception in thread "main" java.lang.NullPointerException
at ListExample.insertInSkip(ListExample.java:97)
at ListExample.readFile(ListExample.java:154)
at ListExample.run(ListExample.java:208)
at ListExample.main(ListExample.java:237)
Here is the code (It has two classes)
ListExample.java
import java.io.File;
import java.util.Scanner;
import java.io.*;
public class ListExample
{
Node head;
Node tail;
private Scanner scanner;
public ListExample()
{
head = null;
}
public ListExample(Node head)
{
this.head = head;
}
//Load Database
public void Database()
{
head = null;
Node tmp = null; //problem here?
Node lastFirst = tmp;
Node lastSecond = tmp;
Node lastThird = tmp;
for(int i = 9;i >= 0; i--)
{
String L1 = Integer.toString(i);
for(int j = 9;j >= 0; j--)
{
String L2 = Integer.toString(j);
for(int k = 9;k >= 0; k--)
{
String L3 = Integer.toString(k);
String allNum = L1 + L2 + L3 + "000000";
tmp = new Node(allNum);
tmp.third = lastThird;
tmp.fourth = lastThird;
lastThird = tmp;
}
tmp.second = lastSecond;
lastSecond = tmp;
}
tmp.first = lastFirst;
lastFirst = tmp;
}
head = tmp;
}
// SkipSearch method
public void skipSearch(String SSN)
{
Node tmp = head;
while((tmp.first != null) && (SSN.charAt(0) < tmp.first.SSN.charAt(0)))
{
tmp = tmp.first;
}
while((tmp.second != null) && SSN.charAt(0) < tmp.second.SSN.charAt(0))
{
tmp = tmp.second;
}
while((tmp.third != null) && SSN.charAt(0) < tmp.third.SSN.charAt(0))
{
tmp = tmp.third;
}
while((tmp.fourth != null) && SSN.charAt(0) < tmp.fourth.SSN.charAt(0))
{
tmp = tmp.fourth;
}
while(tmp.fourth != null)
{
tmp = tmp.fourth;
if(tmp.SSN.equals(SSN))
{
System.out.println(tmp.toString()+"has been found.");
endTimer();
System.out.println("Search took: " + timeElapsed()+ " seconds");
}
}
}
//Inserting Skip Search
public void insertInSkip(String SSN, String lName, String fName)
{
Node tmp = head;
while((tmp != null) && SSN.charAt(0) >= tmp.first.SSN.charAt(0) )
{
tmp = tmp.first;
}
while((tmp != null) && SSN.charAt(1) >= tmp.second.SSN.charAt(1) )
{
tmp = tmp.second;
}
while((tmp != null) && SSN.charAt(2) >= tmp.third.SSN.charAt(2) )
{
tmp = tmp.third;
}
System.out.println(SSN);
System.out.println(tmp.fourth.SSN);
System.out.println(SSN.compareTo(tmp.fourth.SSN));
while((tmp != null) && (SSN.compareTo(tmp.fourth.SSN) > 0))
{
tmp = tmp.fourth;
}
if(SSN.compareTo(tmp.SSN) == 0)
{
return;
}
else
{
Node temp = new Node(SSN, fName, lName);
temp.fourth = tmp.fourth;
tmp.fourth = temp;
}
endTimer();
}
// Read the file Database.txt
public void readFile(String data)
{
startTimer();
File file = new File(data);
String firstName = " ";
int count = 0;
try
{
Scanner scanner = new Scanner(file);
while(scanner.useDelimiter(";") != null && scanner.hasNext())
{
String ssn = scanner.next();
String lastN = scanner.next();
if(scanner.useDelimiter(" ") != null)
{
scanner.skip(";");
String firstN = scanner.next();
firstName = firstN;
if(scanner.hasNext())
{
scanner.skip(" ");
scanner.skip(" ");
}
}
insertInSkip(ssn, firstName, lastN);
count++;
if(count % 1000 == 0)
{
System.out.print(".");
}
}
scanner.close();
endTimer();
System.out.println("Data loaded in " + timeElapsed() + " secs.");
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
}
// End of program
public void endProg()
{
System.out.println("Program exiting..");
System.exit(0);
}
public long startTimer()
{
return System.currentTimeMillis();
}
public long endTimer()
{
return System.currentTimeMillis();
}
public float timeElapsed()
{
return (endTimer() - startTimer()) / 1000;
}
//run the program
public void run(ListExample list)
{
scanner = new Scanner(System.in);
int option;
System.out.println("Select an option: ");
System.out.println("1. Load database 2. Skip Search 3. Exit ");
option = scanner.nextInt();
while(true)
{
switch(option)
{
case 1:
list.Database();
list.readFile("Database.txt");
break;
case 2:
System.out.println("Enter SSN: ");
String ssn2 = scanner.next();
list.skipSearch(ssn2);
break;
case 3:
endProg();
break;
default:
System.out.println("Incorrect value entered. Please enter a number between 1 and 3.");
System.out.println("Select an option: ");
System.out.println("1. Load database 2. Skip Search 3. Exit ");
option = scanner.nextInt();
break;
}
System.out.println("Select an option: ");
System.out.println("1. Load database 2. Skip Search 3. Exit ");
option = scanner.nextInt();
}
}
public static void main(String[] args) {
ListExample list = new ListExample();
list.run(list);
}
}
Node.java
public class Node {
public String SSN, fName, lName, allNum;
public Node first;
public Node second;
public Node third;
public Node fourth;
public Node head, tail, next;
public Node(String allNum){
this.allNum = allNum;
}
public Node(String SSN, String fName, String lName){
this.SSN = SSN;
this.fName = fName;
this.lName = lName;
}
}
The Database.txt should look like this
510421600;Shelley;Morgan
790701850;Holton;Jose
932371897;Hynes;Naomi
714797789;Kunkel;Dylan
878566780;Grisham;Ellie
810639750;Childs;Lillian
801417178;Gunn;Noah
609545138;Stratton;Katelyn
554961600;Lynch;Taylor
114850785;Ochs;Kylie
752334056;Ratcliff;Gabriel
743773137;Whitworth;Zachary
325511335;Tillman;Megan
507762588;Robledo;Tanner
396955663;Mclendon;Lauren
202613763;Friday;Lucas
294299672;Thornton;Amelia
180439889;Bayne;Blake
972360457;Singleton;Maria
970202921;Paz;Allison
158744801;Musgrove;Tristan
251227559;Good;Zoe
664219727;Stepp;Madison
286302958;Sorensen;Ryan
747848533;August;Ethan
130758466;Flanigan;Alexander
497483346;Blythe;Molly
666325010;Crist;Marissa
473288099;Driggers;Jonathan
600577467;Kimball;Vanessa
507141362;Hansen;Liam
033084592;Dawson;Paige
492514666;Betancourt;Elizabeth
448735126;Schenk;Amanda
223088851;Choate;Jade
159601041;Cobbs;Adam
126186132;Dickens;Jocelyn
303556063;Bingham;Jacob
Explanation / Answer
package numericalComputatio; public class fibo { static double c = -0.618; // double c = [(1-sqrt(5))/2] = - 0.618 /** * Computes the fibonacci series * @param n * @return */ private static double fibo(int n){ if (n == 0) return 1; else if (n == 1) return c; else { double result = fibo(n - 1) + fibo(n - 2); return result; } } public static void main(String[] args) { int n = 0; double result = 0.0; double result1 = 1.000000000; if (args[0] != null) n = Integer.parseInt(args[0]); for(int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.