We help IT Professionals succeed at work.
Get Started

Java --- Adding Numbers in a loop

awolarczuk
awolarczuk asked
on
326 Views
Last Modified: 2012-04-22
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(System.in);  //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();
            
            
         
            mystery(i);
            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[])
   {
      
      runProgram();
   }
}

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE