我正在编写一个导入文本文件并通过执行以下操作创建输出文件的程序:
输入.txt中所有字母的大小写相反,并显示在输出.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
我无法弄清楚使用FileReader将Strings转换为char以正确打印到控制台和文件的情况。此新编码器的任何帮助将不胜感激。
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();
}
}
对不起,很难看!我只需要一些指导!任何帮助是极大的赞赏。
编辑
我修复了代码并使它工作。这是已解决的问题。
导入java.io .;
导入java.util .;
公共课程Lab09 {
公共静态void main(String [] args)抛出FileNotFoundException,IOException {
字符串输入;
字符串altInput;
int lineCount = 0;
PrintWriter outFile =新的PrintWriter(" output.txt");
Scanner inFile = new Scanner(new File(" input.txt"));
while (inFile.hasNextLine()) {
input = inFile.nextLine();
lineCount++;
altInput ="";
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c >= 'A' && c <= 'Z')
altInput = altInput + ((char) (c + 32));
else if (c >= 'a' && c <= 'z')
altInput = altInput + ((char) (c - 32));
else if (c >= '0' && c <= '9') {
switch (c) {
case '0':
altInput +="zero";
break;
case '1':
altInput +="one";
break;
case '2':
altInput +="two";
break;
case '3':
altInput +="three";
break;
case '4':
altInput +="four";
break;
case '5':
altInput +="five";
break;
case '6':
altInput +="six";
break;
case '7':
altInput +="seven";
break;
case '8':
altInput +="eight";
break;
case '9':
altInput +="nine";
break;
}
if (i < input.length() - 1 && (input.charAt(i + 1) >= '0' && input.charAt(i + 1) <= '9'))
altInput +="-";
} else
altInput = altInput + input.charAt(i);
}
outFile.println(altInput);
System.out.println(altInput);
}
inFile.close();
outFile.close();
}
}
字符串为char?" String" .toCharArray(); 或类似的就足够了。 无论哪种方式,我都建议您删除重复的代码,这只会使重复操作更加困难
每个数字都有一个数组
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]);
}