I cannot get the data file to print out correctly when I go to the display case. It only prints one name three times.
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.lang.*; import java.util.Scanner; import java.io.*; import java.text.*; import java.math.*; import java.util.StringTokenizer; public class Program5Final { static Scanner scan = new Scanner(System.in); // static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); static int num; static UnorderedArrayList stringList = new UnorderedArrayList(); static UnorderedArrayList tempList = new UnorderedArrayList(); static StringElement strObject = new StringElement(); static StringElement strOther = new StringElement(); public static void main(String[]args)throws IOException { System.out.println("############################################################################"); System.out.println("# The purpose of program five is to display, #"); System.out.println("# insert, delete, search, or quit a list of First Names and #"); System.out.println("# last names. The program is centered around being an ArrayList program #"); System.out.println("# This type of program has many extended classes. #"); System.out.println("# Users will be prompted to input different choices as they wish. #"); System.out.println("# This program is menu driven and is read from a data file #"); System.out.println("# #"); System.out.println("############################################################################"); do { System.out.println("######################################################"); System.out.println("# #"); System.out.println("# MENU #"); System.out.println("# 2. Display #"); System.out.println("# 3. Insert #"); System.out.println("# 4. Delete #"); System.out.println("# 5. Search #"); System.out.println("# 6. Quit #"); System.out.println("# #"); System.out.println("######################################################"); System.out.println(); System.out.println(" Please enter 1 or 2 to quit "); num = scan.nextInt(); switch(num) { case 1: Second(); break; case 2: Display(); break; case 3: Insert(); break; case 4: Delete(); break; case 5: Search(); break; case 6: System.out.println("Thank you for using this program!"); break; default: System.out.println("Oops! Please follow directions"); break; } } while(num!= 6); } public static void Second() { int n = stringList.listSize(); for(int i=0; i<n-1; i=i+2) { System.out.println(stringList.retrieveAt(i) + " " + stringList.retrieveAt(i+1)); } } public static void Display()throws IOException, FileNotFoundException { Scanner infile = new Scanner(new FileReader("G:\\DataFileDS.txt")); StringTokenizer token = new StringTokenizer(infile.nextLine()); StringElement str = new StringElement(); while(token.hasMoreTokens()) { str.setString(token.nextToken()); stringList.insert(str); int n = stringList.listSize(); for(int i=0; i<n-1; i=i+1) { System.out.println(stringList.retrieveAt(i) + " " + stringList.retrieveAt(i+1)); } stringList.print(); } } public static void Insert() { System.out.println("Enter the Name to be Inserted"); System.out.flush(); strObject.setString(scan.next()); strOther.setString(scan.next()); System.out.println(); stringList.insert(strObject); stringList.insert(strOther); System.out.println("The New List Is:"); stringList.print(); System.out.println(); } public static void Delete() { System.out.print("Enter the name that you would like deleted:"); System.out.flush(); strObject.setString(scan.next()); strOther.setString(scan.next()); System.out.println(); stringList.remove(strObject); stringList.remove(strOther); System.out.println(strObject + "The New List Is:"); stringList.print(); System.out.println(); } public static void Search() { System.out.print("Enter the name you would like to Search for "); System.out.flush(); strObject.setString(scan.next()); System.out.println(); if(stringList.seqSearch(strObject) != -1) System.out.println("The Name is in the list!"); else System.out.println("The Name is Not in the List!"); System.out.println(); } }
Output when I choose Case 1:
Minia
Minia Jane
Minia Jane