0
public static void main(String[] args) throws FileNotFoundException {
    Scanner sc = new Scanner(new FileReader(args[1]));
    String co = sc.next();
    coup = Integer.parseInt(co);

I get a FileNotFoundException when I try to pass an int into the second argument in command line. This is only part of the code, a text file is passed as args[0]. However, I can't figure out how to pass a simple integer, only text files.

4
  • 1
    You're getting a FileNotFoundException which suggests that your program can't find the specified file. I'd focus on solving that problem first Commented Nov 11, 2015 at 23:43
  • I am not sure if I get you right, but can you just use int coup = Integer.parseInt(args[1]);? Commented Nov 11, 2015 at 23:43
  • You say that there's a text file being read in through args[0], so what are you doing with args[1]? Commented Nov 11, 2015 at 23:44
  • What is your goal? Do you want to selectively pass an integer or a filename? Commented Nov 11, 2015 at 23:57

3 Answers 3

1
public static void main(String[] args) throws Exception
{
    Scanner scan = new Scanner(new FileReader(args[0]));
    int integerFromCM = Integer.parseInt(args[1]);
}

You state that a text file is the first argument (args[0]) so assign that in the scanner and when grabbing the integer all you need to do is send args[1] into Integer.parseInt method. You are getting the exception because you are assigning a FileReader object with the file name of the integer passed in.

Sign up to request clarification or add additional context in comments.

2 Comments

I think your solution worked, but I'm not entirely sure yet. I declared a static variable in the class, can I modify that in main by reading in? int coup = 0; in the class, then coup = Integer.parseInt(args[1]); in main? It's compiling so I think it's good though.
@user3364161 yes, you can just change the second line of code inside the main method to (coup = Integer.parseInt(args[1]);)
0

You can't pass an int, but you can parse one:

public static void main(String[] args) {
    String filename = args[0];
    int i = Integer.parseInt(args[1]);
    // ...
}

If you are getting a FileNotFoundException, one easy way to debug it is:

File file = new File(filename);
System.out.println(file.getAbsolutePath());

and it will be obvious where the problem lies, which is almost always the current directory of the application is not what you think it is.

Comments

0

Reviewing your code it reads as follows:

  1. Create a Scanner to read the file in the first command line argument
  2. Get the first integer from that Scanner as a String
  3. Parse that String to an int

It is clearly sequenced to require a file from the first argument and that looks like it is intended.

Create a file called number.txt:

42

NumberPrinter.java:

import java.io.Scanner;
import java.io.FileReader;

public final class NumberPrinter {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(new FileReader(args[1]));
        String numberInFile = scanner.next();
        int number = Integer.parseInt(numberInFile);
        System.out.println(number);
    }
}

Run as follows:

java NumberPrinter number.txt

And it will print:

42

Alternatively if you intend to parse an int directly from the command line parameters try:

public final class NumberPrinterDirect {
    public static void main(String[] args) throws Exception {
        int number = Integer.parseInt(args[0]);
        System.out.println(number);
    }
}

NumberOrFilenameAwkward.java:

import java.io.Scanner;
import java.io.FileReader;

public final class NumberOrFilenameAwkward {
    public static void main(String[] args) throws Exception {
        int number;
        try {
            number = Integer.parseInt(args[0]);
        } catch (NumberFormatException thisIsVeryUgly) {
            Scanner scanner = new Scanner(new FileReader(args[1]));
            String numberInFile = scanner.next();
            number = Integer.parseInt(numberInFile);
        }
        System.out.println(number);
    }
}

That is a terrible solution and screams for using a command line parsing library like JewelCLI or commons-cli to solve it cleanly.

1 Comment

I ended up using int x = Integer.parseInt(args[0]); which worked out. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.