Im writing a code that will will take the entered number and only add the values that are in the even positions.
For example:
If user enters 53429 The sum of of the even positions is 5.
My issue is I'm trying to convert the strings of the even positions back into integers and add them together. This is what I have
I keep receiving an error when I try to parse the string to an integer.
Cannot find symbol
symbol : method parseInt(java.lang.String)
location: class Integer
Code:
import java.util.Scanner;
public class NumberSums {
public static void main(String [] args) {
Scanner keyboard=new Scanner(System.in);
System.out.print("Enter a number: ");
String x=keyboard.next();
String s1 = x;
int length = s1.length();
if (length<5) {
System.out.println("Invalid value");
System.exit(0);
}
if (length>5) {
System.out.println("Invalid value");
System.exit(0);
}
String s2 = s1.substring(0,1);
String s3 = s1.substring(1,2);
String s4 = s1.substring(2,3);
String s5 = s1.substring(3,4);
String s6 = s1.substring(4,5);
int a = Integer.parseInt(s3);
//int b = Integer.parseInt(s5);
//sum = (a + b);
System.out.println("The sum of all even positions is " + sum);
}
}