dimanche 29 mars 2015

How to convert String to Double in Java?, if that's the real thing here

I'm trying to make a program than actualy take the different sides of one triangle and return the type of triangle than it is, just for learn how to use java.


for the purpose of the app i'm using two classes


Triangle Class:



import java.util.Scanner;
public class Triangle
{
private double base;
private double ladoa;
private double ladob;
private double res;
public Triangle(double base, double ladoa, double ladob) {
Scanner TriangleMarker = new Scanner( System.in );

System.out.println("Ingrese el tamaño de la Base del triángulo");
this.base = TriangleMarker.nextInt();

System.out.println("Ingrese el tamaño del lado A del triángulo");
this.ladoa = TriangleMarker.nextInt();

System.out.println("Ingrese el tamaño del lado B del triángulo");
this.ladob = TriangleMarker.nextInt();
}
public Triangle() {
base = 0;
ladoa = 0;
ladob = 0;
}

public double getTriangle() {
double lados=ladoa+ladob;
if (lados==base) {
if (base==(lados/2) && ladoa==ladob) {
res = 1;
} else if (ladoa==ladob && lados < base || lados > base){
res = 2;
} else if (ladoa > ladob || ladoa < ladob && base > ladoa || base < ladob) {
res = 3;
} else {
res = 0;
}
} else {
res = 0;
}
return res;
}
}


And triangleChecker Class:



public class triangleChecker
{
private static double n;
private static double val;
public static void main(String[] args) {
n = 0;
for (String arg: args) {

val[n] = Double.parseDouble(arg);
n++;
}
Triangle obj1 = new Triangle(val);
obj1.getTriangle();
if (val == 1) {
System.out.println(obj1.getTriangle());
} else if (val == 2){
System.out.println(obj1.getTriangle());
} else if (val == 3){
System.out.println(obj1.getTriangle());
} else {
System.out.println(obj1.getTriangle());
}
}
}


Now, in triangleChecker untile i understand, the string[] must be some kind of array, (correct me please if i'm wrong) and i need it to convert it in doubles so i can insert with scanner the data, convert it and bring it to the getTriangle() method, so i can finish this app and learn about it.


The errors in the console has been several and of very different types, the thing all the tuts and things i found to try to solve it by myself doesn't seem to work with user input from scanner (at least) and thats what i'm trying to convert.


Yeah, i have some confusion about how to convert, or better say, select the input from scanner (here, in the code called TriangleMaker(?) ) and convert it to doubles.


So, the question is: how can i convert the content of the String[] array (if it is really an array consider in triangleChecker) so to insert it in the getTriangle() method?


or alternatibly


How can i make the conversion requeired so to convert the user input(Strings) in doubles and/or at least use the user input in the method? (even if the conversion is not the way to go)


Any question, request for clarification, answer, comment or wherever helps to improve the question quality or find a solution to the problem would be much appreciated.


Thanks in advance


Aucun commentaire:

Enregistrer un commentaire