如何更改输入文件中的所有字母并在输出文件中将它们打印为大写?

时间:2019-03-29 00:02:44

标签: java string

我需要询问用户输入和输出文件,然后将输入文件中的所有字母全部输出为大写。

我尝试创建不同的变量并弄乱char

 package programassignment;
 import java.util.Scanner;
 import java.io.*;

 /**
 *
 * @author bambo
 */
public class ProgramAssignment {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        Scanner keyboard = new Scanner (System.in);
        System.out.println("What is the name of the input file?");
        String inputfilename=keyboard.nextLine();
        File f = new File(inputfilename);
        Scanner inputFile = new Scanner(f);
        System.out.println("What is the name of the output file?");
        String outputfile=keyboard.nextLine();
         FileWriter fw = new FileWriter(outputfile);
         PrintWriter pw = new PrintWriter(fw);

          int lineNumber=0;
          String upper = Letter.toUppercase();

            while(inputFile.hasNext());
         {   
             lineNumber++;
             int letterCount = 0;


         String line = inputFile.nextLine();
             if (line.length () != 0)
                letterCount++;

              for(int i=0; i< line.length(); i++)

              {
                 if(char.upper);
                 {
                    char.toUpperCase();
                 }


             }

我希望输入文件在输出文件中将所有字母都打印为大写

2 个答案:

答案 0 :(得分:2)

您的代码包含许多缺陷,包括不关闭输出文件;用分号终止while的正文;没有明显的原因盘点线;不读台词;不将它们转换为大写;而不会写入您的输出。我将使用try-with-resources来确保适当关闭我的资源(即Scanner和输出)。我会使用PrintStream。可能看起来像这样,

Scanner keyboard = new Scanner(System.in);
System.out.println("What is the name of the input file?");
String inputfilename = keyboard.nextLine();
File f = new File(inputfilename);
System.out.println("What is the name of the output file?");
String outputfile = keyboard.nextLine();
try (Scanner inputFile = new Scanner(f);
            PrintStream ps = new PrintStream(new File(outputfile))) {
    while (inputFile.hasNextLine()) {
        ps.println(inputFile.nextLine().toUpperCase());
    }
}
  

好吧,如果不使用Try或Printstream怎么办?

应该 正在使用try;但是如果没有它,您将负责手动关闭资源。至于使用PrintWriter而不是PrintStream,请进行两次调用以进行写操作。一个用于行,第二个用于行分隔符。喜欢,

Scanner keyboard = new Scanner(System.in);
System.out.println("What is the name of the input file?");
String inputfilename = keyboard.nextLine();
File f = new File(inputfilename);
System.out.println("What is the name of the output file?");
String outputfile = keyboard.nextLine();
Scanner inputFile = new Scanner(f);
PrintWriter pw = new PrintWriter(new File(outputfile));
while (inputFile.hasNextLine()) {
    pw.write(inputFile.nextLine().toUpperCase());
    pw.write(System.lineSeparator());
}
pw.close();
inputFile.close();

答案 1 :(得分:1)

我看到您的代码有几个问题,主要问题是您从未关闭过扫描仪或文件编写器。这是我的简单解决方案。

import java.util.*;
import java.io.*;
public class StackOverflowHelp {
    public static void main(String args[])
    {
        Scanner keyboard = new Scanner (System.in);
        System.out.println("What is the name of the input file?");
        String inputfilename = keyboard.nextLine();
        keyboard.close();
        try
        {
            Scanner fileScanner = new Scanner(new File(inputfilename));
            FileWriter fileOut = new FileWriter("output.txt",true); 
            while(fileScanner.hasNextLine())
            {
                String temp = fileScanner.nextLine();
                temp = temp.toUpperCase();
                fileOut.write(temp+"\n");
            }
            fileScanner.close();
            fileOut.close();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }

    }
}