troubleshooting Question

Java --- Adding Numbers in a loop

Avatar of awolarczuk
awolarczukFlag for Australia asked on
3 Comments1 Solution327 ViewsLast Modified:
Hi all i will try to explain this as best as i can

i am passing the keyboard in a class called runprogram, i am then passing this to a class called
mystery.  This is in a loop, what i need to do is add up all the number that are entered and keep track of them untill i enter a netive number and then the program uses my count of postive number and find the average.  like i said i hae the count and the retune working but for the life of me i cant get the 1+2+3+4+5...... working.

Here is my code could someone please help me of this, thanks in advance
// Example Test (S1234567): Program output is 4.714285...
// Test (Enter your student number here): Program output is ...
import java.util.Scanner; // Needed for the scanner class 

// You can add import statements here if you wish
public class Assignment2

   // Do not make any modifications above this line
   public static int mystery(String i)
      double a = new Integer(i);
      double c = 0; 
      double b = 0;
	System.out.println("Testing i" + " " +  i);
            b = (a + a);
            c = new Integer(i);
            System.out.println("Testing a" + " " +  a);
            System.out.println ("ading numbers" + " " +b); 
             //  System.out.println ("avg" + " " + (b / index));
      return (int) b; // You will need to modify this line

   public static void runProgram()
      String i; // to get the keyboard input
      double index = 0; //This will hold the count
       do {  
            Scanner keyboard = new Scanner(;  //Create a Scabber object to read input
            System.out.print("\nPlease enter a Number! "); //get the input from the user from the keyboard
            i = keyboard.nextLine();
            index ++;
         //   System.out.println ("counting the number" + " " + (index  ));
         //   System.out.println ("avg" + " " + (mystery(i) / index));

 }while (new Integer(i) > 0);

   // Do not make any modifications below this line

   public static void main(String args[])
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros