Java help! (7)

1 Name: #!/usr/bin/anonymous : 2008-11-14 16:03 ID:xeymDurh

In b4 there are other threads. They are all permasaged ^^

In any case, I have a stoopid question. I understand Java concepts, just not code, so i've been having a rather unpleasant time with something I know it's simple.

We are asked to make a program that asks the user for two points with two coordinates (x, y) and then output the distance between those two points in the cartesian plane.

My major trouble have been with the input thing. I read something about BufferedReader, but that thing is kicking my ass.

Btw, everything needs to be done on the console.

Thanks for your help =)

2 Name: #!/usr/bin/anonymous : 2008-11-14 17:28 ID:Heaven

Holy crap you're one lazy motherfucker I don't know java at all and it took me 1 minute to solve your problem. I googled for: "java user input", read about readLine, then googled "java string to integer".

BufferedReader br = new BufferedReader(System.in);
String s = reader.readLine("enter x: ");
String p = reader.readLine("enter y: ");
int x = new Integer(s).intValue();
int y = new Integer(p).intValue();

3 Name: #!/usr/bin/anonymous : 2008-11-18 03:07 ID:ma90ECU+

>>2 "Holy crap you're one lazy motherfucker"

I lol'd

4 Name: #!/usr/bin/anonymous : 2008-12-31 12:49 ID:b5nbczpd

5 Name: #!/usr/bin/anonymous : 2009-01-22 04:30 ID:eBQs10/m

>Holy crap you're one lazy motherfucker

most humans are

6 Name: #!/usr/bin/anonymous : 2009-01-22 14:31 ID:Heaven

I definitely am. That's why I learned programming - so I can make computers do my work for me.

7 Name: #!/usr/bin/anonymous : 2009-01-23 21:48 ID:vzmo9I6N

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class hello {
private static int read(String var) {
int a = 0;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
try {
System.out.print(var + ": ");
String s = br.readLine();
a = new Integer(s).intValue();
break;
} catch (NumberFormatException t) {
System.err.println("Not an integer");
continue;
}
}
} catch (IOException e) {
System.err.println("IO Error.");
}
return a;
}
public static void main(String[] args) {
int x1 = read("x1");
int y1 = read("y1");

int x2 = read("x2");
int y2 = read("y2");

int a = Math.abs(x1 - x2);
int b = Math.abs(y1 - y2);
System.out.println("Distance = " + Math.sqrt(a * a + b * b));
}
};

I'm working with JDK 1.2 here guys.

This thread has been closed. You cannot post in this thread any longer.