如何在Java中使用Scanner类及其next()方法?

如何在Java中使用Scanner类及其next()方法?,java,Java,嗨,我有这个Java代码 import java.io.*; import java.util.*; public class SongWriter { public static void main(String[] args) { PrintWriter outputStream = null; // Scope must be outside the try/catch structure try { output

嗨,我有这个Java代码

import java.io.*;
import java.util.*;
public class SongWriter  
{ 
   public static void main(String[] args)  
   {
     PrintWriter outputStream = null;  // Scope must be outside the try/catch structure
     try  
     { 
        outputStream = new PrintWriter("Song.txt");  // new
        FileOutputStream("Song.txt")  
     } 
     catch(FileNotFoundException e) 
     { 
        System.out.println("Error opening the file Song.txt."); 
        System.exit(0);  
     } 
     System.out.println("\n classical songs has many lines");
     System.out.println("\nNow enter the three lines of your Song.");
     String line = null;
     Scanner keyboard = new Scanner(System.in);  
     int count; 
     for (count = 1; count <= 3; count++) 
     {
        System.out.println("\nEnter line " + count + ": ");
        line = keyboard.nextLine(); 
        outputStream.println(count + "\t" + line);  
     } 
     outputStream.close();   
     System.out.println("\nYour Song has been written to the file Song.txt.\n");
    } // end of main  
} // end of class
import java.io.*;
导入java.util.*;
公共级词曲作者
{ 
公共静态void main(字符串[]args)
{
PrintWriter outputStream=null;//作用域必须在try/catch结构之外
尝试
{ 
outputStream=new PrintWriter(“Song.txt”);//新建
FileOutputStream(“Song.txt”)
} 
catch(filenotfounde异常)
{ 
System.out.println(“打开文件Song.txt时出错”);
系统出口(0);
} 
System.out.println(“\n经典歌曲有很多行”);
System.out.println(“\n现在输入歌曲的三行。”);
字符串行=null;
扫描仪键盘=新扫描仪(System.in);
整数计数;
对于(count=1;count您几乎就拥有了它

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter first file name:");
String first = keyboard.nextLine();
System.out.println("Enter second file name:");
String second = keyboard.nextLine();
System.out.println("Enter third file name:");
String third = keyboard.nextLine();
//and so on and continue whatever you want to do..
编辑:在你的评论之后


首先将这3行存储在一个
StringBuilder
中,然后要求输入要写入的文件名。现在您有了歌词和文件名。

使用Scanner类从用户处获取输入:

String fileName1;
Scanner keyboard = new Scanner(System.in); //creates Scanner object
System.out.print ("Enter the name of the file. The file should end in the suffix .txt") //prompt the user to enter the file name
fileName1 = keyboard.next(); //store the name of the file
您应该在try/catch块之前执行此操作,以便可以使用用户输入的文件名,而不是硬编码(就像您在这里使用song.txt所做的那样)


您可以通过这种方式提示用户输入所需的任意多个文件名。

如果您添加了空格以使其更易于阅读,这将非常有用。您是否阅读了Scanner类的Javadocs?@Deena Hi抱歉,我已修复它。@Deena如果您执行代码,您将能够在第1、2和3行中输入您的歌词。当您输入歌曲时,将是一首歌曲.txt将与输入的歌词一起创建在您的文件夹中。但是,如果您再次执行该程序并输入不同的歌词,您以前的歌词将被删除并替换为新的歌词。因此,我假设问题是问一种方法,将歌词保存在Haiku1.txt、Haiku2.txt、Haiku3.txt中,以防止获得它替换。如果我没有很好地解释这个问题,我很抱歉。fileName1将是我将要创建的文件的名称?因此,当我执行代码时,我正在输入文件的名称?您能解释一下这里发生了什么吗?对不起,我对Java有点陌生。程序将向用户显示一个请求,要求用户输入文件的名称h.txt扩展名。您可以使用Scanner类的.next()方法将其存储在变量fileName1(或任何您调用的变量)中。然后,您可以使用存储文件名的变量,而不是硬编码song.txt作为文件名。我是否还需要outputStream=new PrintWriter(“Haiku.txt”);对于Haiku1.txt和Haiku2.txt?您是要同时写入所有三个文本文件,还是要运行程序三次不同的时间,每次写入不同的文本文件?我想运行程序三次不同的时间。我复制粘贴了您显示的内容。这就是我到目前为止得到的结果。导入java.io.*;导入java.util*;公共类词曲作者{public static void main(String[]args){String fileName1;Scanner keyboard=new Scanner(System.in);System.out.print(“输入扩展名”);fileName1=keyboard.next();PrintWriter outputStream=null;//作用域必须在try/catch结构之外。try{outputStream=new PrintWriter(“Song.txt”);//new FileOutputStream(“Song.txt”)}catch(filenotfounde){….等等你在stringbuilder中存储这3行是什么?我知道首先你的程序要求输入歌词,然后是要将歌词写入的文件名。你需要继续将歌词附加到
stringbuilder
中,然后你会要求输入文件名并将
stringbuilder
写入文件。