Hello, I am pretty new to programming and needed some help with one of my programs. I am trying to have the user input how many numbers they want to plug in then they plug in x amount of numbers until they reach whatever they input. After that I am trying to create three methods which do different functions. One method calculates the sum of all the input numbers. Second method calculates the average of all input numbers. Fourth method displays all of the results. I am not looking for someone to do all o the work for me, but I am seeking guidance to figure out how I would use information from one method in another without using arrays (we haven't gotten that far in the class yet, and I want to learn this).

This is the code I have so far:

// Import JOptionPane
import javax.swing.JOptionPane;
        
public class hidingmyname {
    public static void main(String[] args) {
        
        // Declare variables
        int LoopEnd = 1;
        double InputNumbers;
        
        // Asks user how many numbers they would like to input
        String HowManyNumbersstring = JOptionPane.showInputDialog(null, "How many numbers would you like to input?", "?", JOptionPane.QUESTION_MESSAGE);
        
        // Convert string into integer
        int HowManyNumbers = Integer.parseInt(HowManyNumbersstring);
        
        // Output message notifying how many numbers the user is plugging in
        System.out.println("Your " + HowManyNumbers + " numbers are: ");
        do {
        // Prompts user to input their integers
        String InputNumbersstring = JOptionPane.showInputDialog(null, "Enter an integer: ", "Input", JOptionPane.QUESTION_MESSAGE);
        
        // Convert string into double
        InputNumbers = Double.parseDouble(InputNumbersstring);
        
        // If statement for outputting numbers for user to see in display box
            if (HowManyNumbers == LoopEnd){
                System.out.println(InputNumbers + ".");
                LoopEnd++;
            }
            else if (LoopEnd % 10 == 0){
                System.out.println(InputNumbers + ". ");
                LoopEnd++;
            }
            else {
                System.out.print(InputNumbers + ", ");
                LoopEnd++;
            }
        } while (LoopEnd <= HowManyNumbers);
        
        System.out.println("The sum of all your numbers is " + calculateSum(HowManyNumbers, InputNumbers, LoopEnd));
    }
        
    // Create new method to add all numbers that were input by user
    public static double calculateSum(int HowManyNumbers, double InputNumbers, int LoopEnd){
        double calculateSum = 0.0;
        int sum = 0;
            
            while (LoopEnd <= HowManyNumbers){
                calculateSum = sum + InputNumbers;
            }
            
            return calculateSum;   
    }
}

At the moment I am having issues trying to get the input values from the first method over to the second method to calculate the sum. It seems like the while loop in the second method is not adding like I want it to.

Any and all help is appreciated.

Hello, I am pretty new to programming and needed some help with one of my programs. I am trying to have the user input how many numbers they want to plug in then they plug in x amount of numbers until they reach whatever they input. After that I am trying to create three methods which do different functions. One method calculates the sum of all the input numbers. Second method calculates the average of all input numbers. Fourth method displays all of the results. I am not looking for someone to do all o the work for me, but I am seeking guidance to figure out how I would use information from one method in another without using arrays (we haven't gotten that far in the class yet, and I want to learn this).

This is the code I have so far:

// Import JOptionPane
import javax.swing.JOptionPane;
        
public class hidingmyname {
    public static void main(String[] args) {
        
        // Declare variables
        int LoopEnd = 1;
        double InputNumbers;
        
        // Asks user how many numbers they would like to input
        String HowManyNumbersstring = JOptionPane.showInputDialog(null, "How many numbers would you like to input?", "?", JOptionPane.QUESTION_MESSAGE);
        
        // Convert string into integer
        int HowManyNumbers = Integer.parseInt(HowManyNumbersstring);
        
        // Output message notifying how many numbers the user is plugging in
        System.out.println("Your " + HowManyNumbers + " numbers are: ");
        do {
        // Prompts user to input their integers
        String InputNumbersstring = JOptionPane.showInputDialog(null, "Enter an integer: ", "Input", JOptionPane.QUESTION_MESSAGE);
        
        // Convert string into double
        InputNumbers = Double.parseDouble(InputNumbersstring);
        
        // If statement for outputting numbers for user to see in display box
            if (HowManyNumbers == LoopEnd){
                System.out.println(InputNumbers + ".");
                LoopEnd++;
            }
            else if (LoopEnd % 10 == 0){
                System.out.println(InputNumbers + ". ");
                LoopEnd++;
            }
            else {
                System.out.print(InputNumbers + ", ");
                LoopEnd++;
            }
        } while (LoopEnd <= HowManyNumbers);
        
        System.out.println("The sum of all your numbers is " + calculateSum(HowManyNumbers, InputNumbers, LoopEnd));
    }
        
    // Create new method to add all numbers that were input by user
    public static double calculateSum(int HowManyNumbers, double InputNumbers, int LoopEnd){
        double calculateSum = 0.0;
        int sum = 0;
            
            while (LoopEnd <= HowManyNumbers){
                calculateSum = sum + InputNumbers;
            }
            
            return calculateSum;   
    }
}

At the moment I am having issues trying to get the input values from the first method over to the second method to calculate the sum. It seems like the while loop in the second method is not adding like I want it to.

Any and all help is appreciated.

not sure if this is what you want but you can create methods like you did with the calculateSum method which returns a double, so you would return values from the method and assign them kind of like this:

public static void main(String[] args) {
        String messagefrommethod= method();//here is where we assign the return value to a variable to use in another method
        System.out.println(messagefrommethod);//this will print hello
    }

    private static String method() {//this method returns a string
        String message="hello";
        return message;//this is where it returns the string and is assigned to another variable in main
    }

here is the first part of your problem :

...
        } while (LoopEnd <= HowManyNumbers);
        //at this point LoopEnd is BIGGER than HowManyNumbers,
        //because you exited the do-while
        ... calculateSum(HowManyNumbers, InputNumbers, LoopEnd));
    } 

    public static double calculateSum(int HowManyNumbers, double InputNumbers, int LoopEnd) 
    {
        double calculateSum = 0.0;
        int sum = 0;
        while (LoopEnd <= HowManyNumbers){ 
        //here you try to loop but the last time you used LoopEnd was to exit the
        //while in the main, so its STILL bigger than HowManyNumbers
        ...

so first thing would be to reset LoopEnd to 1 before sending it to the calculateSum() method but it wouldn't fix it, AND it is useless , because you know you are gonna loop from (1 or 0) 1 in your case since your not using arrays, to HowManyNumbers.

the second problem is you are only sending the LAST number "InputNumbers" to the method, so it can't add them all.

you dont really need methods for what you are trying to do, simply declare a variable "sum" and everytime the user inputs a number : sum += InputNumbers;

then at the end of your do-while simply calculate the average with : sum / HowManyNumbers;

if you NEED to use methods for your assignement then the most logical(but overkill) choice would be to program a simple add() method :

public double addNumbers(double x, double y){
    return x+y;
}

and everytime user inputs a new number instead of "sum += InputNumbers;" use "sum = addNumbers( sum , InputNumbers );"

one last comment, it doesn't generate any errors not to do this but by convention and for your sake : use Capital Letters to start Class names and lower char to start variable names.

Naming Convention
CamelCase
Hungarian Notation

not sure if this is what you want but you can create methods like you did with the calculateSum method which returns a double, so you would return values from the method and assign them kind of like this:

public static void main(String[] args) {
        String messagefrommethod= method();//here is where we assign the return value to a variable to use in another method
        System.out.println(messagefrommethod);//this will print hello
    }

    private static String method() {//this method returns a string
        String message="hello";
        return message;//this is where it returns the string and is assigned to another variable in main
    }

In order for that to work I would still need to pass/call the information from the Input method to the calculateSum method I have though, wouldn't I? I believe that is the one thing that is stumping me at the moment. I am mainly trying to figure out how I would input data, and at the same time the calculateSum method will add them as the data is input. I am trying to keep them two separate methods, then I will have a third method that will display the results.

In order for that to work I would still need to pass/call the information from the Input method to the calculateSum method I have though, wouldn't I? I believe that is the one thing that is stumping me at the moment. I am mainly trying to figure out how I would input data, and at the same time the calculateSum method will add them as the data is input. I am trying to keep them two separate methods, then I will have a third method that will display the results.

just keep calling the method and storing your answer in a static variable(s)?
here is an example but i didnt use a static variable as i had no need for it:

public static void main(String[] args) {
       int answer= calculate(3,5);
        answer += calculate(1,2);//will add on to previous answer if you dont want that then use int answer2= calculate(1,2);
        answer += calculate(3,7);
        answer += calculate(2,9);
        System.out.println(answer);
    }

    private static int calculate(int int1,int int2) {
        return int1+int2;
    }

just keep calling the method and storing your answer in a static variable(s)?
here is an example but i didnt use a static variable as i had no need for it:

public static void main(String[] args) {
       int answer= calculate(3,5);
        answer += calculate(1,2);//will add on to previous answer if you dont want that then use int answer2= calculate(1,2);
        answer += calculate(3,7);
        answer += calculate(2,9);
        System.out.println(answer);
    }

    private static int calculate(int int1,int int2) {
        return int1+int2;
    }

This is the overview of my assignment. Hoping this will help others understand the situation and be able to guide me in the right direction.

"Design and implement a Java program that will gather a floating point numbers and determine the sum and average of the data entered. The program should use separate methods for inputting the data, calculating the sum, calculating the average, and displaying the results. A sentinel value should be used to indicate the user has completed entering their numbers. The output should display a message that includes the count of the numbers entered, the sum of the numbers and the average of the numbers."

From what I understand I need to create a method where I input numbers, and those numbers are then taken from that method and plugged into the sum and average methods. After that I have a 4th method which will display the results. My biggest problem is figuring out how to use information from one method in another. I've been trying Philippe.Lahaie suggestion since that is exactly what I want it seems, but getting a little tired and burnt out at the moment so will have to take a nap and come back to it later today.

There is only a need for one while loop in your assignment. Try using the while in the main, and call the methods to input and calculate the sum from within it.

I think this solves your problem...
Code:
/**
*
* @author gowtham
*/
import javax.swing.JOptionPane;

public class hidingmyname {
static double calculateSum=0.0,s=0.0;
public static void main(String[] args) {

// Declare variables
int LoopEnd = 0;
double InputNumbers;


// Asks user how many numbers they would like to input
String HowManyNumbersstring = JOptionPane.showInputDialog(null, "How many numbers would you like to input?", "?", JOptionPane.QUESTION_MESSAGE);

// Convert string into integer
int HowManyNumbers = Integer.parseInt(HowManyNumbersstring);

// Output message notifying how many numbers the user is plugging in
System.out.println("Your " + HowManyNumbers + " numbers are: ");
do {
// Prompts user to input their integers
String InputNumbersstring = JOptionPane.showInputDialog(null, "Enter an integer: ", "Input", JOptionPane.QUESTION_MESSAGE);

// Convert string into double
InputNumbers = Double.parseDouble(InputNumbersstring);
System.out.print(InputNumbers+", ");
s=calculateSum(InputNumbers);
//System.out.println("s="+s);
// If statement for outputting numbers for user to see in display box
/*if (HowManyNumbers == LoopEnd){
System.out.println(InputNumbers + ".");
LoopEnd++;
}
else if (LoopEnd % 10 == 0){
System.out.println(InputNumbers + ". ");
LoopEnd++;
}
else {
System.out.print(InputNumbers + ", ");
LoopEnd++;
}*/
LoopEnd++;
//System.out.println(LoopEnd);
} while (LoopEnd != HowManyNumbers);
System.out.println("\n"+"Answer->"+s);
//System.out.println("The sum of all your numbers is " + calculateSum(HowManyNumbers, InputNumbers, LoopEnd));
}

// Create new method to add all numbers that were input by user
/*public static double calculateSum(int HowManyNumbers, double InputNumbers, int LoopEnd){
double calculateSum = 0.0;
int sum = 0;

while (LoopEnd <= HowManyNumbers){
calculateSum = sum + InputNumbers;
}

return calculateSum;
}*/
public static double calculateSum(double InputNumbers){
calculateSum+=InputNumbers;
//System.out.println(calculateSum);
return calculateSum;
}
}


I think you are complicating this process .. for practice you are doing like this or what frnd!!!! We can achieve the same result with one method and even faster than this dude...

commented: doesnt help to give the OP the code -1

I think this solves your problem...
Code:
/**
*
* @author gowtham
*/
import javax.swing.JOptionPane;

public class hidingmyname {
static double calculateSum=0.0,s=0.0;
public static void main(String[] args) {

// Declare variables
int LoopEnd = 0;
double InputNumbers;


// Asks user how many numbers they would like to input
String HowManyNumbersstring = JOptionPane.showInputDialog(null, "How many numbers would you like to input?", "?", JOptionPane.QUESTION_MESSAGE);

// Convert string into integer
int HowManyNumbers = Integer.parseInt(HowManyNumbersstring);

// Output message notifying how many numbers the user is plugging in
System.out.println("Your " + HowManyNumbers + " numbers are: ");
do {
// Prompts user to input their integers
String InputNumbersstring = JOptionPane.showInputDialog(null, "Enter an integer: ", "Input", JOptionPane.QUESTION_MESSAGE);

// Convert string into double
InputNumbers = Double.parseDouble(InputNumbersstring);
System.out.print(InputNumbers+", ");
s=calculateSum(InputNumbers);
//System.out.println("s="+s);
// If statement for outputting numbers for user to see in display box
/*if (HowManyNumbers == LoopEnd){
System.out.println(InputNumbers + ".");
LoopEnd++;
}
else if (LoopEnd % 10 == 0){
System.out.println(InputNumbers + ". ");
LoopEnd++;
}
else {
System.out.print(InputNumbers + ", ");
LoopEnd++;
}*/
LoopEnd++;
//System.out.println(LoopEnd);
} while (LoopEnd != HowManyNumbers);
System.out.println("\n"+"Answer->"+s);
//System.out.println("The sum of all your numbers is " + calculateSum(HowManyNumbers, InputNumbers, LoopEnd));
}

// Create new method to add all numbers that were input by user
/*public static double calculateSum(int HowManyNumbers, double InputNumbers, int LoopEnd){
double calculateSum = 0.0;
int sum = 0;

while (LoopEnd <= HowManyNumbers){
calculateSum = sum + InputNumbers;
}

return calculateSum;
}*/
public static double calculateSum(double InputNumbers){
calculateSum+=InputNumbers;
//System.out.println(calculateSum);
return calculateSum;
}
}


I think you are complicating this process .. for practice you are doing like this or what frnd!!!! We can achieve the same result with one method and even faster than this dude...

That helped me out a ton. Your program works perfectly for what I want to do, but I do want to finish my program for the learning process. I know that this can be done with one method in less than 15 minutes, but the assignment strictly requires me to create Method 1: Calculating Sum, Method 2: Calculating Average, Method 3: Displaying Results, Method 4(Main): Input Data. With that said I believe I am very close to finishing this assignment, but I ran into a hickup in my program. My method is not adding the InputNumbers properly. It seems like it is only adding the last InputNumber and displaying it as the sum of all numbers instead of adding all input numbers like I want it to.

Below is the current code I have

// Christopher Jewett CMIS141 Project 2
package christopherjewettp2;

// Import JOptionPane
import javax.swing.JOptionPane;
        
public class Hidingmyname {
    public static void main(String[] args) {
        
        // Declare variables
        int LoopEnd = 1;
        double InputNumbers;
        double larger;
        
        // Asks user how many numbers they would like to input
        String HowManyNumbersstring = JOptionPane.showInputDialog(null, "How many numbers would you like to input?", "?", JOptionPane.QUESTION_MESSAGE);
        
        // Convert string into integer
        int HowManyNumbers = Integer.parseInt(HowManyNumbersstring);
        
        // Output message notifying how many numbers the user is plugging in
        System.out.println("Your " + HowManyNumbers + " numbers are: ");
       
        // While loop for user to input 
        while (LoopEnd <= HowManyNumbers){
        
        // Prompts user to input their integers
        String InputNumbersstring = JOptionPane.showInputDialog(null, "Enter an integer: ", "Input", JOptionPane.QUESTION_MESSAGE);
        
        // Convert string into double
        InputNumbers = Double.parseDouble(InputNumbersstring);
        
        // Declare larger variable as calculateSum method
        larger = (calculateSum(InputNumbers, LoopEnd, HowManyNumbers)); 
        
        // If statement for outputting numbers for user to see in display box
            if (HowManyNumbers == LoopEnd){
                System.out.println(InputNumbers + ".");
                System.out.println("The sum of all your numbers is " + larger + ".");
                LoopEnd++;
            }
            else if (LoopEnd % 10 == 0){
                System.out.println(InputNumbers + ". ");
                LoopEnd++;
            }
            else {
                System.out.print(InputNumbers + ", ");
                LoopEnd++;
            }
            
        }
    }
        
    // Create new method to add all numbers that were input by user
    public static double calculateSum(double InputNumbers, int LoopEnd, int HowManyNumbers){
    
    // Declare variables  
    double sum = 0; 
    
    // Loop to calculate sum of input numbers 
    if (LoopEnd <= HowManyNumbers){ 
    sum = sum + InputNumbers; 
    LoopEnd++;
    } 
    // Returns value to use in other methods 
    return sum; 
    }
}

That helped me out a ton. Your program works perfectly for what I want to do, but I do want to finish my program for the learning process. I know that this can be done with one method in less than 15 minutes, but the assignment strictly requires me to create Method 1: Calculating Sum, Method 2: Calculating Average, Method 3: Displaying Results, Method 4(Main): Input Data. With that said I believe I am very close to finishing this assignment, but I ran into a hickup in my program. My method is not adding the InputNumbers properly. It seems like it is only adding the last InputNumber and displaying it as the sum of all numbers instead of adding all input numbers like I want it to.

Below is the current code I have

// Christopher Jewett CMIS141 Project 2
package christopherjewettp2;

// Import JOptionPane
import javax.swing.JOptionPane;
        
public class Hidingmyname {
    public static void main(String[] args) {
        
        // Declare variables
        int LoopEnd = 1;
        double InputNumbers;
        double larger;
        
        // Asks user how many numbers they would like to input
        String HowManyNumbersstring = JOptionPane.showInputDialog(null, "How many numbers would you like to input?", "?", JOptionPane.QUESTION_MESSAGE);
        
        // Convert string into integer
        int HowManyNumbers = Integer.parseInt(HowManyNumbersstring);
        
        // Output message notifying how many numbers the user is plugging in
        System.out.println("Your " + HowManyNumbers + " numbers are: ");
       
        // While loop for user to input 
        while (LoopEnd <= HowManyNumbers){
        
        // Prompts user to input their integers
        String InputNumbersstring = JOptionPane.showInputDialog(null, "Enter an integer: ", "Input", JOptionPane.QUESTION_MESSAGE);
        
        // Convert string into double
        InputNumbers = Double.parseDouble(InputNumbersstring);
        
        // Declare larger variable as calculateSum method
        larger = (calculateSum(InputNumbers, LoopEnd, HowManyNumbers)); 
        
        // If statement for outputting numbers for user to see in display box
            if (HowManyNumbers == LoopEnd){
                System.out.println(InputNumbers + ".");
                System.out.println("The sum of all your numbers is " + larger + ".");
                LoopEnd++;
            }
            else if (LoopEnd % 10 == 0){
                System.out.println(InputNumbers + ". ");
                LoopEnd++;
            }
            else {
                System.out.print(InputNumbers + ", ");
                LoopEnd++;
            }
            
        }
    }
        
    // Create new method to add all numbers that were input by user
    public static double calculateSum(double InputNumbers, int LoopEnd, int HowManyNumbers){
    
    // Declare variables  
    double sum = 0; 
    
    // Loop to calculate sum of input numbers 
    if (LoopEnd <= HowManyNumbers){ 
    sum = sum + InputNumbers; 
    LoopEnd++;
    } 
    // Returns value to use in other methods 
    return sum; 
    }
}

I finally managed to get the code to work according to the assignment instructions. Thanks everyone for the help as I did use a lot of the information in previous posts. I'm not completely finished but I'm well on my way to it. Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.