基本上我希望用户通过控制台进行输入,我想指出第一个数字并将其发送到控制台,例如:
hello465924whats334up // userinput
465924 // console output
这基本上是我现在的代码:
import java.util.Scanner;
public class ZahlZusammenFueger {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please enter something!");
String e = s.nextLine();
}
}
答案 0 :(得分:1)
由于该数字是子字符串,因此请查找索引开始的位置和结束位置。像这样:
int i = 0;
int j = 0;
Scanner s = new Scanner(System.in);
System.out.println("Please enter something!");
String e = s.nextLine();
while (!Character.isDigit(e.charAt(i))) i++; // finding index
// where substring of first number starts
j = i;
while (Character.isDigit(e.charAt(j))) j++; // finding index
// where substring of first number ends
String number = e.substring(i, j));
现在,您可以通过此方式从中Integer
或Long
(取决于尺寸):
System.out.println(Integer.parseInt(e.substring(i, j)));
System.out.println(Long.parseInt(e.substring(i, j)));