Scanner是否禁止换行符(即\ n)?

时间:2015-06-23 18:22:08

标签: java java.util.scanner

我已经注意到Scanner如何忽略换行字符,如\ n为新行或\“对于字符串双引号,它似乎有必要工作,所以我想知道是否有什么我是做错了,或者Scanner确实忽略了换行?

以下是代码示例,其中注释String text = "This \n Must \n Work!!"的换行工作正在输出

  


必须
工作!!

但是,如果我们要使用String text = sc.nextLine();和类型“这个\ n Wont \ n Work”它不会创建一个新行但只会输出

  

这个\ n不会工作

to test.txt

代码示例:

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class StoringTheString {

    public static void main(String[] args) {
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("Type anything: ");
            String text = sc.nextLine();
            // String text = "This \n Must \n Work!!!" ;
            sc.close();
            PrintWriter out = new PrintWriter("test.txt");
            out.println(text);
            out.close();
        }catch (FileNotFoundException e) {
            e.printStackTrace();

        }

    }
}

<小时/> 我很抱歉,如果我的帖子不清楚,请评论是否存在一些误解,我会尽力详细说明。
感谢您的时间^ ... ^
< p>

2 个答案:

答案 0 :(得分:2)

尝试使用扫描仪从键盘获取输入,然后使用另一台扫描仪将输入分成不同的行。

public static void main(String[] args) throws Exception {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Type anything: ");
    String text = keyboard.nextLine();

    Scanner scanner = new Scanner(text);
    // This is what will break the line apart
    scanner.useDelimiter("\\s?\\\\n\\s?");

    while (scanner.hasNext()) {
        System.out.println(scanner.next());
    }
}

结果:

Type anything: This \n Must \n Work!!!
This
Must
Work!!!

答案 1 :(得分:0)

试试这个:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.harit</groupId>
    <artifactId>akkaio</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>akkaio-remote</module>
    </modules>

    <packaging>pom</packaging>
    <inceptionYear>2015</inceptionYear>
    <properties>
        <scala.version>2.11.6</scala.version>
        <junit.version>4.12</junit.version>
        <scalatest_2.11.version>2.2.5</scalatest_2.11.version>
        <akka-actor_2.11.version>2.3.11</akka-actor_2.11.version>
        <akka-slf4j_2.11.version>2.3.11</akka-slf4j_2.11.version>
        <akka-testkit_2.11.version>2.3.11</akka-testkit_2.11.version>
        <mockito-all.version>1.10.19</mockito-all.version>
        <maven-scala-plugin.scalaCompatVersion>2.11.6</maven-scala-plugin.scalaCompatVersion>
        <scalatest-maven-plugin.version>1.0</scalatest-maven-plugin.version>
    </properties>

    <repositories>
        <repository>
            <id>scala-tools.org</id>
            <name>Scala-Tools Maven2 Repository</name>
            <url>http://scala-tools.org/repo-releases</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>scala-tools.org</id>
            <name>Scala-Tools Maven2 Repository</name>
            <url>http://scala-tools.org/repo-releases</url>
        </pluginRepository>
    </pluginRepositories>

    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-actor_2.11</artifactId>
            <version>${akka-actor_2.11.version}</version>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-slf4j_2.11</artifactId>
            <version>${akka-slf4j_2.11.version}</version>
        </dependency>
        <dependency>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest-maven-plugin</artifactId>
            <version>${scalatest-maven-plugin.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-testkit_2.11</artifactId>
            <version>${akka-testkit_2.11.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest_2.11</artifactId>
            <version>${scalatest_2.11.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <defaultGoal>clean install</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <scalaCompatVersion>${maven-scala-plugin.scalaCompatVersion}</scalaCompatVersion>
                    <scalaVersion>${scala.version}</scalaVersion>
                    <scalaVersion>${scala.version}</scalaVersion>
                    <args>
                        <arg>-target:jvm-1.8</arg>
                    </args>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.scalatest</groupId>
                <artifactId>scalatest-maven-plugin</artifactId>
                <version>1.0</version>
                <configuration>
                    <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                    <junitxml>.</junitxml>
                    <filereports>WDF TestSuite.txt</filereports>
                </configuration>
                <executions>
                    <execution>
                        <id>test</id>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <configuration>
                    <scalaVersion>${scala.version}</scalaVersion>
                </configuration>
            </plugin>
        </plugins>
    </reporting>
</project>

如果不起作用,请更改out.println(text); to out.print(text);