java - 用java写文件,改变大小写和数字
问题描述
我正在编写一个导入文本文件并通过执行以下操作创建输出文件的程序:
- 反转 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
我无法弄清楚Strings
tochar
与 a的转换以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();
}
}
对不起,它很难看!我只是需要一些指导!任何帮助是极大的赞赏。
解决方案
每个数字都有一个数组
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]);
}
推荐阅读
- monitoring - 无接口的 Zabbix Web 监控
- coldfusion - cfhttp 返回浏览器可以通过 http 和 https 访问的网络 url 的连接失败
- c# - 您的应用已使用来自 Google Cloud SDK 的最终用户凭据进行身份验证
- matlab - 当我使用积分函数时,Matlab 没有给出结果
- c# - 我怎样才能给图像一个八角形?
- python - Python中子列表的字母顺序组织
- powershell - 从 run.exe 运行 powershell 命令
- c# - 运行单元测试 (UWP) 时应用程序启动
- node.js - 如何从一系列承诺中获取价值?
- visual-studio - 用于 Visual Studio CRforVS_13_0_1 的 Crystal 也称为 crredist2008_x64