Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Please help with output from Data File!

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help with output from Data File!

    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
    Last edited by cocominta; May 7th, 2014 at 10:00 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Please help with output from Data File!

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Please post the program's output and add some comments showing what it should be.

    Also post a list of what the user's responses must be to execute the code and show the problem.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 19
    Last Post: March 15th, 2013, 03:11 PM
  2. counting the values in input file and and writing the output to a file
    By srujirao in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 8th, 2012, 02:48 PM
  3. Output some data from a txt file?
    By j4ze in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: April 24th, 2012, 07:20 AM
  4. Replies: 1
    Last Post: April 7th, 2011, 01:32 PM
  5. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM

Tags for this Thread