Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Looping doesn't change value

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Looping doesn't change value

    Okay guys so here is my problem.
    Write a program that asks the user for the low and high integer in a range of integers. The program then asks the user for integers to be added up. The program computes two sums:

    The sum of integers that are in the range (inclusive),
    and the sum of integers that are outside of the range.
    The user signals the end of input with a 0.

    Your output should look like this:

    Sample input/output

    In-range Adder Low end of range: 20
    High end of range: 50
    Enter data: 21
    Enter data: 60
    Enter data: 49
    Enter data: 30
    Enter data: 91
    Enter data: 0
    Sum of in range values: 100
    Sum of out of range values: 151

    This is my assignment and below is my code.

     
    import java.util.Scanner;
    class InRangeAdder {
      public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
        int low, high, data=1, rangesum=0, outrangesum=0;
        System.out.println("Low end of range:");
        low = scan.nextInt();
        System.out.println("High end of range:");
        high = scan.nextInt();
        while (data!=0) {
          System.out.println("Enter data:");
          data = scan.nextInt(); 
        } 
        if (data >= low && data <= high) {
          rangesum= rangesum+data;
        } 
        if (data > high || data < low) {
          outrangesum=outrangesum+data;
        }
      System.out.println("Sum of in range values: " +rangesum);
      System.out.println("Sum of out of range values: " +outrangesum);
    }
    }

    The problem is that the program simply gives an output of 0 for both "rangesum" and "outrangesum". I don't quite understand why that is.
    Also i have a quick question, the program needs me to end the program when the value of data is 0 but in order to initialize it I need to give it a value. Usually I would give it a value of 0 like I have for rangesum and outrangesum but if I do the program does not run till the loop as it considers the value of data to be 0 and ends the program right away. What would be a work around to this and when do I need to have a value to initialize an integer? for example, I do not need a value for low and high. Is this because the program recognizes that a value is going to be defined but cannot do that for the other integers as they are inside a loop?

    Thanks so much and any feedback is greatly appreciated.
    Cheers.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Looping doesn't change value

    Also i have a quick question, the program needs me to end the program when the value of data is 0 but in order to initialize it I need to give it a value. Usually I would give it a value of 0 like I have for rangesum and outrangesum but if I do the program does not run till the loop as it considers the value of data to be 0 and ends the program right away.
    then equate it to a number other than zero.


    The problem is that the program simply gives an output of 0 for both "rangesum" and "outrangesum". I don't quite understand why that is.
    First, look at this statement in your code:
        while (data!=0) {
          System.out.println("Enter data:");
          data = scan.nextInt(); 
        }
    it just keep over writing the value of your data variable. and the only way to terminate the loop is when it handles a zero value. in short, at the end of your loop, the value of your data variable is zero for sure.

    then look at this statement in your code:
        if (data >= low && data <= high) {
          rangesum= rangesum+data;
        } 
        if (data > high || data < low) {
          outrangesum=outrangesum+data;
        }
    you do the checking and storing outside the while loop, hence the value of data in that part of your code is zero for sure since the while loop only terminates when the data is zero that is why you always got a zero value.

    Try to put those checking inside your loop that might solved your problem

  3. The Following User Says Thank You to dicdic For This Useful Post:

    cod (April 29th, 2014)

  4. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Looping doesn't change value

    Thank you, yes it turns out that the if statements were outside the while loop hence only displaying the output is 0 but as for your first point about the 'while (data!=0)' statement, my code asks it to ADD the value to a variable and end the loop but considering that the value is 0 it shouldn't affect my loop. So I don't understand why that would make a difference in my code. Am I missing something? I don't understand why you would point that part of the code out. (I mean that in the most humble way possible, I'm very new to coding and just trying to learn, hope that inquiry didn't sound offensive.)
    Thank you so much for your help!
    Cheers

  5. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Looping doesn't change value

    but as for your first point about the 'while (data!=0)' statement, my code asks it to ADD the value to a variable and end the loop but considering that the value is 0 it shouldn't affect my loop. So I don't understand why that would make a difference in my code. Am I missing something? I don't understand why you would point that part of the code out.
    What I'm pointing out is just to initialize your data variable to a number not equal to zero. Why? just to make sure that the while loop executes at least once. Actually you did it in your code, but you ask this question in your first post:
    Also i have a quick question, the program needs me to end the program when the value of data is 0 but in order to initialize it I need to give it a value. Usually I would give it a value of 0 like I have for rangesum and outrangesum but if I do the program does not run till the loop as it considers the value of data to be 0 and ends the program right away.
    or maybe I just did not understand what you are pointing out with that question that is why I ended up with a suggestion that made you confused. sorry, actually, I am not good in english.

  6. The Following User Says Thank You to dicdic For This Useful Post:

    cod (May 3rd, 2014)

Similar Threads

  1. Why doesn't this change the variables that are passed in?
    By Dr.Cognitive in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 13th, 2014, 10:35 AM
  2. how to change DB design for a change in java
    By harry7ster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 7th, 2013, 12:57 PM
  3. DO WHILE LOOPING
    By njabulo ngcobo in forum What's Wrong With My Code?
    Replies: 14
    Last Post: May 22nd, 2013, 09:57 AM
  4. Looping Over and Over Again?
    By avalanche72 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 1st, 2012, 05:11 AM
  5. Replies: 24
    Last Post: April 14th, 2009, 03:43 PM