Anfängerprobleme (Casting,...)

Beats150

Mitglied
Hi,

ich bin noch in der Konsolenphase und habe da ein Problem.

Java:
public class Eingabe{
public static void main(String[] args){
int x= (args[0]; y= args[1]; a;
a= y*x;
System.out.println("="+a);

}
}

Ich will einfach nur zwei zahlen multiplizieren die ich selber eingeben kann.
Aber es kommt immer ein Fehler:
fehler.png


Ich weiß nicht genau, wie ich das jetzt casten muss bzw. was ich überhaupt machen muss^^
 
Naja der Fehler, den dir Java wirft hat aber erstmal nichts mit der Zahl zu tun, sondern mit der Klammer die du bei

Java:
int x= (args[0]; //usw

geschrieben hast.

Edit: Der zweite hat was, wie schon geschrieben wurde, damit zu tun, dass du mit Komma und nicht mit Semikolons trennen musst.
 
Vieleicht hilft dir ja diese Klasse weiter:

[Java]/**
* @version 1.0
* @author Steev
*/
public final class IOTools {
private static void handleException(Exception e) {
System.out.println("An error occurs while consoleinput:\n" + e);
System.out.print("Please retry the input: ");
}

public static char readChar() {
return readChar("");
}

public static char readChar(String output) {
System.out.print(output);
char c = 0;
try {
c = (char) System.in.read();
} catch (Exception e) {
handleException(e);
return readChar();
}
return c;
}

public static String readString() {
return readString("");
}

public static String readString(String output) {
System.out.print(output);
char c = 0;
String line = "";
while (c != '\n') {
c = readChar();
if (c != '\n' && c != '\r')
line = line.concat("" + c);
}
return line;
}

public static double readDouble() {
return readDouble("");
}

public static double readDouble(String output) {
String s = readString(output);
double d = 0.;
try {
d = Double.parseDouble(s);
} catch (Exception e) {
handleException(e);
return readDouble();
}
return d;
}

public static int readInteger() {
return readInteger("");
}

public static int readInteger(String output) {
String s = readString(output);
int n = 0;
try {
n = Integer.parseInt(s);
} catch (Exception e) {
handleException(e);
return readInteger();
}
return n;
}

public static float readFloat() {
return readFloat("");
}

public static float readFloat(String output) {
String s = readString(output);
float f = 0.f;
try {
f = Float.parseFloat(s);
} catch (Exception e) {
handleException(e);
return readFloat();
}
return f;
}

public static long readLong() {
return readLong("");
}

public static long readLong(String output) {
String s = readString(output);
long l = 0l;
try {
l = Long.parseLong(s);
} catch (Exception e) {
handleException(e);
return readLong();
}
return l;
}

public static short readShort() {
return readShort("");
}

public static short readShort(String output) {
String s = readString(output);
short h = 0;
try {
h = Short.parseShort(s);
} catch (Exception e) {
handleException(e);
return readShort();
}
return h;
}

public static byte readByte() {
return readByte("");
}

public static byte readByte(String output) {
String s = readString(output);
byte b = 0;
try {
b = Byte.parseByte(s);
} catch (Exception e) {
handleException(e);
return readByte();
}
return b;
}

public static boolean readBoolean() {
return readBoolean("");
}

public static boolean readBoolean(String output) {
String s = readString(output);
boolean b = false;
try {
b = Boolean.parseBoolean(s);
} catch (Exception e) {
handleException(e);
return readBoolean();
}
return b;
}
}[/Java]

Du kannst einfach die Methoden dieser Klasse verwenden, um eine Zahl aus der Komandozeile zu lesen.

Das geht dann so:
[Java]int number1 = IOTools.readInteger(" ");
int number2 = IOTools.readInteger("+ ");
System.out.println("= " + (number1 + number2));[/Java]

Ich hoffe das hilfe dir weiter.

Gruß
Steev
 
Zuletzt bearbeitet:
Mhmm, ja. Wie gesagt ich bin noch ein Einfänger und les grad das Buch "Java Intensivkurs". Und da wollte ich halt zwischendurch schonmal ein bischen selber probieren.

Mit so langen Codes kann ich noch gar nichts anfangen und bin damit völlig überfordert den Sinn dahinter zu verstehen 😀
Trotzdem Danke für die Hilfe hier 😉

Mit den Semikolons hab ich auch direkt geändert 😉
 

Zurück
Oben