首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用java编写文件,更改大小写和编号

用java编写文件,更改大小写和编号
EN

Stack Overflow用户
提问于 2018-10-16 13:56:31
回答 1查看 144关注 0票数 -1

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

在output.txt.

  • Digits中显示的
  • input.txt中所有字母的大小写颠倒,将以连字符分隔的单词列表显示数字中的数字。(例如,123 =1-2-3)。
  • 还应在控制台中显示行号。

input.txt如下:

代码语言:javascript
复制
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

我不知道如何使用FileReaderStrings转换为char,以便正确打印到控制台和文件。对这个新的编码器的任何帮助都将不胜感激。

代码语言:javascript
复制
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();
  }
}

对不起,这很难看!我只是需要一些指导!任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-16 14:07:01

每个数字都有一个数组

代码语言:javascript
复制
String nums [] = {"zero", "one", "two"};  // etc

然后测试字符是否是一个数字,如果是,则减去'0‘的ascii。

代码语言:javascript
复制
char c = '2';

if (c >= '0' && c <= '9') {
    int index = c - '0';
    System.out.println(nums [index]);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52828749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档