0

I'm trying to prompt the user to input the name a file they'd like to write to, create that .txt file and then write the qualifying lines of text into that file and save it. inside the do while, it seems to be skipping over the user input for the name of the file they'd like to save to, looping back around and then getting a FileNotFoundException, and it shouldn't even be looking for a file.

import java.util.*;
import java.io.*;

public class Main {

    public static void main(String[] args) {
        Scanner user = new Scanner(System.in);
        Scanner docInName = null;
        PrintWriter docOutName = null;

        do {
            System.out.println("Please enter the filename of the file you 
                                would like to read from: ");
            try {
                docInName = new Scanner(new File(user.nextLine()));
            } catch (FileNotFoundException e) {
                System.out.println("File not found!");
            }
        } while (docInName == null);

        int lineNum = docInName.nextInt();
        BikePart[] bp = new BikePart[lineNum];
        System.out.println("please enter the max cost for a part: ");
        int cost = user.nextInt();

        do {
            System.out.println("please enter a name for the file to write to 
                                (end with .txt): ");
            String out = user.nextLine();  //PROBLEM HERE! SKIPS USER INPUT
            try {
                docOutName = new PrintWriter(out);
                for (int i = 0; i < lineNum; i++) {
                    String line = docInName.nextLine();
                    String[] elements = line.split(",");
                    bp[i] = new BikePart(elements[0], 
                            Integer.parseInt(elements[1]), 
                            Double.parseDouble(elements[2]),
                            Double.parseDouble(elements[3]), 
                            Boolean.parseBoolean(elements[4]));
                    double temp = Double.parseDouble(elements[3]);
                    if ((temp < cost && bp[i].isOnSale() == true)
                            || (bp[i].getListPrice() < cost && 
                                bp[i].isOnSale() == false)) {
                        docOutName.write(line);
                    }
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } while (docOutName == null);
        user.close();
    }

}
Arik Jones
  • 13
  • 3
  • 2
    Possible duplicate of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Ken Y-N Sep 19 '17 at 00:42
  • It was actually just the opposite, i needed to skip one more line after calling for the max cost line. right idea though, thank you! – Arik Jones Sep 19 '17 at 01:15

1 Answers1

1

I just needed to skip a line before the loop began.

import java.util.*;
import java.io.*;

public class Main {

    public static void main(String[] args) {
        Scanner user = new Scanner(System.in);
        Scanner docInName = null;
        PrintWriter docOutName = null;

        do {
            System.out.println("Please enter the filename of the file you would like to read from: ");
            try {
                docInName = new Scanner(new File(user.nextLine()));
            } catch (FileNotFoundException e) {
                System.out.println("File not found!");
            }
        } while (docInName == null);

        int lineNum = docInName.nextInt();
        BikePart[] bp = new BikePart[lineNum];
        System.out.println("please enter the max cost for a part: ");
        int cost = user.nextInt();
        user.nextLine();    //SOLUTION HERE

        do {
            System.out.println("please enter a name for the file to write to (end with .txt): ");
            String out = user.nextLine();
            try {
                docOutName = new PrintWriter(out);
                for (int i = 0; i < lineNum; i++) {
                    String line = docInName.nextLine();
                    String[] elements = line.split(",");
                    bp[i] = new BikePart(elements[0], Integer.parseInt(elements[1]), Double.parseDouble(elements[2]),
                            Double.parseDouble(elements[3]), Boolean.parseBoolean(elements[4]));
                    double temp = Double.parseDouble(elements[3]);
                    if ((temp < cost && bp[i].isOnSale() == true)
                            || (bp[i].getListPrice() < cost && bp[i].isOnSale() == false)) {
                        docOutName.write(line);
                    }
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } while (docOutName == null);
        user.close();
    }

}
Arik Jones
  • 13
  • 3