Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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 - Fatal编程技术网

如何要求用户输入文件名和输出文件名java?

如何要求用户输入文件名和输出文件名java?,java,Java,我试图制作一个程序,要求输入和输出文件名,但我在制作程序时遇到了困难,特别是在要求输出文件的文件名时。 这两种方法都将要求用户输入适当的文件名并以字符串形式返回该名称。这些方法将从主方法调用。它们将返回一个字符串,因此在调用方法之前应该声明两个字符串变量 这是一个读取输入文件并创建输出文件的部分程序,但是我在添加程序中请求文件名的部分时遇到了问题 import java.io.File; import java.io.IOException; import java.io.PrintWriter

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

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

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();
    }
}
}
请帮助我,我是一个编程初学者,我尝试了一些不同的东西,但都没有真正起作用。任何帮助都将不胜感激

这是我尝试过的一个例子(我在读了这篇文章的评论后尝试过):


如果您想请求用户输入,最常见的方法是创建一个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行:
公共静态无效字符串getInputFileName()

方法/函数必须在类的主体内声明。所以移动整个函数

紧接着你的功能,你有一个额外的关闭卷曲大括号,所以删除

不能两次声明公共静态void main(字符串[]args)。尤其是在另一个方法中不能有一个方法,因此请删除外部的
publicstaticvoidmain(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.io.File;
导入java.io.IOException;
导入java.io.PrintWriter;
导入java.text.DecimalFormat;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.Scanner;
公共类Weberproject3test1{
私有静态映射ticketTypeToPrice=newhashmap();
私有静态最终字符串空间=”;
私有静态最终字符串CURRENCY_SYMBOL=“$”;
公共静态void getInputFileName(){
System.out.println(“在此处输入文件名:”);
弦乐;
Scanner scanIn=新扫描仪(System.in);
sWhatever=scanIn.nextLine();
scanIn.close();
System.out.println(sWhatever);
}
公共静态void main(字符串[]args){
扫描器=空;
PrintWriter outputFile=null;
DecimalFormat DecimalFormat=新的DecimalFormat();
decimalFormat.setMinimumFractionDigits(2);
试一试{
File File=新文件(“portlandvip2.txt”);
扫描仪=新扫描仪(文件);
outputFile=新的PrintWriter(“portland2out.txt”);
while(scanner.hasNext()){
字符串行=scanner.nextLine();
String[]entriesOnLine=line.split(“”);
//与价格和门票类型一致
if(entriesOnLine.length==2){
ticketTypeToPrice.put(entriesOnLine[0],Integer.parseInt(entriesOnLine[1]);
StringBuilder sb=新的StringBuilder();
sb.追加(entriesOnLine[0])
.append(货币符号)
.append(decimalFormat.format(Integer.parseInt(entriesOnLine[1]));
println(sb.toString());
}else if(entriesOnLine.length==4){
//列上名字、姓氏、票数和价格
int numberoftickst=Integer.parseInt(entriesOnLine[2]);
int ticketPrice=ticketTypeToPrice.get(entriesOnLine[3]);
int totalPrice=numberOfTickest*ticketPrice;
StringBuilder sb=新的StringBuilder();
sb.追加(entriesOnLine[0])
.append(空格)
.append(entriesOnLine[1])
.append(货币符号)
.append(decimalFormat.format(totalPrice));
println(sb.toString());
}
}
}捕获(IOE异常){
System.out.println(“异常:+e”);
}最后{
scanner.close();
outputFile.close();
}
}
}

注意:这段代码现在可以编译了,但这并不意味着我让它做了您希望它做的事情。

如果您需要读取用户的输入,您可以这样做,例如:

    System.out.println("Type an input path: ");
    Scanner s = new Scanner (System.in);

    String input = s.nextLine();

输入
中,您将获得用户编写的文本。

如果您希望通过在控制台中键入文件名来要求用户提供文件名,您可以使用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);
  }
}

中选取的例子你可以使用时髦的新类。好吧,不是那么新-因为1.6你应该把你的错误列表放在不同的帖子中,并将某人的代码标记为答案。你代码中的错误是编译错误,如果你给我一个链接,我可以在不同的帖子中解释。我更新了我的答案以反映你的更新在尝试将答案添加到程序中后编辑了我的初始帖子,如果我做错了什么,你能告诉我吗?以及为什么我会出现这些错误?代码是一个示例,用于说明BufferedReader的红线和Scanner的下一条线的用法。在新版本中,我看到get中嵌套的main方法
    System.out.println("Type an input path: ");
    Scanner s = new Scanner (System.in);

    String input = s.nextLine();
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);
  }
}