Fares K. A.
Fares K. A.

Reputation: 1285

Capturing Integers from Strings in Java

I'm attempting to capture integers from a user input using Scanner. These integers represent coordinates and a radius between 0 and 1000. It's a circle on a 2D plane.

What I have to do is to somehow capture these integers separately from one line. So, for example, the user inputs

5 100 20

Therefore, the x-coordinate is 5, the y-coordinate is 100, and the radius is 20.

The user must input all of these values on the same line, and I have to somehow capture the values from the program into three different variables.

So, I tried using this:

Scanner input = new Scanner(System.in);
String coordAndRadius = input.nextLine();

int x = coordAndRadius.charAt(0);   //  x-coordinate of ship
int y = coordAndRadius.charAt(2);   //  y-coordinate of ship
int r = coordAndRadius.charAt(4);   //  radius of ship

for one-digit characters, as a test. Didn't turn out so well.

Any suggestions?

Upvotes: 1

Views: 1226

Answers (5)

Luke
Luke

Reputation: 1306

try this:

Scanner scanner = new Scanner(System.in);

System.out.println("Provide x, y and radius,");
int x = scanner.nextInt();
int y = scanner.nextInt();
int radius = scanner.nextInt();

System.out.println("Your x:"+x+" y: "+y+" radius:"+radius);

It will work either you will type in "10 20 24" or "10\n20\n24" where \n is of course a newline character.

And just in case you would like to know why your approach does not work here is explanation.

int x = coordAndRadius.charAt(0);

charAt(0) return first character of your string which then gets implicitly casted into int. Assume your coordAndRadius ="10 20 24". So in this case first char is '1'. So the statement above can be written as: int x = (int)'1';

Upvotes: 2

Amar
Amar

Reputation: 12010

Split the values by space

String[] values = coordAndRadius.split(" ");

Then get each value as int using Integer.parseInt:

int x = Integer.parseInt(values[0]);
int y = Integer.parseInt(values[1]);
int radious = Integer.parseInt(values[2]);

Upvotes: 1

libik
libik

Reputation: 23029

Well the easiest way (not the nicest one) is just split them into array using String methods :

public static void filesInFolder(String filename) {
    Scanner input = new Scanner(System.in);
    String coordAndRadius = input.nextLine();
    String[] array = coordAndRadius.split(" ");

    int x = Integer.valueOf(array[0]);
    int y = Integer.valueOf(array[1]);
    int r = Integer.valueOf(array[2]);
}

You can also use nextInt method, which looks like this :

public static void filesInFolder(String filename) {
    Scanner input = new Scanner(System.in);
    int[] data = new int[3];
    for (int i = 0; i < data.length; i++) {
        data[i] = input.nextInt();
    }            
}

Your input will be stored in array data

Upvotes: 3

rgettman
rgettman

Reputation: 178263

You must split the input into 3 different string variables, each of which can be parsed separately. Use the split method to return an array, with each element containing a piece of input.

String[] fields = coordAndRadius.split(" ");  // Split by space

Then you can parse each piece into an int using Integer.parseInt:

int x = Integer.parseInt(fields[0]);
// likewise for y and r

Just make sure you have 3 elements in your array before accessing it.

Upvotes: 3

Piovezan
Piovezan

Reputation: 3223

Create a string array using coordAndRadius.split(" "); and extract the values from each array element.

Upvotes: 3

Related Questions