RollerGirlatHeart
RollerGirlatHeart

Reputation: 31

Print a multiline text document

I am having an issue with my code not printing out all of what is in a text document. My assignment is to take what is in a .txt file and put in a new .txt file I guess you could say "fancier."

This is the text file I am given.

Thomas Smith 3 2.25 44

Kim Johnson 2 55.60 35

John Doe 33 2.90 21

Karen Java 1 788.99 65

This is the "fancy" output I need(only It needs to output all of them).

The first name is: Thomas

The last name is: Smith

The total number of items bought is: 3

The customer's total is: 6.75

The customer's total rounded (cast) is: 6

The age of the customer is: 44

I think I have just been staring at it so long I'm just over looking it...

Scanner inFile = new Scanner(new FileReader("customer.txt"));
    
    double options;
    System.out.println("How would you like to input your data?\n1 Input information from customer.txt\n2 Input information from the keyboard.");
    options = console.nextDouble();
    
    
    if (options == 1){
        
        //Variable to store first name
        String firstName;
        //Variable to store last name
        String lastName;
                
        //Variable to store how many items bought
        int itemsBought;
        //Variable to store the price per item
        double itemPrice; 
        //Variable to store their age
        int age;
        
        
        while (inFile.hasNext()){
            
            
            //Gets the first name
            firstName = inFile.next();
            //Gets the last name
            lastName = inFile.next();
            
            //Gets number of items bought
            itemsBought = inFile.nextInt();
            //Gets the price per item
            itemPrice = inFile.nextDouble();
            // Gets their age
            age = inFile.nextInt(); 
            

            PrintWriter outFile = new PrintWriter("programOutputFile.txt");
        
            outFile.println("The customers first name is: " + firstName); 
            outFile.println("The customers last name is: " + lastName);
            outFile.println("The customer bought " + (int)itemsBought + " items.");
            outFile.println("The customers total is " + itemPrice);
            outFile.println("The total cost rounded " + (int)itemPrice);
            outFile.println("The customers age is: " + (int)age);

            
            outFile.close();
            
        }   
    
        
    }
    else if (options == 2) {
        
        String firstname;
        String lastname;
        int items = 0;
        double price = 0;
        int age1 = 0;
        
        int counter; //loop control variable
        counter = 0;
    
        int limit; //store the number of items
        System.out.print("Enter the number of entries you have "); //Line 1
        limit = console.nextInt(); //Line 2
        
        
        
    while (counter < limit) {
        
        // It is asking for the user to input their first name 
         
                    System.out.println("Please tell me what is your first name? ");
                    firstname = console.next();
                    
                    // It is asking for the user to input their last name
                    System.out.println("What is your last name? ");
                    lastname = console.next();
                    
                    // It is asking for the number of items they purchased
                    System.out.println("How many items did your purchase? ");
                    items = console.nextInt();
                    
                    // Here it is asking for the total price they paid

                    System.out.println("What was the cost of each item? ");
                    price = console.nextDouble();
                    
                    System.out.println("How old are you? ");
                    age1 = console.nextInt();
                    
                    double total = items * price;
        
            
        counter++;
            
        if (counter != 0){
            //Outputs the length of Firstname
            System.out.println("The name is " + firstname.length() + " letters in your first name.");
                            
            //Outputs the first letter of LastName
            System.out.println("The first letter of your last name is: " + lastname.charAt(0));
        
            //Outputs the number of items bought
            System.out.println("You bought " + items + " items.");
            
            //Outputs Price
            System.out.println("Your total price was " + total);
            
            //Outputs the Price as a int number
            System.out.println("Your total price rounded is " + (int)total);
            
            //Outputs the age
            System.out.println("They are " + age1 + " years old.");
            
            
            PrintWriter outFile = new PrintWriter("programOutputFile.txt");
            
            //Outputs the information given above into a .txt file
            outFile.println("The customers first name is: " + firstname); 
            outFile.println("The customers last name is: " + lastname);
            outFile.println("The customer bought " + (int)items + " items.");
            outFile.println("The customers total is " + total);
            outFile.println("The total cost rounded " + (int)total);
            outFile.println("The customers age is: " + (int)age1);
            
            outFile.close();
        
                
            
        }
        else
            System.out.println("Invalid. Please try again.");
    
    
            
        }
    
    
    }
    
    
    inFile.close();
    
    
    
    
}

As of right now it will print out Karen Java's line instead of Karen, John, Kim, and Thomas's. I have option 2 finished but again I am having the same problem of it only prints out the last input.

Any advise would be greatly appreciated!

Upvotes: 2

Views: 107

Answers (1)

vandale
vandale

Reputation: 3650

You reopen the file in your loop each time you write. This has the effect of overwrite any previous contents of the file (the previous output you just wrote). declare outfile before the while loop and close it after.

Upvotes: 2

Related Questions