How to create an input and output file based on user input? [duplicate]

最后都变了- 提交于 2019-12-13 07:48:35

问题


The input file DATA1.txt will contain 7 lines, each a single positive integer describing the amount of candies of each type. The output file OUT1.txt will contain the same number of stars as the number entered. i.e: Input DATA1.txt would show:

12
8
10
5
20
3
7

Output OUT1.txt would show:

12: ************
8: ********
10: **********
5: *****
20: ********************
3: ***
7: *******

When I run my code, it only creates one file which is OUT1.txt but it has the numbers and not the stars. How would I have a DATA1.txt too? I dont quite understand this since i have not learned about input or output files. Any help would be greatly appreciated. What would i modify in my code?

import javax.swing.*;
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class CandyLand{
    public static void main(String str[]){
        int num1,num2,num3,num4,num5,num6,num7;

        String input1 = JOptionPane.showInputDialog("Please, enter the number of candies in the first pile");
        num1 = Integer.parseInt(input1);
        String input2 = JOptionPane.showInputDialog("Please, enter the number of candies in the second pile");
        num2 = Integer.parseInt(input2);
        String input3 = JOptionPane.showInputDialog("Please, enter the number of candies in the third pile");
        num3 = Integer.parseInt(input3);
        String input4 = JOptionPane.showInputDialog("Please, enter the number of candies in the fourth pile");
        num4 = Integer.parseInt(input4);
        String input5 = JOptionPane.showInputDialog("Please, enter the number of candies in the fifth pile");
        num5 = Integer.parseInt(input5);
        String input6 = JOptionPane.showInputDialog("Please, enter the number of candies in the sixth pile");
        num6 = Integer.parseInt(input6);
        String input7 = JOptionPane.showInputDialog("Please, enter the number of candies in the seventh pile");
        num7 = Integer.parseInt(input7);

        String fileName = JOptionPane.showInputDialog("Please, enter the name of the file");
        try{

            DataOutput f = new DataOutputStream(new FileOutputStream(fileName +".txt"));
            f.writeBytes(String.valueOf(num1));
            f.writeBytes("\r\n");
            f.writeBytes(String.valueOf(num2));
            f.writeBytes("\r\n");
            f.writeBytes(String.valueOf(num3));
            f.writeBytes("\r\n");
            f.writeBytes(String.valueOf(num4));
            f.writeBytes("\r\n");
            f.writeBytes(String.valueOf(num5));
            f.writeBytes("\r\n");
            f.writeBytes(String.valueOf(num6));
            f.writeBytes("\r\n");
            f.writeBytes(String.valueOf(num7));
        }
        catch(Exception e){
            String msg = e.toString();
            System.out.println(msg);
        }

        Scanner file = null;
        ArrayList<Integer> list = new ArrayList<Integer>();

        try {
            file = new Scanner(new File("OUT1.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        while(file.hasNext()){
            if (file.hasNextInt()) list.add(file.nextInt());
            else file.next();
        }   
        for (Integer j: list){
             System.out.print(j + ": ");
             for(int i=1; i<=j; i++){
                System.out.print("* ");
             }
             System.out.println("");
        }
    }
}

回答1:


Are you creating any of the files beforehand? If I run this with no files premade, I enter seven numbers and file name of "test". The program stops at:

java.io.FileNotFoundException: OUT1.txt (The system cannot find the file specified)

I now see that test.txt was created with the 7 numbers inside. This was on the first run with no files created manually by me before. OUT1.txt was not created.

How can you run the while loop when file OUT1.txt does not exist?

while(file.hasNext()){
    if (file.hasNextInt())
        list.add(file.nextInt());
    else file.next();
}

I am working on fixing the program which I will edit in when I'm done, but I want you to understand first.

Code: (I have commented the changes)

import javax.swing.*;
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class CandyLand{
   public static void main(String str[]){
      int num1,num2,num3,num4,num5,num6,num7;

      String input1 = JOptionPane.showInputDialog("Please, enter the number of candies in the first pile");
      num1 = Integer.parseInt(input1);
      String input2 = JOptionPane.showInputDialog("Please, enter the number of candies in the second pile");
      num2 = Integer.parseInt(input2);
      String input3 = JOptionPane.showInputDialog("Please, enter the number of candies in the third pile");
      num3 = Integer.parseInt(input3);
      String input4 = JOptionPane.showInputDialog("Please, enter the number of candies in the fourth pile");
      num4 = Integer.parseInt(input4);
      String input5 = JOptionPane.showInputDialog("Please, enter the number of candies in the fifth pile");
      num5 = Integer.parseInt(input5);
      String input6 = JOptionPane.showInputDialog("Please, enter the number of candies in the sixth pile");
      num6 = Integer.parseInt(input6);
      String input7 = JOptionPane.showInputDialog("Please, enter the number of candies in the seventh pile");
      num7 = Integer.parseInt(input7);

      String fileName = JOptionPane.showInputDialog("Please, enter the name of the file");
      try{
         DataOutput f = new DataOutputStream(new FileOutputStream(fileName + ".txt"));
         f.writeBytes(String.valueOf(num1));
         f.writeBytes("\r\n");
         f.writeBytes(String.valueOf(num2));
         f.writeBytes("\r\n");
         f.writeBytes(String.valueOf(num3));
         f.writeBytes("\r\n");
         f.writeBytes(String.valueOf(num4));
         f.writeBytes("\r\n");
         f.writeBytes(String.valueOf(num5));
         f.writeBytes("\r\n");
         f.writeBytes(String.valueOf(num6));
         f.writeBytes("\r\n");
         f.writeBytes(String.valueOf(num7));
      }
      catch(Exception e){
         String msg = e.toString();
         System.out.println(msg);
      }

      Scanner file = null;
      ArrayList<Integer> list = new ArrayList<Integer>();

      try {
        //You should be reading from the first file you created, not OUT1.txt
         file = new Scanner(new File(fileName + ".txt"));
      } 
      catch (FileNotFoundException e) {
         e.printStackTrace();
      }

      while(file.hasNext()){
         if (file.hasNextInt())
            list.add(file.nextInt());
         else file.next();
      }
      try {
        //Instead using System.out.println, use DataOutput like you were using before
         DataOutput f2 = new DataOutputStream(new FileOutputStream("OUT1.txt"));
         for (Integer j: list){
            f2.writeBytes(j + ": ");
            for(int i=1; i<=j; i++){
               f2.writeBytes("* ");
            }
            f2.writeBytes("\r\n");
         }
      }
      catch(Exception e){
         e.printStackTrace();
      }
   }
}


来源:https://stackoverflow.com/questions/34345427/how-to-create-an-input-and-output-file-based-on-user-input

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!