在 java 中写入文件,更改大小写和数字
Writing files in java, changing case and number
我正在编写一个程序,通过执行以下操作导入文本文件并创建输出文件:
- 反转 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
我无法通过 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();
}
}
抱歉,很难看!我只需要一些指导!非常感谢任何帮助。
每个数字都有一个数组
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]);
}
我正在编写一个程序,通过执行以下操作导入文本文件并创建输出文件:
- 反转 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
我无法通过 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();
}
}
抱歉,很难看!我只需要一些指导!非常感谢任何帮助。
每个数字都有一个数组
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]);
}