【问题标题】:Writing files in java, changing case and number用java写文件,改变大小写和数字
【发布时间】:2018-10-16 05:56:31
【问题描述】:

我正在编写一个导入文本文件并通过执行以下操作创建输出文件的程序:

  • 将 input.txt 中的所有字母反转并显示在 output.txt 中。
  • 数字将显示为以连字符分隔的单词列表,表示数字中的数字。(例如 123 = 一 - 二 - 三)。
  • 还应将行号显示到控制台。

input.txt如下:

Here is A TEXT
File To Be
Processed FOR Lab09.
There are now 3459 ways to
DESIGN this kind of PROGRAM.
Here's hoping you can figure OUT 1, 2,
or 3 of these designs -
Or AT LEAST come close, even if
you don't find all 3459

我无法弄清楚 Stringschar 的转换以及 FileReader 以正确打印到控制台和文件。对这位新编码员的任何帮助将不胜感激。

import java.io.*;
import java.util.*;

/**
 * CSC110 Java 10am Lab09 Reading
 *
 * Writing Files file will create output file of the text formatted a certain way.
 */
public class Lab09 {

  public static void main(String[] args) throws FileNotFoundException, IOException {
    Scanner input = new Scanner(System.in);

    System.out.print("Enter name of file: ");
    String fileName = input.next();
    System.out.println();

    Scanner fileInput = new Scanner(new File(fileName));
    FileWriter fw = new FileWriter("output.txt");
    PrintWriter pw = new PrintWriter(fw);

    while (fileInput.hasNext()) {
      char c = fileInput.next().charAt(0);
      String numberToWord = "";
      pw.println(fileInput.nextLine().toUpperCase());

      if (Character.isLowerCase(c)) {
        System.out.println(fileInput.nextLine().toUpperCase());
        pw.println(fileInput.nextLine().toUpperCase());
      } else if (Character.isUpperCase(c)) {
        System.out.println(fileInput.nextLine().toLowerCase());

        if (Character.isDigit(c)) {
          switch (c) {
            case '1':
              numberToWord = numberToWord + "one";
              pw.println(fileInput.next(numberToWord));
              System.out.println(numberToWord);
              break;

            case '2':
              numberToWord = numberToWord + "two";
              pw.println(fileInput.next(numberToWord));
              System.out.println(numberToWord);
              break;

            case '3':
              numberToWord = numberToWord + "three";
              pw.println(fileInput.next(numberToWord));
              System.out.println(numberToWord);
              break;

            case '4':
              numberToWord = numberToWord + "four";
              pw.println(fileInput.next(numberToWord));
              System.out.println(numberToWord);
              break;

            case '5':
              numberToWord = numberToWord + "five";
              pw.println(fileInput.next(numberToWord));
              System.out.println(numberToWord);
              break;

            case '6':
              numberToWord = numberToWord + "six";
              pw.println(fileInput.next(numberToWord));
              System.out.println(numberToWord);
              break;
          }
        }
      }
    }

    pw.close();
    fileInput.close();
  }
}

抱歉,很难看!我只是需要一些指导!非常感谢任何帮助。

【问题讨论】:

  • 字符串到字符? "字符串".toCharArray();或类似的就足够了。无论哪种方式,我都建议摆脱重复的代码,这只会使重新读取变得更加困难

标签: java java-io


【解决方案1】:

每个数字都有一个数组

String nums [] = {"zero", "one", "two"};  // etc

然后测试char是否为数字,如果是则减去'0'的ascii

char c = '2';

if (c >= '0' && c <= '9') {
    int index = c - '0';
    System.out.println(nums [index]);
}

【讨论】:

    猜你喜欢
    • 2014-11-01
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 2016-03-11
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    相关资源
    最近更新 更多