java输入字符串异常_线程“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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值