0

I habitually compile after every change I make, i had all of 5 minutes to start a project and this will not compile!!:

public class Battleship{
    public static void main(String[] args){
        int boardInput = parseInt(args[0]);
        }
}

I get this error:
Battleship.java:19: cannot find symbol
symbol : method parseInt(java.lang.String)
location: class Battleship
int boardInput = parseInt(args[0]);
What I need is for the first command line argument to be made into an integer so I can work with it.
Assistance please :)

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by lrolsto1
0

try this

public class Battleship{

public static void main(String[] args){

int boardInput = Integer.parseInt(args[0]);
}

}

Cheers

0

try this

public class Battleship{
public static void main(String[] args){
int boardInput = Integer.parseInt(args[0]);
}
}

Cheers

That is correct, but I would prefer something like this:

public class Battleship{

public static void main(String[] args){
    if (args.length>0) {
         int boardInput = Integer.parseInt(args[0]);
    } else {
         System.out.println("No arguments given");
    }
}

}

After you understand the above and get familiar with exceptions then try this in case the argument is not a number.

public class Battleship{

public static void main(String[] args){
    if (args.length>0) {

          try {
              int boardInput = Integer.parseInt(args[0]);
          } catch(NumberFormatException nfe) {
              System.out.println("No number was given: "+args[0]);
          }

    } else {
         System.out.println("No arguments given");
    }
}

}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.