package project5;
import java.util.Scanner;
import java.util.*;
import java.io.*;
import java.awt.Component;
import javax.swing.JOptionPane;
// Zach Krizan
// Java Program 5
public class Main {
/**********************************************************
* The purpose of a dummy node is to prevent errors when you
* might have an empty list, or when you get an error trying to
* do something at the beginning of the list.
* This program will ask the user for a number, and enter
* it into a list. The user will keep getting an option to
* add to the list until the user chooses to stop.
* Then the user will be asked to choose a number thats on
* the list. If they choose an incorrect number, their will
* be an error checker. If they choose correctly, the will
* be asked if they want to delete the number.
* Then the final list will be displayed.
**********************************************************/
static void printList(ListNode start) {
// Prints the items in the list to which start points.
// They are printed on one line, separated by spaces.
ListNode runner; // For running along the list.
runner = start;
String tempList = "";
// This while loop runs through the entire list.
while (runner != null) {
JOptionPane.showMessageDialog(null, " " + runner.item);
runner = runner.next;
}
} // end printList()
//method numCheck
static boolean numCheck(ListNode start2, String checker) {
// This method checks to see if the number is on the list or not.
// If it is not on the list the boolean errorCheck will be false.
// If it is on the list the boolean errorCheck will be true.
ListNode runner2 = new ListNode(); // A new node to add to the list.
runner2 = start2;
boolean errorCheck = false;
// This while loop will run through the entire list.
while (runner2 != null) {
if (runner2.item.equals(checker)) {
errorCheck = true;
}
runner2 = runner2.next;
}
return errorCheck;
}
//method setNum
static String setNum(String methodNum) {
// This method will ask the number they want to search the list for.
// Then it will set that number equal to tempNum, and return tempNum.
methodNum = JOptionPane.showInputDialog("Enter a number to search"
+ " the list for: ");
//search for the number
String tempNum = methodNum;
return tempNum;
}
//deleteNum
static void deleteNum(ListNode start3, String theNum) {
// This method will only be used if a number on the list is deleted.
// It will print the new list without the deleted number.
// Prints the items in the list to which start points.
// They are printed on one line, separated by spaces.
ListNode runner3 = new ListNode(); // For running along the list.
runner3 = start3;
// This while loop will run through the entire list.
while (runner3 != null) {
if (runner3.next != null) {
if (runner3.next.item.equals(theNum)) //Whenever the number in the list is found use this if statement
{
removeAfter(runner3);
}
}
if (runner3.item.equals(theNum)) {
removeBeg(runner3);
}
JOptionPane.showMessageDialog(null, " " + runner3.item);
// prints list.
runner3 = runner3.next;
}
System.out.println();
}
//method removeAfter()
static void removeAfter(ListNode temp) {
//This will delete list.next
ListNode obsoleteNode;
obsoleteNode = temp.next;
temp.next = temp.next.next;
obsoleteNode = null;
//list.next turns into list.next.next
}
//method removeBeg
static void removeBeg(ListNode l) { // remove first node
ListNode obsoleteNode2;
obsoleteNode2 = l;
obsoleteNode2.item = l.item;
l.item = l.next.item; // point past deleted node
obsoleteNode2 = null;
//This will make the beginning node = to the second node
ListNode obsoleteNode3;
obsoleteNode3 = l.next;
l.next = l.next.next;
obsoleteNode3 = null;
//runs same code to take out second node
}
/****************************************
*
* Main Method
*
****************************************/
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "You are now running Program 5");
ListNode list = null; // A list, initially empty.
int counter = 0; // counts how long the list is.
String num = "";
String str = "yes";
//This is to create the dummy node
ListNode dummy = new ListNode();
dummy.item = "Dummy Node";
list = dummy;
JOptionPane.showMessageDialog(null, "Dummy Node Created");
while (str.equals("yes")) {
// This while loop will go as long as the user wants to keep
// entering numbers in the list.
num = JOptionPane.showInputDialog("Enter a number to put "
+ "on the list: ");
ListNode head = new ListNode(); // A new node to add to the list.
head.item = num; // Makes the head.item = to the number the
// user entered
//the dummy nodes are head and tail
//tail
//while (temp.getNext() != tail) - instaed of notnull
//
head.next = list;
list = head;
String a = JOptionPane.showInputDialog("Would you"
+ "like to enter another number?"
+ "\nPlease enter 'yes' or 'no'");
str = a;
counter++;
if (str.equals("no")) {
JOptionPane.showMessageDialog(null, "Your final list is: ");
printList(list);
}
}
String part2num = "";
part2num = setNum(part2num);
// set Num will use a scanner to make part2num
// equal to whatever the user enters.
numCheck(list, part2num);
boolean EC; //made to error check
EC = numCheck(list, part2num);
//numCheck will check for the number
//This loop will give an error message if EC is false and have the
// user enter another number
while (EC == false) {
JOptionPane.showMessageDialog(null, "Error: the number you entered "
+ "is not in the list");
String again;
again = JOptionPane.showInputDialog("Would you like to enter another"
+ " number?");
if (again.equals("no")) // This if statement will run if the user choose "no" and
// did not want to continue
{
JOptionPane.showMessageDialog(null, "The final list is: ");
printList(list);
JOptionPane.showMessageDialog(null, "Program will now end");
System.exit(0);
}
part2num = setNum(part2num); //sets the number for the second part
numCheck(list, part2num); //checks the number to see if it exists
EC = numCheck(list, part2num); //makes EC equal to true or false
}
JOptionPane.showMessageDialog(null, "Found Number");
String wishDelete = "";
wishDelete = JOptionPane.showInputDialog("Do you wish to Delete it?"
+ "\nPlease enter 'yes' or 'no'");
if (wishDelete.equals("yes")) {
JOptionPane.showMessageDialog(null, "Deleting");
JOptionPane.showMessageDialog(null, "The final list is:");
deleteNum(list, part2num); //print list with deleted number
JOptionPane.showMessageDialog(null, "The program is now finished");
}
if (wishDelete.equals("no")) //This if statement will be user if the user did not want to
// delete the chosen number
{
JOptionPane.showMessageDialog(null, "The entry will not be "
+ "deleted");
JOptionPane.showMessageDialog(null, "The final list is:");
printList(list);
}
}
}
///////////////////////////////////////////////////////////
*
*
*Page2
*
*
*/////////////////////////////////////////////////////////
package project5;
public class ListNode
{
String item; // An item in the list.
ListNode next; // Pointer to the next node in the list.
}
No comments:
Post a Comment