在java中获取整数输入
实际上,我是java编程新手,我发现很难接受整数输入并将其存储在变量中…如果有人能告诉我如何做,或者提供一个示例,比如添加用户给定的两个数字..如果您是从控制台输入中谈论这些参数,或者任何其他字符串参数,使用静态方法将它们转换为整数。如果您正在从控制台输入中谈论这些参数或任何其他字符串参数,请使用静态方法将它们转换为整数。以下是我的条目,包括相当健壮的错误处理和资源管理:在java中获取整数输入,java,parsing,input,Java,Parsing,Input,实际上,我是java编程新手,我发现很难接受整数输入并将其存储在变量中…如果有人能告诉我如何做,或者提供一个示例,比如添加用户给定的两个数字..如果您是从控制台输入中谈论这些参数,或者任何其他字符串参数,使用静态方法将它们转换为整数。如果您正在从控制台输入中谈论这些参数或任何其他字符串参数,请使用静态方法将它们转换为整数。以下是我的条目,包括相当健壮的错误处理和资源管理: import java.io.BufferedReader; import java.io.IOException; imp
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Simple demonstration of a reader
*
* @author jasonmp85
*
*/
public class ReaderClass {
/**
* Reads two integers from standard in and prints their sum
*
* @param args
* unused
*/
public static void main(String[] args) {
// System.in is standard in. It's an InputStream, which means
// the methods on it all deal with reading bytes. We want
// to read characters, so we'll wrap it in an
// InputStreamReader, which can read characters into a buffer
InputStreamReader isReader = new InputStreamReader(System.in);
// but even that's not good enough. BufferedReader will
// buffer the input so we can read line-by-line, freeing
// us from manually getting each character and having
// to deal with things like backspace, etc.
// It wraps our InputStreamReader
BufferedReader reader = new BufferedReader(isReader);
try {
System.out.println("Please enter a number:");
int firstInt = readInt(reader);
System.out.println("Please enter a second number:");
int secondInt = readInt(reader);
// printf uses a format string to print values
System.out.printf("%d + %d = %d",
firstInt, secondInt, firstInt + secondInt);
} catch (IOException ioe) {
// IOException is thrown if a reader error occurs
System.err.println("An error occurred reading from the reader, "
+ ioe);
// exit with a non-zero status to signal failure
System.exit(-1);
} finally {
try {
// the finally block gives us a place to ensure that
// we clean up all our resources, namely our reader
reader.close();
} catch (IOException ioe) {
// but even that might throw an error
System.err.println("An error occurred closing the reader, "
+ ioe);
System.exit(-1);
}
}
}
private static int readInt(BufferedReader reader) throws IOException {
while (true) {
try {
// Integer.parseInt turns a string into an int
return Integer.parseInt(reader.readLine());
} catch (NumberFormatException nfe) {
// but it throws an exception if the String doesn't look
// like any integer it recognizes
System.out.println("That's not a number! Try again.");
}
}
}
}
以下是我的文章,包括相当强大的错误处理和资源管理:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Simple demonstration of a reader
*
* @author jasonmp85
*
*/
public class ReaderClass {
/**
* Reads two integers from standard in and prints their sum
*
* @param args
* unused
*/
public static void main(String[] args) {
// System.in is standard in. It's an InputStream, which means
// the methods on it all deal with reading bytes. We want
// to read characters, so we'll wrap it in an
// InputStreamReader, which can read characters into a buffer
InputStreamReader isReader = new InputStreamReader(System.in);
// but even that's not good enough. BufferedReader will
// buffer the input so we can read line-by-line, freeing
// us from manually getting each character and having
// to deal with things like backspace, etc.
// It wraps our InputStreamReader
BufferedReader reader = new BufferedReader(isReader);
try {
System.out.println("Please enter a number:");
int firstInt = readInt(reader);
System.out.println("Please enter a second number:");
int secondInt = readInt(reader);
// printf uses a format string to print values
System.out.printf("%d + %d = %d",
firstInt, secondInt, firstInt + secondInt);
} catch (IOException ioe) {
// IOException is thrown if a reader error occurs
System.err.println("An error occurred reading from the reader, "
+ ioe);
// exit with a non-zero status to signal failure
System.exit(-1);
} finally {
try {
// the finally block gives us a place to ensure that
// we clean up all our resources, namely our reader
reader.close();
} catch (IOException ioe) {
// but even that might throw an error
System.err.println("An error occurred closing the reader, "
+ ioe);
System.exit(-1);
}
}
}
private static int readInt(BufferedReader reader) throws IOException {
while (true) {
try {
// Integer.parseInt turns a string into an int
return Integer.parseInt(reader.readLine());
} catch (NumberFormatException nfe) {
// but it throws an exception if the String doesn't look
// like any integer it recognizes
System.out.println("That's not a number! Try again.");
}
}
}
}
你是说用户的输入
Scanner s = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = s.nextInt();
//process the number
你是说用户的输入
Scanner s = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = s.nextInt();
//process the number
是完成此任务的最佳选择
从文件中:
例如,此代码允许用户从System.in读取数字:
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
读一个int只需要两行,但不要低估扫描仪的强大功能。例如,以下代码将一直提示输入一个数字,直到给出一个为止:
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number: ");
while (!sc.hasNextInt()) {
System.out.println("A number, please?");
sc.next(); // discard next token, which isn't a valid int
}
int num = sc.nextInt();
System.out.println("Thank you! I received " + num);
这就是您需要编写的全部内容,而且由于它,您根本不必担心Integer.parseInt和NumberFormatException
另见
相关问题
其他例子
扫描器可以使用一个或一个平面作为其源
下面是一个使用Scanner标记字符串并同时解析为数字的示例:
Scanner sc = new Scanner("1,2,3,4").useDelimiter(",");
int sum = 0;
while (sc.hasNextInt()) {
sum += sc.nextInt();
}
System.out.println("Sum is " + sum); // prints "Sum is 10"
这里有一个更高级的用法,使用正则表达式:
Scanner sc = new Scanner("OhMyGoodnessHowAreYou?").useDelimiter("(?=[A-Z])");
while (sc.hasNext()) {
System.out.println(sc.next());
} // prints "Oh", "My", "Goodness", "How", "Are", "You?"
正如你们所看到的,扫描仪是相当强大的!您应该更喜欢它,它现在是一个遗留类
另见
相关问题
是完成此任务的最佳选择
从文件中:
例如,此代码允许用户从System.in读取数字:
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
读一个int只需要两行,但不要低估扫描仪的强大功能。例如,以下代码将一直提示输入一个数字,直到给出一个为止:
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number: ");
while (!sc.hasNextInt()) {
System.out.println("A number, please?");
sc.next(); // discard next token, which isn't a valid int
}
int num = sc.nextInt();
System.out.println("Thank you! I received " + num);
这就是您需要编写的全部内容,而且由于它,您根本不必担心Integer.parseInt和NumberFormatException
另见
相关问题
其他例子
扫描器可以使用一个或一个平面作为其源
下面是一个使用Scanner标记字符串并同时解析为数字的示例:
Scanner sc = new Scanner("1,2,3,4").useDelimiter(",");
int sum = 0;
while (sc.hasNextInt()) {
sum += sc.nextInt();
}
System.out.println("Sum is " + sum); // prints "Sum is 10"
这里有一个更高级的用法,使用正则表达式:
Scanner sc = new Scanner("OhMyGoodnessHowAreYou?").useDelimiter("(?=[A-Z])");
while (sc.hasNext()) {
System.out.println(sc.next());
} // prints "Oh", "My", "Goodness", "How", "Are", "You?"
正如你们所看到的,扫描仪是相当强大的!您应该更喜欢它,它现在是一个遗留类
另见
相关问题
从哪里输入?程序参数?控制台用户界面?GUI/Swing?到目前为止您尝试了什么?如果从控制台阅读,您是否研究过扫描仪类?从哪里输入?程序参数?控制台用户界面?GUI/Swing?到目前为止您尝试了什么?如果从控制台阅读,您是否研究过Scanner类?这对学习很有好处,但正如Ash所建议的,Scanner类更方便,我不确定在Javadoc头中包含BNF语法的对象是否是最用户友好的对象。确保它对解析控制台输入既方便又强大,但它是有状态的,比解释如何读取一行并将其解析为int要复杂得多。这对学习很有好处,但正如Ash所建议的,Scanner类更方便,我不确定在Javadoc头中包含BNF语法的对象是否是最用户友好的对象。当然,解析控制台输入非常方便和强大,但它是有状态的,比解释如何读取一行并将其解析为int要复杂得多。