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 :)

Recommended Answers

All 3 Replies

try this

public class Battleship{

public static void main(String[] args){

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

}

Cheers

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");
    }
}

}

good call. That worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.