如何根据用户输入创建输入和输出文件? [英] How to create an input and output file based on user input?

查看:45
本文介绍了如何根据用户输入创建输入和输出文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入文件 DATA1.txt 将包含 7 行,每行一个正整数,描述每种类型的糖果数量.输出文件 OUT1.txt 将包含与输入的数字相同数量的星星.IE:输入 DATA1.txt 会显示:

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

输出 OUT1.txt 将显示:

Output OUT1.txt would show:

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

当我运行我的代码时,它只创建一个文件 OUT1.txt 但它有数字而不是星星.我怎么会有 DATA1.txt 呢?我不太明白这一点,因为我还没有了解输入或输出文件.任何帮助将不胜感激.我会在我的代码中修改什么?

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

推荐答案

您是否事先创建了任何文件?如果我在没有预制文件的情况下运行它,我会输入七个数字和测试"的文件名.程序停止于:

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(系统找不到指定的文件)

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

我现在看到 test.txt 是用 7 个数字创建的.这是第一次运行,我之前没有手动创建文件.OUT1.txt 未创建.

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.

当文件 OUT1.txt 不存在时,如何运行 while 循环?

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.

代码:(我已经评论了更改)

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

这篇关于如何根据用户输入创建输入和输出文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆