我正在尝试创建一个程序,要求输入和输出文件名,但是我在制作程序时遇到了麻烦,特别是在询问输出文件的文件名时。 这两种方法都会要求用户输入相应的文件名并将该名称作为String返回。这些方法将从main方法调用。它们将返回一个String,因此在调用方法之前应该声明两个String变量。
这是读取输入文件并创建输出文件的零件程序,但是我在添加要求文件名的程序部分时遇到问题。
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class WebberProject3 {
private static Map<String, Integer> ticketTypeToPrice = new HashMap<String, Integer>();
private static final String SPACE = " ";
private static final String CURRENCY_SYMBOL = " $";
public static void main(String[] args) {
Scanner scanner = null;
PrintWriter outputFile = null;
DecimalFormat decimalFormat = new DecimalFormat();
decimalFormat.setMinimumFractionDigits(2);
try {
File file = new File("portlandvip2.txt");
scanner = new Scanner(file);
outputFile = new PrintWriter("portland2out.txt");
while (scanner.hasNext()) {
String line = scanner.nextLine();
String[] entriesOnLine = line.split(" ");
// Line with price and ticket type
if(entriesOnLine.length == 2) {
ticketTypeToPrice.put(entriesOnLine[0], Integer.parseInt(entriesOnLine[1]));
StringBuilder sb = new StringBuilder();
sb.append(entriesOnLine[0])
.append(CURRENCY_SYMBOL)
.append(decimalFormat.format(Integer.parseInt(entriesOnLine[1])));
outputFile.println(sb.toString());
} else if (entriesOnLine.length == 4) {
//Line with First Name, Last Name, number of Tickets and Price
int numberOfTickest = Integer.parseInt(entriesOnLine[2]);
int ticketPrice = ticketTypeToPrice.get(entriesOnLine[3]);
int totalPrice = numberOfTickest*ticketPrice;
StringBuilder sb = new StringBuilder();
sb.append(entriesOnLine[0])
.append(SPACE)
.append(entriesOnLine[1])
.append(CURRENCY_SYMBOL)
.append(decimalFormat.format(totalPrice));
outputFile.println(sb.toString());
}
}
} catch (IOException e) {
System.out.println("exception:" + e);
} finally {
scanner.close();
outputFile.close();
}
}
}
为了让这个程序工作,我需要使用标题名称制作两种不同的方法:
public static String getInputFileName()
public static String getOutputFileName()
请帮助,我是编程的初学者,我尝试了一些不同的东西,但没有什么是真的有效。任何帮助将非常感激
这将是我尝试过的一个例子(我在阅读了这篇文章的评论后试过这个):
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public static void String getInputFileName()
{
System.out.println("Enter filename here : ");
String sWhatever;
Scanner scanIn = new Scanner(System.in);
sWhatever = scanIn.nextLine();
scanIn.close();
System.out.println(sWhatever);
}
}
public class WebberProject3Test1
{
public static void main(String[] args) {
{
private static Map<String, Integer> ticketTypeToPrice = new HashMap<String, Integer>();
private static final String SPACE = " ";
private static final String CURRENCY_SYMBOL = " $";
public static void main(String[] args) {
Scanner scanner = null;
PrintWriter outputFile = null;
DecimalFormat decimalFormat = new DecimalFormat();
decimalFormat.setMinimumFractionDigits(2);
try {
File file = new File("portlandvip2.txt");
scanner = new Scanner(file);
outputFile = new PrintWriter("portland2out.txt");
while (scanner.hasNext()) {
String line = scanner.nextLine();
String[] entriesOnLine = line.split(" ");
// Line with price and ticket type
if(entriesOnLine.length == 2) {
ticketTypeToPrice.put(entriesOnLine[0], Integer.parseInt(entriesOnLine[1]));
StringBuilder sb = new StringBuilder();
sb.append(entriesOnLine[0])
.append(CURRENCY_SYMBOL)
.append(decimalFormat.format(Integer.parseInt(entriesOnLine[1])));
outputFile.println(sb.toString());
} else if (entriesOnLine.length == 4) {
//Line with First Name, Last Name, number of Tickets and Price
int numberOfTickest = Integer.parseInt(entriesOnLine[2]);
int ticketPrice = ticketTypeToPrice.get(entriesOnLine[3]);
int totalPrice = numberOfTickest*ticketPrice;
StringBuilder sb = new StringBuilder();
sb.append(entriesOnLine[0])
.append(SPACE)
.append(entriesOnLine[1])
.append(CURRENCY_SYMBOL)
.append(decimalFormat.format(totalPrice));
outputFile.println(sb.toString());
}
}
} catch (IOException e) {
System.out.println("exception:" + e);
} finally {
scanner.close();
outputFile.close();
}
}
}
}
我得到这样的错误:
发现了9个错误:File: C:\Users\Eddie\CISS100\WebberProject3Test1.java [line: 10]
Error: Syntax error on tokens, ClassHeader expected instead
File: C:\Users\Eddie\CISS100\WebberProject3Test1.java [line: 12]
Error: Syntax error on token(s), misplaced construct(s)
File: C:\Users\Eddie\CISS100\WebberProject3Test1.java [line: 12]
Error: Syntax error on token ""Enter filename here : "", delete this token
File: C:\Users\Eddie\CISS100\WebberProject3Test1.java [line: 16]
Error: Syntax error on token ";", { expected after this token
File: C:\Users\Eddie\CISS100\WebberProject3Test1.java [line: 26]
Error: Duplicate method main(java.lang.String[]) in type WebberProject3Test1
File: C:\Users\Eddie\CISS100\WebberProject3Test1.java [line: 27]
Error: Syntax error, insert "}" to complete BlockStatements
File: C:\Users\Eddie\CISS100\WebberProject3Test1.java [line: 27]
Error: Syntax error, insert "}" to complete MethodBody
File: C:\Users\Eddie\CISS100\WebberProject3Test1.java [line: 32]
Error: Duplicate method main(java.lang.String[]) in type WebberProject3Test1
File: C:\Users\Eddie\CISS100\WebberProject3Test1.java [line: 74]
Error: Syntax error on token "}", delete this token
答案 0 :(得分:2)
如果您想要求用户输入,最常见的方法是使用System输入的参数创建Scanner类的实例,如此
Scanner userInput = new Scanner(System.in);
然后,您可以调用此类的方法来获取用户键入的下一行文本,因此您的代码可能看起来像这样
System.out.println("where to read?");
String in = userInput.nextLine();
System.out.println("where to write?");
String out = userInput.nextLine();
Scanner scanner = new Scanner(new File(in));
PrintWriter outputFile = new PrintWriter(out);
此代码允许用户在命令行或netbeans / eclipse中从终端读取和写入。
编译错误:
第10行:public static void String getInputFileName()
必须在类的主体内声明方法/函数。所以移动整个函数
在你的功能之后,你有一个额外的闭合大括号,所以删除它。
您不能两次声明public static void main(String [] args)。您尤其不能在另一个方法中使用方法,因此请删除外部public static void main(String[] args) {
和后面的额外开口花括号。
public static void String getInputFileName()
只能有一种返回类型,因此请将其设置为void,因为您没有返回。
最后删除末尾关闭大括号的额外内容。
您的代码看起来应该是这样的
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class WebberProject3Test1 {
private static Map<String, Integer> ticketTypeToPrice = new HashMap<String, Integer>();
private static final String SPACE = " ";
private static final String CURRENCY_SYMBOL = " $";
public static void getInputFileName() {
System.out.println("Enter filename here : ");
String sWhatever;
Scanner scanIn = new Scanner(System.in);
sWhatever = scanIn.nextLine();
scanIn.close();
System.out.println(sWhatever);
}
public static void main(String[] args) {
Scanner scanner = null;
PrintWriter outputFile = null;
DecimalFormat decimalFormat = new DecimalFormat();
decimalFormat.setMinimumFractionDigits(2);
try {
File file = new File("portlandvip2.txt");
scanner = new Scanner(file);
outputFile = new PrintWriter("portland2out.txt");
while (scanner.hasNext()) {
String line = scanner.nextLine();
String[] entriesOnLine = line.split(" ");
// Line with price and ticket type
if (entriesOnLine.length == 2) {
ticketTypeToPrice.put(entriesOnLine[0], Integer.parseInt(entriesOnLine[1]));
StringBuilder sb = new StringBuilder();
sb.append(entriesOnLine[0])
.append(CURRENCY_SYMBOL)
.append(decimalFormat.format(Integer.parseInt(entriesOnLine[1])));
outputFile.println(sb.toString());
} else if (entriesOnLine.length == 4) {
//Line with First Name, Last Name, number of Tickets and Price
int numberOfTickest = Integer.parseInt(entriesOnLine[2]);
int ticketPrice = ticketTypeToPrice.get(entriesOnLine[3]);
int totalPrice = numberOfTickest * ticketPrice;
StringBuilder sb = new StringBuilder();
sb.append(entriesOnLine[0])
.append(SPACE)
.append(entriesOnLine[1])
.append(CURRENCY_SYMBOL)
.append(decimalFormat.format(totalPrice));
outputFile.println(sb.toString());
}
}
} catch (IOException e) {
System.out.println("exception:" + e);
} finally {
scanner.close();
outputFile.close();
}
}
}
注意:此代码现在将编译,但这并不意味着我已按照您的意愿执行此操作。
答案 1 :(得分:0)
如果您需要阅读用户的输入,您可以这样做:
System.out.println("Type an input path: ");
Scanner s = new Scanner (System.in);
String input = s.nextLine();
在input
中,您将获得用户所写的文字。
答案 2 :(得分:0)
如果您想要通过在控制台中输入文件名来要求用户提供文件名,您可以使用BufferedReader的readLine或Scanner的nextLine。 BufferedReader的示例:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReadConsoleSystem {
public static void main(String[] args) {
System.out.println("Enter filename here : ");
try{
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
String s = bufferRead.readLine();
System.out.println(s);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
扫描仪示例
import java.util.Scanner;
public class ReadConsoleScanner {
public static void main(String[] args) {
System.out.println("Enter filename here : ");
String sWhatever;
Scanner scanIn = new Scanner(System.in);
sWhatever = scanIn.nextLine();
scanIn.close();
System.out.println(sWhatever);
}
}
的示例