java - 打印多行文本文档

标签 java

我的代码存在问题,无法打印文本文档中的所有内容。我的任务是获取 .txt 文件中的内容并放入一个新的 .txt 文件中,我想您可能会说“更喜欢”。

这是我得到的文本文件。

Thomas Smith 3 2.25 44

Kim Johnson 2 55.60 35

John Doe 33 2.90 21

Karen Java 1 788.99 65

这是我需要的“奇特”输出(仅需要输出所有这些)。

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

我想我只是盯着它看了太久,我只是忽略了它......

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();
    
    
    
    
}

截至目前,它将打印 Karen Java 的行,而不是 Karen、John、Kim 和 Thomas 的行。我已经完成了选项 2,但我再次遇到了同样的问题,它只打印出最后一个输入。

如有任何建议,我们将不胜感激!

最佳答案

每次写入时,您都会在循环中重新打开该文件。这会覆盖文件以前的任何内容(您刚刚编写的以前的输出)。在 while 循环之前声明 outfile 并在之后关闭它。

关于java - 打印多行文本文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22392933/

相关文章:

java - 是否可以在java类文件中存储字节数组(原始数据)?

java - 玩! + 锡耶纳 + GAE + JUnit

java - 当我在其他 Activity 中使用 mqtt 客户端时,他们向我显示错误

java - 无法部署到 GAE - .jsp 文件无法编译

java - Elasticsearch 嵌套映射查询不起作用 - JAVA?

java - 通过 ObjectInputStream 反序列化时出现 StreamCorruptedException

java - 使用 CountDownTimer 方法暂停循环,直到方法完成

Java:列出方法中使用的字段

java - Android Retrofit连接JAVA套接字

java - 冰上滑动拼图寻路