-1

My System.out.println(info); line gives back "Model_X_Sale_2014.txt" instead of the information in the file. The first line being: "Jan 3128 1.59 3421 1.79" I'm new to splitting a string, but this problem is occuring before the string is even splitting.

Any idea what may be causing this? Thanks for the time either way. Also, is there a particular reason Eclipse wont let me use a try catch around the file stuff?

    public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("What year would you like to review?");
            int year = Integer.parseInt(keyboard.nextLine());
            String fileName= "Model_X_Sale_" + year + ".txt";

                Scanner scanner = new Scanner(fileName);
                   while (scanner.hasNextLine()) {
                       String line = scanner.nextLine();//read one line at a time
                       MonthlySale_Baumbach input = new MonthlySale_Baumbach(line);
                       System.out.printf("\n%s %15.2f %s15.2f", input.getMonth(), input.getProfitX310(), input.getProfitX410());
                   }
                   scanner.close();
}

public class MonthlySale_Baumbach {
//variables
String month;
int X310_Units, X410_Units;
double X310_uPrice, X410_uPrice;


public MonthlySale_Baumbach(){}
public MonthlySale_Baumbach(String info){

System.out.println(info);
    String[] st = info.split("\\s");

            month = st[0];
            X310_Units = Integer.parseInt(st[1]);
            X310_uPrice = Double.parseDouble(st[2]);
            X410_Units = Integer.parseInt(st[3]);
            X410_uPrice = Double.parseDouble(st[4]);



    }//end of constructor
}
Shawn
  • 15
  • 6
  • Change `Scanner scanner = new Scanner(fileName);` to `Scanner scanner = new Scanner(new File(fileName));` as always, look at the java api [link](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html) – Multithreader Dec 01 '14 at 03:43

1 Answers1

2

Start by reading the documention for Scanner(String)

public Scanner(String source)

Constructs a new Scanner that produces values scanned from the specified string.

Parameters:
source - A string to scan

What you probably want is Scanner(File)

public Scanner(File source) throws FileNotFoundException

Constructs a new Scanner that produces values scanned from the specified file. Bytes from the file are converted into characters using the underlying platform's default charset.

Parameters:
source - A file to be scanned

Throws:
FileNotFoundException - if source is not found

MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
  • Thank you, it's working now. Sorry for such a easy question. I was trying to debug it for over an hour but had the mindset that you were suppose to pass a string with an extension (.txt etc.). I'll select this as the right answer when it becomes available. – Shawn Dec 01 '14 at 03:50
  • Java Docs and tutorials should always be the first port of call, sure, even then you might not know when you've stumbled across the answer, but effort is always reward ;) – MadProgrammer Dec 01 '14 at 03:51