簡體   English   中英

如何要求用戶輸入和輸出文件名?

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

我正在嘗試制作一個要求輸入和輸出文件名的程序,但是我在制作程序時遇到了麻煩,尤其是在詢問輸出文件的文件名時。 這兩種方法都將要求用戶輸入適當的文件名,並將該名稱作為字符串返回。 這些方法將從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 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();
    }
}
}

為了使該程序正常工作,我需要使用標題名稱制作2種不同的方法:

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

我如何使getInputFileName方法起作用?

您需要做的2件事

1:您必須在主方法中的某處調用getInputFileName()

例:

public static void main(String[] args) {

    ...

    getInputFileName();

2:您必須實際打印文件名:

例:

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