打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
线程“main”中的异常java.lang.NumberFormatException:对于输入字符串:“t1”?

我是Java的新手,我在这里遇到了这个问题.我会发布链接并记住,这里的其他类似问题对我没有帮助,因为我有不同的代码,因此我在这里创建了这个帐户.

码:

package secret.package.guys;import java.util.Scanner;public class NewClass {public static void main(String[] args) {    System.out.println("Number: ");    Scanner scanner = new Scanner(System.in);     String data=scanner.nextLine();     System.out.println(data);    int a = 0;     while(a < 6) {        System.out.println(a);        a  ;         }    if (a > 6){         System.out.println("SAFE SPACE");    } else {        System.out.println("Get in the Safe Space");        System.out.println("PERSON has entered the Safe Space. Safe Space closes instantly.");    }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     String s = new String("Old marks: ");    String t = new String("5.5");     String u = new String("4");     String v = new String("3");     String w = new String("2.5");     String x = new String("6.0");     String y = new String("5.2");     String z = new String("4");     String t1 = t.replaceAll("5.5", "6");    String u1 = u.replaceAll("4", "4");    String v1 = v.replaceAll("3", "5");    String w1 = w.replaceAll("2.5", "3");    String x1 = x.replaceAll("6.0", "2");    String y1 = y.replaceAll("5.2", "1.8");    String z1 = z.replaceAll("4", "4.4");    System.out.println("New: "   s   " "   t1   " "   u1   " "   v1   " "   w1   " "       x1   " "   y1   " "   z1);    System.out.println("Enter new marks: ");    int foo = Integer.parseInt("t1");     int foo1 = Integer.parseInt("u1");     int foo2 = Integer.parseInt("v1");     int foo3 = Integer.parseInt("w1");     int foo4 = Integer.parseInt("x1");     int foo5 = Integer.parseInt("y1");     int foo6 = Integer.parseInt("z1");     System.out.println("foo1   foo2   foo3   foo4   foo5   foo6");}   

}

解决方法:

您必须在String变量的值上调用parseInt,而不是在变量名称上调用.

例如,

int foo = Integer.parseInt("t1"); 

应该

int foo = Integer.parseInt(t1); 

这只有在t1包含整数的String表示时才会起作用(这意味着parseInt(z1)和parseInt(y1)仍会失败,因为这些字符串不包含整数).

来源:https://www.icode9.com/content-1-405001.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
常用的基本进制转换
权限管理二进制算法(***)
JAVA Integer类
Java 异常的捕获与处理详解(一)
java的四种输入方法,你会几种?
String,Integer,int之间转换
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服