user3605753
user3605753

Reputation: 3

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. They will return a String so there should be two String variables declared before the methods are called.

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:

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

How do i get the getInputFileName Method to work?

Upvotes: 0

Views: 4116

Answers (1)

2 things that you need to do

1: You have to call getInputFileName() somewhere in your main method

example:

public static void main(String[] args) {

    ...

    getInputFileName();

2: you have to actually print the filename:

example:

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

...

System.out.println(filename);

Upvotes: 1

Related Questions