解析 java 中用户输入的整数?
parsing for integers from user input in java?
我想以 188/4 的形式从用户那里得到一个有理数,例如
并以 int 188 和 int 4 结束。
在 java 中执行此操作的有效方法是什么? stringTokenizer 是最好的方法吗??或者在 c 中有类似 scanf 的东西吗?
private int NUM;
private int DEN;
static Scanner s;
public Rational(){
s = new Scanner(System.in).useDelimiter("/");
System.out.println("Enter Rational number in the form of a/b:");
NUM = s.nextInt();
int d = s.nextInt();
if(d == 0){
throw new IllegalArgumentException();
}
else{
DEN = d;
}
}
当我运行输入数字后我的程序冻结
尝试这样的事情(假设每个分数都可以在单独的行中输入):
s = new Scanner(System.in).useDelimiter("\n");
System.out.println("Enter Rational number in the form of a/b:");
String in = s.nextLine();
while (in.length() > 0) {
if (in.contains("/")) {
try {
NUM = Integer.parseInt(in.split("/")[0]);
DEN = Integer.parseInt(in.split("/")[1]);
} catch (Exception e){
throw new IllegalArgumentException("Fraction must contain numeric values only");
}
} else {
throw new IllegalArgumentException("Fraction must contain a '/' character");
}
System.out.println("NUM = " + NUM + " DEN = " + DEN);
in = s.nextLine();
}
我能想到的唯一方法是将 is 视为字符串,删除不必要的“/”,然后使用 Integer.parseInt();
将其变回 int
int NUM;
int DEN;
Scanner s;
s = new Scanner(System.in).useDelimiter("/");
System.out.println("Enter Rational number in the form of a/b:");
String enteredString = s.nextLine();
String firstString = "", secondString = "";
StringBuilder sb = new StringBuilder(enteredString);
boolean foundWeirdChar = false;
boolean firstChar = true;
char tempChar = '/';
for(int i = 0; i < sb.length(); i++) {
if(sb.charAt(i) == tempChar) {
foundWeirdChar = true;
}
if(!foundWeirdChar) {
firstString += sb.charAt(i);
}else {
if(firstChar) { // Because the first char will be "/"
firstChar = false;
}else{
secondString += sb.charAt(i);
}
}
}
int a = 0;
int b = 0;
try {
a = Integer.parseInt(firstString);
b = Integer.parseInt(secondString);
System.out.println("First int: " + a + "\nSecond int: " + b);
}catch(NumberFormatException ex) {
ex.printStackTrace();
}
小调整...
private int NUM;
private int DEN;
static Scanner s;
public Rational(){
System.out.println("Enter Rational number in the form of a/b:");
s = new Scanner(System.in);
String[] values = s.next().split("/");
NUM = Integer.parseInt(values[0]);
int d = Integer.parseInt(values[1]);
if(d == 0){
throw new IllegalArgumentException();
} else{
DEN = d;
}
}
我想以 188/4 的形式从用户那里得到一个有理数,例如 并以 int 188 和 int 4 结束。 在 java 中执行此操作的有效方法是什么? stringTokenizer 是最好的方法吗??或者在 c 中有类似 scanf 的东西吗?
private int NUM;
private int DEN;
static Scanner s;
public Rational(){
s = new Scanner(System.in).useDelimiter("/");
System.out.println("Enter Rational number in the form of a/b:");
NUM = s.nextInt();
int d = s.nextInt();
if(d == 0){
throw new IllegalArgumentException();
}
else{
DEN = d;
}
}
当我运行输入数字后我的程序冻结
尝试这样的事情(假设每个分数都可以在单独的行中输入):
s = new Scanner(System.in).useDelimiter("\n");
System.out.println("Enter Rational number in the form of a/b:");
String in = s.nextLine();
while (in.length() > 0) {
if (in.contains("/")) {
try {
NUM = Integer.parseInt(in.split("/")[0]);
DEN = Integer.parseInt(in.split("/")[1]);
} catch (Exception e){
throw new IllegalArgumentException("Fraction must contain numeric values only");
}
} else {
throw new IllegalArgumentException("Fraction must contain a '/' character");
}
System.out.println("NUM = " + NUM + " DEN = " + DEN);
in = s.nextLine();
}
我能想到的唯一方法是将 is 视为字符串,删除不必要的“/”,然后使用 Integer.parseInt();
将其变回 int int NUM;
int DEN;
Scanner s;
s = new Scanner(System.in).useDelimiter("/");
System.out.println("Enter Rational number in the form of a/b:");
String enteredString = s.nextLine();
String firstString = "", secondString = "";
StringBuilder sb = new StringBuilder(enteredString);
boolean foundWeirdChar = false;
boolean firstChar = true;
char tempChar = '/';
for(int i = 0; i < sb.length(); i++) {
if(sb.charAt(i) == tempChar) {
foundWeirdChar = true;
}
if(!foundWeirdChar) {
firstString += sb.charAt(i);
}else {
if(firstChar) { // Because the first char will be "/"
firstChar = false;
}else{
secondString += sb.charAt(i);
}
}
}
int a = 0;
int b = 0;
try {
a = Integer.parseInt(firstString);
b = Integer.parseInt(secondString);
System.out.println("First int: " + a + "\nSecond int: " + b);
}catch(NumberFormatException ex) {
ex.printStackTrace();
}
小调整...
private int NUM;
private int DEN;
static Scanner s;
public Rational(){
System.out.println("Enter Rational number in the form of a/b:");
s = new Scanner(System.in);
String[] values = s.next().split("/");
NUM = Integer.parseInt(values[0]);
int d = Integer.parseInt(values[1]);
if(d == 0){
throw new IllegalArgumentException();
} else{
DEN = d;
}
}