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 =)
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();
>>2 "Holy crap you're one lazy motherfucker"
I lol'd
Java IO is pretty complex.
http://www.digilife.be/quickreferences/PT/Introduction%20to%20Java%20IO.pdf
>Holy crap you're one lazy motherfucker
most humans are
I definitely am. That's why I learned programming - so I can make computers do my work for me.
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.