0

This is supposed to be a refresher assignment for start of 2nd semester, and for some reason my program is endlessly looping. The assignment is to take inputs and output the sum of positive integers, the count of even integers, etc.

The only time my program will break the loop is if the first input is 0, if the first input is not 0, no matter what, my program will not output anything.

import java.util.Scanner;

public class Assignment2{
  public static void main(String args[]){

    Scanner in = new Scanner(System.in);
    int min = 0;
    int count = 0;
    int sumpos = 0;
    int evencount = 0;
    int input = in.nextInt();
    int temp = 0;

    while(input != 0){

        count++;

        if(input %2 == 0){
            evencount++;
        }

        if(input > 0){
            temp = sumpos;
            sumpos = temp + input;
        }

        if(input < min){
            min=input;
        }
    }

    System.out.println("The minimum integer is " + min);
    System.out.println("The count of integers is " + count);
    System.out.println("The sum of positive integers is " + sumpos);
    System.out.println("The count of even integers in the sequence is " + evencount);

    in.close(); 

    }
}
YCF_L
  • 49,027
  • 13
  • 75
  • 115
matt kaminski
  • 75
  • 1
  • 5
  • 2
    You need to re-read input from your scanner at some point in your while loop. – azurefrog Jan 13 '17 at 19:21
  • Well, you never change input in the loop so..... if it doesn't start at 0 it will never become 0. – takendarkk Jan 13 '17 at 19:24
  • After you fix this error, study the following in every detail: http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo – PM 77-1 Jan 13 '17 at 19:28

1 Answers1

0
    int input = in.nextInt();
    int temp = 0;
    while(input != 0){ //

        count++;

        if(input %2 == 0){
            evencount++;
        }

        if(input > 0){
            temp = sumpos;
            sumpos = temp + input;
        }

        if(input < min){
            min=input;
        }


  input = in.nextInt();
    }

You need to Re-REad your input. Because it will never break the while loop

Gatusko
  • 2,253
  • 1
  • 13
  • 23