简体   繁体   English

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

[英]how to ask user for output and input file names?

I am trying to make a program that asks for a program input and output file names, but i am having trouble in making the program, especially for asking the file name for the output file. 我正在尝试制作一个要求输入和输出文件名的程序,但是我在制作程序时遇到了麻烦,尤其是在询问输出文件的文件名时。 Both of these methods will ask the user to enter the appropriate file name and return that name as a String. 这两种方法都将要求用户输入适当的文件名,并将该名称作为字符串返回。 These methods will be called from the main method. 这些方法将从main方法中调用。 They will return a String so there should be two String variables declared before the methods are called. 它们将返回一个String,因此在调用方法之前应声明两个String变量。

This is the part program that reads an input file and creates an output file, but i am having trouble with adding the part of the program that will ask for the file names. 这是读取输入文件并创建输出文件的零件程序,但是我在添加程序中询问文件名的部分时遇到了麻烦。 I am trying to work on the method that asks the user for the input method, but when i run the program nothing is printed out in the interaction page like i need it to. 我正在尝试要求用户提供输入法的方法,但是当我运行该程序时,交互页面中没有任何打印出来,就像我需要它一样。 I have looked in my book for help, but i am getting no where with it, any help will be really appreciated. 我已经在书中寻找帮助,但没有帮助,我们将不胜感激。

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() {
 // Create Scanner object for keyboard input. 
  Scanner keyboard = new Scanner(System.in);

  //Get the file name
    System.out.println("Enter filename here : ");
    String filename = keyboard.nextLine();

    //Open the file.
    File file = new File(filename);




    String sWhatever;

    Scanner scanIn = new Scanner(System.in);
    sWhatever = scanIn.nextLine();
    System.out.println(filename);

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

In order for this program to work, i need to make 2 different methods with the headers names: 为了使该程序正常工作,我需要使用标题名称制作2种不同的方法:

public static String getInputFileName()
public static String getOutputFileName()

How do i get the getInputFileName Method to work? 我如何使getInputFileName方法起作用?

2 things that you need to do 您需要做的2件事

1: You have to call getInputFileName() somewhere in your main method 1:您必须在主方法中的某处调用getInputFileName()

example: 例:

public static void main(String[] args) {

    ...

    getInputFileName();

2: you have to actually print the filename: 2:您必须实际打印文件名:

example: 例:

System.out.println("Enter filename here : ");
String filename = keyboard.nextLine();

...

System.out.println(filename);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM