Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program should be in java Create a linked list using the Node class that con

ID: 3699108 • Letter: T

Question

The program should be in java

Create a linked list using the Node class that contains the first million prime numbers (primes4.txt). It should have a menu with these options:

1 = Search for a Number (let the user enter a number to search for)
2 = Add a new Number (let the user enter a number and add it to the head of the list)
3 = Delete a Number (let the user enter a number and delete it if found)
4 = Exit

I have attached the code below but there are errors is it.

import java.util.*;

import java.io.*;

public class linked

{

public static void main (String [] args) throws IOException

{

int userChoice;

Scanner input = new Scanner(System.in);

/*********************************************************/

File file = new File("primes4.txt");

Scanner infile = new Scanner(file);

Node Head = null, Previous = null, Temp = null;

int Count = 0;

while (infile.hasNextInt())

{

Count++;

Temp = new Node();

Temp.Data = infile.nextInt();

Temp.Next = Head;

Head = Temp;

}

System.out.println("Finished reading " + Count + " Prime numbers ");

int selection = 0;

while (selection != 4)

{

/***************************************************/

System.out.println("Choose from these choices");

System.out.println("------------------------- ");

System.out.println("1 - Search for a Number");

System.out.println("2 - Add a new Number");

System.out.println("3 - Delete a Number");

System.out.println("4 - Quit");

selection = input.nextInt();

if (selection == 1)

{

int Search = 998;  

Temp = Head;

while (Temp != null && Temp.Data != Search)

{

Temp = Temp.Next;

}

if (Temp == null)

System.out.println(Search + "Not found");

else

System.out.println(Search + "Found");

}

else if (selection == 2)

{

// add new numbers

int NewNumber = 1000;

Temp = new Node();

Temp.Data = NewNumber;

Temp.Next = Head;

Head = Temp;

}

else if (selection == 3)

{

// delete number

int DeleteNumber = 997;

Temp = Head;

while (Temp != null && Temp.Data != DeleteNumber)

{

Previous = Temp;

Temp = Temp.Next;

}

if (Temp == null)

System.out.println(DeleteNumber + " Not found ");

else

{

System.out.println(DeleteNumber + " Deleted ");

}

}

}

}

}

class Node

{

int Data;

Node Next;

}

Explanation / Answer

HERE IS THE CODE WITH SOME CORRECTION

import java.util.*;

import java.io.*;

class LinkedList

{

public static void main (String[] args) throws IOException

{

/*********************************************************/

File file = new File("primes4.txt");

Scanner infile = new Scanner(file);

Scanner in = new Scanner(System.in);

Node Head = null;

Node Temp, Previous;

int Count = 0;

int A;

while (infile.hasNextInt())

{

A = infile.nextInt();

Count++;

Temp = new Node();

Temp.Data = A;

Temp.Next = Head;

Head = Temp;

}

System.out.println("Finished reading " + Count + " Prime numbers ");

int Choice = 0;

while (Choice != 4)

{

/***************************************************/

System.out.println("Enter your choices");

Choice = in.nextInt();

if(Choice == 1)

System.out.println("Enter number to search for:");

A= in.nextInt();

Temp = Head;

while(Temp!=null && Temp.Data!= A)

{

Temp=Temp.Next;

}

if(Temp == null)

System.out.println("Your Number was not found");

else

System.out.println("your number was found");

if(Choice == 2)

System.out.println("Add");

A=in.nextInt();

Temp = Head;

while(Temp!= null && Temp.Data!= A)

{

Temp=Temp.Next;

}

if(Temp == null)

System.out.println("Your Number was not found");

else

System.out.println("Your Number was found");

if(Choice == 3)

System.out.println("Delete");

A=in.nextInt();

Temp = Head;

while(Temp!= null && Temp.Data!= A)

{

Previous=Temp;

Temp=Temp.Next;

Previous.Next=Temp.Next;

}

if(Temp == null)

System.out.println("Your Number was not found");

else

{

System.out.println("Your Number was Deleted");

}

}

}

}

class Node

{

int Data;

Node Next;

Node()

{

Next=null;

}

}