I'm new to Java and I've got this problem here. I'll post the link and keep in mind, that the other similar questions here couldn't helped me since I have a different code, and because of that I made this account here.
Code:
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");
}
}
You have to call parseInt on the value of the String variable, not on the variable name.
For example,
int foo = Integer.parseInt("t1");
should be
int foo = Integer.parseInt(t1);
And that would only work if t1 contains a String representation of an integer (this means parseInt(z1) and parseInt(y1) would still fail since those Strings don't contain integers).
Your code tries to parse a String into a numerical value.
With the value "1" it would work, but since "t1" is not a valid numerical value, it will fail, and result in this Exception.
What I assume you want is, instead of:
int foo = Integer.parseInt("t1");
where you pass "t1" as a String
int foo = Integer.parseInt(t1);
with the reference variable t1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With