Java language homework I need help with. The second link at the bottom is the fi
ID: 3572551 • Letter: J
Question
Java language homework I need help with. The second link at the bottom is the file that has to be inputed into the program !!
1. 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
class Node
{
int Data;
Node Next;
// you may want to also create a constructor that takes the Data as a parameter
Node()
{
Next = null;
}
}
http://mypccourse.com/cosc2436/primes4.txt
Explanation / Answer
import java.util.*;
import java.io.*;
class Node
{
public static void main(String args[])
{
int ch;
String num;
try
{
// Open the file that is the first
FileInputStream fstream = new FileInputStream("primes4.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line and adding it to linked list.
LinkedList<String> linkList = new LinkedList<String>();
while ((strLine = br.readLine()) != null)
{
linkList.add(strLine);
}
System.out.println("Please select any option.");
System.out.println("Select 1 to search for a number.");
System.out.println("Select 2 to add a new number.");
System.out.println("Select 3 to delete a number");
System.out.println("Select 4 to exit.");
Scanner scan2 = new Scanner(System.in);
// switch case statement to select option.
ch = scan2.nextInt();
switch(ch)
{
case 1:
System.out.println("Please enter no. to search.");
num = scanInput();
search(linkList, num);
break;
case 2:
System.out.println("Please enter no. to add it to the head of list.");
num = scanInput();
add(linkList, num);
break;
case 3:
System.out.println("Please enter no. to delete it.");
num = scanInput();
delete(linkList, num);
break;
case 4:
break; //to quit
}
//Close the input stream
in.close();
//Modifying contents in file
FileWriter fw = new FileWriter("primes4.txt");
BufferedWriter out = new BufferedWriter(fw);
for(String s : linkList)
{
out.write(s);
out.write(" ");
out.flush();
}
out.close();
}
catch (Exception e)
{//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
//scaning input
public static String scanInput()
{
Scanner scan = new Scanner(System.in);
return scan.nextLine();
}
//searching number
public static void search(LinkedList<String> list, String num)
{
if(list.contains(num))
System.out.println("Number " + num + " is at " + list.indexOf(num) + " position.");
else
System.out.println("File doesn't contain this number");
}
//adding number to list
public static void add(LinkedList<String> list, String num)
{
list.addFirst(num);
}
//deleting number from list
public static void delete(LinkedList<String> list, String num)
{
if(list.contains(num))
list.remove(num);
else
System.out.println("File doesn't contain this number");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.