如何在java中使用点分别使用两个整数?

时间:2017-09-15 12:20:29

标签: java integer java.util.scanner

我想让用户输入用点分隔的整数,如(11.56.98) 在x = 11 y = 56 z = 98

之后使用
    `Scanner s = new Scanner(System.in);
    System.out.print("Enter Your number like (36.52.10): ");
    int x = s.nextInt();
    int y = s.nextInt();
    int z = s.nextInt();`

现在如何使用delimiter将空格改为点以及如何再次返回空白

1 个答案:

答案 0 :(得分:1)

如果您想要在同一行中使用:s.next()

如果您希望下一行中的文字需要执行:s.nextLine()

无论您使用哪种方法,它都会返回java.lang.String

您可以使用String[] numbers = yourString.split(".")

如果你拆分字符串,你可以得到所有数字的数组:

int x = Integer.valueOf(numbers[0]);
int y = Integer.valueOf(numbers[1]);
int z = Integer.valueOf(numbers[2]); 
//Dont forget it can throw a NumberFormatException if the current String is not a valid number.