gpt4 book ai didi

java - 如何询问用户输入文件名和输出文件名java?

转载 作者:行者123 更新时间:2023-12-02 05:51:41 24 4
gpt4 key购买 nike

我正在尝试制作一个要求程序输入和输出文件名的程序,但是我在制作该程序时遇到了麻烦,特别是在询问输出文件的文件名时。这两种方法都会要求用户输入适当的文件名并将该名称作为字符串返回。这些方法将从主方法中调用。它们将返回一个字符串,因此在调用方法之前应该声明两个字符串变量。

这是读取输入文件并创建输出文件的部分程序,但我在添加要求文件名的程序部分时遇到问题。

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();
}
}
}

为了使该程序正常工作,我需要使用 header 名称创建两种不同的方法:

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

最佳答案

如果您想要求用户输入,最常见的方法是使用系统输入的参数创建 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();
}
}
}

注意:此代码现在可以编译,但这并不意味着我让它执行您希望它执行的操作。

关于java - 如何询问用户输入文件名和输出文件名java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23482028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com