0

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); 
    }
}
5
  • 2
    Care explaining where you are getting error if any? Commented Sep 23, 2013 at 18:20
  • Im getting an error when I try to parse the string to an integer Commented Sep 23, 2013 at 18:21
  • 1
    what kind of error/exception? Possibly post an error log. Commented Sep 23, 2013 at 18:22
  • If you use 53429 then String s6 = s1.substring(4,5); will generate ArrayIndexOutOfBoundExceptions. Commented Sep 23, 2013 at 18:28
  • 1
    Just for fun, I made a one-line Python solution at ideone.com/NP8Wxp Commented Sep 23, 2013 at 18:35

4 Answers 4

6

I'm willing to bet that you have a class named Integer, and Java is trying to use that rather than java.lang.Integer.

Rename your class, or use java.lang.Integer.parseInt(s3) instead.

Sign up to request clarification or add additional context in comments.

2 Comments

I am inclining to this answer, I tried copy pasting OP's code and I got no errors whatsoever..
@KubaSpatny Except that the sum variable isn't defined in the pasted code :)
2

code to add even placed chars in the string.

 String str="1234567";
        int sum=0;
        for(int i=1;i<str.length();i=i+2){
            sum+=(str.charAt(i)-'0');
        }
        System.out.println(sum);

And we can also take from keyboard and start the calculation:

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Please enter number : ");
    String number=null;
    try{
        number = reader.readLine();
    } 
    catch (IOException e) {
        e.printStackTrace();
        throw e;
    }
    int sum=0;
    for(int i=1;i<number.length();i=i+2){
        sum+=(number.charAt(i)-'0');
    }
    System.out.println(sum);

Comments

1

I ran the program and it works fine... if you uncomment the lines int b and sum = (a + b);. However, you have to declare the sum variable, i.e. int sum = (a + b);

What JDK are you running? The error you describe would only occur if Integer.parseInt(String foo); didn't exist, except it's been around since at least Java 1.4, so I'm not sure why you wouldn't find it; unless you have another Integer class defined in the same package, which could confuse the compiler.

Here is the complete program, including imports (which may be the problem, if you're importing a different Integer than java.lang.Integer), fixing the variable declaration and removing unnecessary code, fixing indentation, and adding a Scanner.close() statement:

import java.util.Scanner;

public class Test {
    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); 
        } 

        String s3 = s1.substring(1,2);
        String s5 = s1.substring(3,4);

        int a = Integer.parseInt(s3);
        int b = Integer.parseInt(s5);

        int sum = (a + b);

        System.out.println("The sum of all even positions is " + sum); 

        keyboard.close();
    }
}

Comments

0

The Modified code. Try understanding it.

import java.util.Scanner;


public class EvenPos {

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); 
    } 

else{

char a = s1.charAt(1);
char b = s1.charAt(3);
int q = Character.getNumericValue(a); //Convert Char to Integer
int z = Character.getNumericValue(b); // //Convert Char to Integer

int sum = 0;
if (q % 2 == 0 && z % 2 == 0){ //If both even, then.....
    sum = q+z;
    System.out.println("Your sum: " + sum);
}
else{
    System.out.println("No even Number found at even POS");
}

}

} }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.