Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在句号、问号和感叹号之后开始新行?_Java_Io - Fatal编程技术网

Java 如何在句号、问号和感叹号之后开始新行?

Java 如何在句号、问号和感叹号之后开始新行?,java,io,Java,Io,我正在编写一个程序来更改输入文件。它应该在a?之后开始新的生产线。还有!但我似乎不明白。每一行都应该以一个大写字母开头,我想这是我得到的。它还应该消除不必要的空间,我也相信我得到了 例如:你好?酒保我能喝一杯吗!威士忌 Output should be: Hello? Bartender. Can I have a drink!whiskey please. 它应该只在这些操作符后面加一行新的空格。如果没有空间,它就不会换行 import java.util.Scanner; impor

我正在编写一个程序来更改输入文件。它应该在a?之后开始新的生产线。还有!但我似乎不明白。每一行都应该以一个大写字母开头,我想这是我得到的。它还应该消除不必要的空间,我也相信我得到了

例如:你好?酒保我能喝一杯吗!威士忌

Output should be: 
Hello? 
Bartender. 
Can I have a drink!whiskey please.
它应该只在这些操作符后面加一行新的空格。如果没有空间,它就不会换行

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


public class TextFileProcessorDemo
{
public static void main(String[] args)
{
    String fileName, answer;
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Test Input File:");
    fileName = keyboard.nextLine();
    File file = new File(fileName);

    PrintWriter outputStream = null;

    try
    {
        outputStream = new PrintWriter(file);
    }
    catch(FileNotFoundException e)
    {
        System.out.println("Error opening file" + file);
        System.exit(0);
    }

    System.out.println("Enter a line of text:");
    String line = keyboard.nextLine();
    outputStream.println(line);
    outputStream.close();
    System.out.println("This line was written to:" + " " + file);
    System.out.println(" ");

    TextFileProcessor.textFile();


}

}
二等舱

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


public class TextFileProcessor
{
public static void textFile()
{
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Test Input File:");
    String inputFile = keyboard.next();
    System.out.print("Output File:");
    String outputFile = keyboard.next();

    try
    {
        BufferedReader inputStream = new BufferedReader(new FileReader(inputFile));
        PrintWriter outputStream = new PrintWriter(new FileOutputStream(outputFile));
        String line = inputStream.readLine();

        line = line.replaceAll("\\s+", " ").trim();
        line = line.substring(0,1).toUpperCase() + line.substring(1);
        //This is where I would like to add code

        while(line != null)
        {
            outputStream.println(line);
            System.out.println(line);
            line = inputStream.readLine();
        }

        inputStream.close();
        outputStream.close();
    }

    catch(FileNotFoundException e)
    {
        System.out.println("File" + inputFile + " not found");
    }
    catch(IOException e)
    {
        System.out.println("Error reading from file" + inputFile);
    }
}
}

这能解决你的问题吗

if(line.endsWith("! ") || line.endsWith("? ") || line.endsWith(". ")) {
  line = line + '\n';
}

一个简单的正则表达式就足够了:

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        for(String s:"hello? bartender. can I have a drink!whiskey please.".replaceAll("(\\W)(\\s+)", "$1\n").split("\n"))
            System.out.println(s.substring(0, 1).toUpperCase()+s.substring(1));
    }
}
输出:

Hello?
Bartender.
Can I have a drink!whiskey please.

您可以在正则表达式中使用“捕获组”来实现所需

line.replaceAll("(\\? )|(\\! )|(\\. )", "$0\n");
更新:

关于如何将每行的第一个字符大写的注释,可以使用
character
类中的
toUpperCase
方法

line = Character.toUpperCase(line.charAt(0)) + line.substring(1);
注意:

如果您使用java 1.7或以上,您应该考虑使用“资源块”的“代码>扫描器< /COD> < /P>
try (Scanner keyboard = new Scanner(System.in)) {
   ...
}
然后,当您从用户处读取输入时,可以在写入文件之前将其处理为正确的格式。例如,你可以做一些像

 System.out.println("Enter a line of text:");
 String[] lines = keyboard.nextLine().replaceAll("(\\? )|(\\! )|(\\. )", "$0\n").split("\n");

  // Replace the first character of each line with an uppercase character
  for (int i = 0; i < lines.length; i++) {
        lines[i] = Character.toUpperCase(lines[i].charAt(0)) + lines[i].substring(1);
  }

  Path path = Paths.get(fileName);
  Files.write(path, Arrays.asList(lines), Charset.defaultCharset());

  System.out.println("This line was written to:" + " " + path.toString());
  System.out.println(" ");
然后,您可以使用
readAllLines
方法读取文件

    List<String> lines = Files.readAllLines(path);

    for (String line : lines ) {
        System.out.println(line);
    }
List line=Files.readAllLines(路径);
用于(字符串行:行){
系统输出打印项次(行);
}

它能解决问题吗?因为角色并不总是在行尾,所以这不会达到OP想要的结果。它不起作用…但是感谢您的回复。我甚至没有想到像这样的东西。
line=line.replace(“?”,“?\n”)。replace(“!”,“!\n”)。replace(“。”,“\n”)此版本将在您的行中的任何位置插入换行符。谢谢!我感谢你的帮助。我觉得这很容易理解!你知道如何将每行的第一个字母大写吗?我尝试使用for循环,但没有成功!非常感谢你!我现在唯一的问题是新行不是以大写字母开头。我想我已经修好了。有什么想法吗?谢谢你的反馈。但是,我无法让循环在我的代码上工作。我不知道为什么。我有很多errors@nedst3r你会犯什么错误。。你复制了原样的代码了吗?
    List<String> lines = Files.readAllLines(path);

    for (String line : lines ) {
        System.out.println(line);
    }