将用户的字符串输入转换为数字(int)值。

时间:2011-07-28 20:59:20

标签: java string int

我一直在尝试将用户字符串输入转换为int的不同方法,我可以比较并构建一个“if-then”语句。每次我尝试测试它,它只是抛出异常。任何人都可以查看我的Java代码并帮助我找到方法吗?我对它一无所知(在编程中也是一个菜鸟)。如果我违反任何规则,请让我知道我是新来的。谢谢。

无论如何,这是代码:

 System.out.println("Sorry couldn't find your user profile " + userName + ".");
 System.out.println("Would you like to create a new user profile now? (Enter Y for yes), (Enter N for no and exit).");
 try {
     BufferedReader answer = new BufferedReader(new InputStreamReader(System.in));
     String addNewUser = answer.readLine();
     Character i = new Character(addNewUser.charAt(0));
     String s = i.toString();
     int answerInDecimal = Integer.parseInt(s);
     System.out.println(answerInDecimal);
 }
 catch(Exception e) {
     System.out.println("You've mistyped the answer.");
 e.getMessage();
 }

5 个答案:

答案 0 :(得分:1)

我认为你的意思是要求他们输入0表示是,1表示否?也许?

您要求用户键入Y或N,然后您尝试将其解析为整数。这将永远抛出异常。

编辑 - 正如其他人所指出的那样,如果你想继续使用Y或N,你应该做的事情

String addNewUser = answer.readLine();
if ( addNewUser.toLowerCase().startsWith("y") ) {
// Create new profile
}

答案 1 :(得分:1)

您似乎正在尝试将字符串(应该是单个字符,Y或N)转换为其字符值,然后检索字符的数字表示。

如果要将Y或N转换为十进制表示,则必须执行转换为int:

BufferedReader answer = new BufferedReader(new InputStreamReader(System.in));
String addNewUser = answer.readLine();
char i = addNewUser.charAt(0);
int integerChar = (int) i;       //The important part
System.out.println(integerChar);

这将返回用户输入的字符的整数表示。调用String.toUpperCase()方法可能也很有用,以确保Y / N或y / n的不同输入不会给出不同的值。

但是,您也可以根据字符本身执行if-else,而不是将其转换为整数。

BufferedReader answer = new BufferedReader(new InputStreamReader(System.in));
String addNewUser = answer.readLine();
char i = addNewUser.toUpperCase().charAt(0);
if (i == 'Y') {
    //Handle yes
} else if (i == 'N') {
    //Handle no
} else {
    System.out.println("You've mistyped the answer.");
}

答案 2 :(得分:0)

请改用if (addNewUser.startsWith("Y") || addNewUser.startsWith("y")) {。 或者(正如马克指出的那样)if (addNewUser.toLowerCase().startsWith("y")) {。  BTW可能会看Apache Commons CLI

答案 3 :(得分:0)

parseInt只用于将文本数转换为整数:其他所有内容都会产生NumberFormatException。

如果你想要一个字符的十进制ASCII值,只需将其转换为int。

答案 4 :(得分:0)

除非您知道String包含有效整数,否则您无法将int转换为String

首先,使用Scanner类进行输入效果更好,因为它更快    如果你是的话,你不需要陷入使用溪流的麻烦    一个初学者。这就是Scanner用于获取输入的方式:

import java.util.Scanner; // this is where the Scanner class resides

...

Scanner sc = new Scanner(System.in); // "System.in" is the stream, you could also pass a String, or a File object to take input from
System.out.println("Would you like to ... Enter 'Y' or 'N':");
String input = sc.next();
input = input.toUpperCase();
char choice = sc.charAt(0);
if(choice == 'Y') 
{ } // do something

else if(choice == 'N')
{ } // do something

else
System.err.println("Wrong choice!");

此代码也可缩短为一行(但您不会    能够检查第三个“错误选择”条件):

if ( new Scanner(System.in).next().toUpperCase().charAt(0) == 'Y')
      { } // do something
   else  // for 'N'
      { } // do something

其次charint转换只需要一个显式类型    投:

   char ch = 'A';
   int i = (int)ch;  // explicit type casting, 'i' is now 65 (ascii code of 'A')

第三,即使您从缓冲的输入流中获取输入,也是如此    将在String中输入。从中提取第一个字符    字符串并检查它,只需要调用charAt()    以 0 作为参数运行。它返回一个字符,可以    然后用单引号比较单个字符:

   String s = in.readLine();
   if(s.charAt(0) == 'Y') { } // do something

第四,将整个程序放在try中是一个非常糟糕的主意    最后阻止catch ExceptionIOException可以    由readline()函数抛出,而parseInt()可能抛出一个    NumberFormatException,所以你将无法处理2    另外例外。在这个问题中,代码足够小    这被忽略了,但在实践中,会有很多功能    可以抛出异常,因此很容易忘记究竟哪个函数抛出了什么异常和适当的异常处理变得非常困难。