mercredi 1 avril 2015

Convert a String with text to long? (Java) [duplicate]


This question already has an answer here:




I am really just starting out at programming and am currently learning from thenewboston: http://ift.tt/1P0Wi2z


I am looking into how I could (if I can at all) turn a String with text into a long data type. I want to make it so that anything below 0! is "undefined" and display it.



public class bucky {
public static void main(String[] args){
System.out.println(fact(-1));
}

public static long fact(long n){
String s = "undefined";
Long lObj1 = Long.valueOf(s);

if(n <= 1 && n>=0)
return 1;
else if(n<0)
return lObj1;
else
return n * fact(n-1);
}
}


If you know a way in which I can convert this, please let me know. Thanks so much! Just starting out.


Aucun commentaire:

Enregistrer un commentaire