0

Thank you for looking at my code. I am learning java and have run into an issue that is driving me crazy.

/*
 * The loop reads positive integers from standard input and that
 * terminates when it reads an integer that is not positive. After the loop
 * terminates, it prints out, separated by a space and on a single line, the
 * sum of all the even integers read and the sum of all the odd integers
 */

The thing is that the variables are not adding! I know my syntax is good. I think there something about the java language that I don't understand with how loop works and adds.

import java.util.Scanner;

class Testing2 {
    public static void main(String[] args) {
        int sumP = 0;
        int sumO = 0;

        Scanner stdin = new Scanner(System.in);

        System.out.println("Enter a positive or negative integer: ");

        while ((stdin.nextInt()) >= 0) {
            if (stdin.nextInt() % 2 == 0)
                sumP += stdin.nextInt();
            else
                sumO += stdin.nextInt();
        }
        System.out.println(sumP + " " + sumO);
        stdin.close();

    }
};
1
  • Are you sure? Why not add few System.out.println() statements to trace out execution? Commented Sep 30, 2013 at 4:28

7 Answers 7

3

Every time you call stdin.nextInt() it is looking for another integer. To avoid this, at the top set a variable equal to the input:

int myInt = stdin.nextInt();

if (myInt >= 0) {
 if (myInt % 2 == 0)
                sumP += myInt;
            else
                sumO += myInt;
        }
        System.out.println(sumP + " " + sumO);
        stdin.close();

    }
}

You do not put the semicolon after the last curly brace as well. If you are expecting multiple numbers to be input you can continually check for the next it with,

while(stdin.hasNext()){
int myInt = stdin.nextInt();
}
Sign up to request clarification or add additional context in comments.

4 Comments

I think you meant to replace a few more of those nextInt() calls with myInt, didn't you?
Yes sorry, edited it to use the variable instead of stdin.nextInt()
import java.util.Scanner; class Testing2 { public static void main(String[] args) { int sumP = 0; int sumO = 0; System.out.println("Enter a positive or negative integer: "); Scanner stdin = new Scanner(System.in); while (stdin.hasNext()) { int myInt = stdin.nextInt(); if (myInt >= 0) { if (myInt % 2 == 0) sumP += myInt; else sumO += myInt; } System.out.println(sumP + " " + sumO); } stdin.close(); } };
Thank you for your time. Your help definitely helped!
1

I think this solves the problem,

Scanner stdin = new Scanner(System.in);
while(1)
{
int num = stdin.nextInt();
if(num<0)
{
     stdin.close();
     break;
}
else
{
  if(num%2==0)
  {
  //Initialize sumP and sumO to 0
  sumP=sumP+num;
  }
  else
  {
  sumO=sumO+num;
  }
}
//You can now output sumP and sum) outside the loop safely.
}

Comments

1

The problem is that you are using nextInt() every time. Use like this-

import java.util.Scanner;

class Testing2 {
    public static void main(String[] args) {
        int sumP = 0;
        int sumO = 0;

        Scanner stdin = new Scanner(System.in);

        System.out.println("Enter a positive or negative integer: ");
        int temp;
        while ((temp=stdin.nextInt())>0) {
            if (temp % 2 == 0)
                sumP += temp;
            else
                sumO += temp;
        }
        System.out.println(sumP + " " + sumO);
        stdin.close();

    }
}

Comments

0
while ((stdin.nextInt()) >= 0) {
    if (stdin.nextInt() % 2 == 0)
       sumP += stdin.nextInt();
    else
       sumO += stdin.nextInt();
}

Your problem is that you're reading in 3 numbers each time you loop. Store the result of your read and then decided what to do with it, don't discard it and read 2 more numbers.

int nextInt;
while ((nextInt = stdin.nextInt()) >= 0) {
    // Do things with nextInt
}

Comments

0

I think you want to need coding like this

import java.util.Scanner;

class TestScaner {
public static void main(String[] args) {
    int sumP = 0;
    int sumO = 0;
    Scanner stdin = null;
    while (true) {
        stdin = new Scanner(System.in);

        System.out.println("Enter a positive or negative integer: ");

        int num = stdin.nextInt();

        if (num % 2 == 0)
            sumP += num;
        else
            sumO += num;

        System.out.println("==" + sumP + " " + sumO);

    }

}

};

Comments

0

Please try these code

import java.util.Scanner;

class Testing2 {
    public static void main(String[] args) {
    int sumP = 0;
    int sumO = 0;
    int scn = 0;

    Scanner stdin = new Scanner(System.in);

    System.out.println("Enter a positive or negative integer: ");

    scn = stdin.nextInt();
   while (scn >= 0) {

       System.out.println("stdin next " + scn);
        if (scn % 2 == 0){
            sumP += scn;
        }else{
           sumO += scn;
        }
       scn--;
    }
    System.out.println(sumP + " " + sumO);


   }
};

your stdin.nextInt() does not decrease it only return a value of your stdin that is why it doesn't loop properly.

Comments

0

u also should put scanner inside while:

 do {
 Scanner stdin = new Scanner(System.in);
            if (stdin.nextInt() % 2 == 0)
                sumP += stdin.nextInt();
            else
                sumO += stdin.nextInt();
        }while ((stdin.nextInt()) >= 0)

2 Comments

This will not even compile. You cannot define stdin twice. Also this is not a solution of problem
yeah. what i want to say is scanner must put inside loop.

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.