Hallo,
folgendes Codebeispiel:
liefert folgenden Output:
Teil1: 2512.0
Teil2: 2534.0
Warum werde die beiden Kommazahlen des Strings s:
2.512 und 2.534
ohne Kommapunkt eingelesen? Was mache ich falsch?
Danke im Vorraus!
folgendes Codebeispiel:
Java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.util.Scanner;
public class Test {
private static double real, imag;
private static String evMsg;
public static void main(String[] args) {
String s = "(2.512,2.534)";
Scanner tok = new Scanner(s);
tok.useDelimiter("[\\(\\,\\)]");
if(tok.hasNextDouble())
{
real = tok.nextDouble();
}
else if(tok.hasNextInt())
{
real = (double) tok.nextInt();
}
else
{
evMsg = "Teil 1 muss Double oder Int sein.";
System.out.println(evMsg);
double real = 0;
}
if(tok.hasNextDouble())
{
imag = tok.nextDouble();
}
else if(tok.hasNextInt())
{
imag = (double) tok.nextInt();
}
else
{
evMsg = "Teil 2 muss Double oder Int sein.";
System.out.println(evMsg);
double imag = 0;
}
System.out.println("Teil1: " + real + "\n" + "Teil2: " + imag);
}
}
liefert folgenden Output:
Teil1: 2512.0
Teil2: 2534.0
Warum werde die beiden Kommazahlen des Strings s:
2.512 und 2.534
ohne Kommapunkt eingelesen? Was mache ich falsch?
Danke im Vorraus!