Calculating average: Converting Double to String Error2019 Community Moderator ElectionHow to generate a random alpha-numeric string?How do I read / convert an InputStream into a String in Java?How to get an enum value from a string value in Java?How can I convert a stack trace to a string?How to split a string in JavaConverting 'ArrayList<String> to 'String[]' in JavaConvert ArrayList<String> to String[] arrayHow do I convert a String to an int in Java?How to convert a char to a String?Why is char[] preferred over String for passwords?

What kind of footwear is suitable for walking in micro gravity environment?

Why doesn't the fusion process of the sun speed up?

Help with identifying unique aircraft over NE Pennsylvania

How do researchers send unsolicited emails asking for feedback on their works?

pipe commands inside find -exec?

Should I be concerned about student access to a test bank?

Asserting that Atheism and Theism are both faith based positions

Do people actually use the word "kaputt" in conversation?

What are the rules for concealing thieves' tools (or items in general)?

Single word to change groups

Air travel with refrigerated insulin

"Marked down as someone wanting to sell shares." What does that mean?

What is the reasoning behind standardization (dividing by standard deviation)?

Turning a hard to access nut?

Is xar preinstalled on macOS?

Do I need to convey a moral for each of my blog post?

Hot air balloons as primitive bombers

Friend wants my recommendation but I don't want to

Symbolism of 18 Journeyers

Have the tides ever turned twice on any open problem?

Should a narrator ever describe things based on a characters view instead of fact?

When did hardware antialiasing start being available?

Why is "la Gestapo" feminine?

Fair way to split coins



Calculating average: Converting Double to String Error



2019 Community Moderator ElectionHow to generate a random alpha-numeric string?How do I read / convert an InputStream into a String in Java?How to get an enum value from a string value in Java?How can I convert a stack trace to a string?How to split a string in JavaConverting 'ArrayList<String> to 'String[]' in JavaConvert ArrayList<String> to String[] arrayHow do I convert a String to an int in Java?How to convert a char to a String?Why is char[] preferred over String for passwords?










-1















I just can't get my code right how I want to. (Java Beginner)



So the way it should work is this:
When running the program I want to get a window and fill in a number(X) how large(Z) the array should be. Next I get X windows where I have to type in the numbers I want to put in the array. Finally the program should calculate the sum of all numbers and divide by the size(Z) of the array, to get the average of all numbers in the array.



I'm almost finished but all I get are errors regarding Double to String conversion. I tried everything I know out but couldn't make it work. Please excuse there may be spelling mistakes, I had to translate it to english.



Error:
Below //Initialization and Output line, either it's 'void' type not allowed here or incompatible types: void cannot be converted to String



import javax.swing.*;
import java.util.*;

public class Aufgabe42

public static void main(String[] args)

//Declare variable
String input, output, requestNumber;
double[] arrayNumber;
int size;

//Input
input = JOptionPane.showInputDialog(null, "Type in the size of the array.");

//Variable Initialization
size = Integer.parseInt(input);
arrayNumbers = new double[size];

//Request numbers
for(int i = 0; i < size; i++)

requestNumber = JOptionPane.showInputDialog(null, "Tell me a number:");
arrayNumber[i] = Double.parseDouble(requestNumber);


String returnAverage;

//Initialization and Output
output = Double.toString(JOptionPane.showMessageDialog(null, "The Average is: " + getAverage(arrayNumber)));
System.exit(0);


public static double getAverage(double[] arrayNumber)

//Declare variable

double arraySum,average;

//Initialization
arraySum = 0;



returnAverage = Double.toString(average);

//Sum array values
for(int i = 0; i < arrayZNumber.length; i++)

arraySum += arrayNumber[i];


//Calculate Average
average = arraySum / arrayNumber.length;

return average;











share|improve this question
























  • Please edit your question to include the error you're getting and to indicate which line of your code is causing it.

    – azurefrog
    Mar 6 at 23:34











  • I have like 5 versions of the code now. But it always tells me the err is in the //Initialization and Output line, either it's 'void' type not allowed here or incompatible types: void cannot be converted to String

    – Hip
    Mar 6 at 23:38











  • The return type of JOptionPane.showMessageDialog is void: it does not return anything. You cannot use it as an argument to Double.toString(). Why do you think you need to do that?

    – Kevin Anderson
    Mar 7 at 0:30











  • That was recommend by someone on a forum post I have read so I tried this out. I also tried out to convert it like this 'variable = Double.toString(variableOne);' So how can I convert it?

    – Hip
    Mar 7 at 0:41












  • There's no point in assigning something to output immediately before exiting the program. If the last thing you want to do is pop up a dialog to display your result, then just write JOptionPane.showMessageDialog(null, "The average is: ",...);". JOptionPane.showMessageDialog' returns nothing; there's nothing to "convert".

    – Kevin Anderson
    Mar 7 at 0:54
















-1















I just can't get my code right how I want to. (Java Beginner)



So the way it should work is this:
When running the program I want to get a window and fill in a number(X) how large(Z) the array should be. Next I get X windows where I have to type in the numbers I want to put in the array. Finally the program should calculate the sum of all numbers and divide by the size(Z) of the array, to get the average of all numbers in the array.



I'm almost finished but all I get are errors regarding Double to String conversion. I tried everything I know out but couldn't make it work. Please excuse there may be spelling mistakes, I had to translate it to english.



Error:
Below //Initialization and Output line, either it's 'void' type not allowed here or incompatible types: void cannot be converted to String



import javax.swing.*;
import java.util.*;

public class Aufgabe42

public static void main(String[] args)

//Declare variable
String input, output, requestNumber;
double[] arrayNumber;
int size;

//Input
input = JOptionPane.showInputDialog(null, "Type in the size of the array.");

//Variable Initialization
size = Integer.parseInt(input);
arrayNumbers = new double[size];

//Request numbers
for(int i = 0; i < size; i++)

requestNumber = JOptionPane.showInputDialog(null, "Tell me a number:");
arrayNumber[i] = Double.parseDouble(requestNumber);


String returnAverage;

//Initialization and Output
output = Double.toString(JOptionPane.showMessageDialog(null, "The Average is: " + getAverage(arrayNumber)));
System.exit(0);


public static double getAverage(double[] arrayNumber)

//Declare variable

double arraySum,average;

//Initialization
arraySum = 0;



returnAverage = Double.toString(average);

//Sum array values
for(int i = 0; i < arrayZNumber.length; i++)

arraySum += arrayNumber[i];


//Calculate Average
average = arraySum / arrayNumber.length;

return average;











share|improve this question
























  • Please edit your question to include the error you're getting and to indicate which line of your code is causing it.

    – azurefrog
    Mar 6 at 23:34











  • I have like 5 versions of the code now. But it always tells me the err is in the //Initialization and Output line, either it's 'void' type not allowed here or incompatible types: void cannot be converted to String

    – Hip
    Mar 6 at 23:38











  • The return type of JOptionPane.showMessageDialog is void: it does not return anything. You cannot use it as an argument to Double.toString(). Why do you think you need to do that?

    – Kevin Anderson
    Mar 7 at 0:30











  • That was recommend by someone on a forum post I have read so I tried this out. I also tried out to convert it like this 'variable = Double.toString(variableOne);' So how can I convert it?

    – Hip
    Mar 7 at 0:41












  • There's no point in assigning something to output immediately before exiting the program. If the last thing you want to do is pop up a dialog to display your result, then just write JOptionPane.showMessageDialog(null, "The average is: ",...);". JOptionPane.showMessageDialog' returns nothing; there's nothing to "convert".

    – Kevin Anderson
    Mar 7 at 0:54














-1












-1








-1








I just can't get my code right how I want to. (Java Beginner)



So the way it should work is this:
When running the program I want to get a window and fill in a number(X) how large(Z) the array should be. Next I get X windows where I have to type in the numbers I want to put in the array. Finally the program should calculate the sum of all numbers and divide by the size(Z) of the array, to get the average of all numbers in the array.



I'm almost finished but all I get are errors regarding Double to String conversion. I tried everything I know out but couldn't make it work. Please excuse there may be spelling mistakes, I had to translate it to english.



Error:
Below //Initialization and Output line, either it's 'void' type not allowed here or incompatible types: void cannot be converted to String



import javax.swing.*;
import java.util.*;

public class Aufgabe42

public static void main(String[] args)

//Declare variable
String input, output, requestNumber;
double[] arrayNumber;
int size;

//Input
input = JOptionPane.showInputDialog(null, "Type in the size of the array.");

//Variable Initialization
size = Integer.parseInt(input);
arrayNumbers = new double[size];

//Request numbers
for(int i = 0; i < size; i++)

requestNumber = JOptionPane.showInputDialog(null, "Tell me a number:");
arrayNumber[i] = Double.parseDouble(requestNumber);


String returnAverage;

//Initialization and Output
output = Double.toString(JOptionPane.showMessageDialog(null, "The Average is: " + getAverage(arrayNumber)));
System.exit(0);


public static double getAverage(double[] arrayNumber)

//Declare variable

double arraySum,average;

//Initialization
arraySum = 0;



returnAverage = Double.toString(average);

//Sum array values
for(int i = 0; i < arrayZNumber.length; i++)

arraySum += arrayNumber[i];


//Calculate Average
average = arraySum / arrayNumber.length;

return average;











share|improve this question
















I just can't get my code right how I want to. (Java Beginner)



So the way it should work is this:
When running the program I want to get a window and fill in a number(X) how large(Z) the array should be. Next I get X windows where I have to type in the numbers I want to put in the array. Finally the program should calculate the sum of all numbers and divide by the size(Z) of the array, to get the average of all numbers in the array.



I'm almost finished but all I get are errors regarding Double to String conversion. I tried everything I know out but couldn't make it work. Please excuse there may be spelling mistakes, I had to translate it to english.



Error:
Below //Initialization and Output line, either it's 'void' type not allowed here or incompatible types: void cannot be converted to String



import javax.swing.*;
import java.util.*;

public class Aufgabe42

public static void main(String[] args)

//Declare variable
String input, output, requestNumber;
double[] arrayNumber;
int size;

//Input
input = JOptionPane.showInputDialog(null, "Type in the size of the array.");

//Variable Initialization
size = Integer.parseInt(input);
arrayNumbers = new double[size];

//Request numbers
for(int i = 0; i < size; i++)

requestNumber = JOptionPane.showInputDialog(null, "Tell me a number:");
arrayNumber[i] = Double.parseDouble(requestNumber);


String returnAverage;

//Initialization and Output
output = Double.toString(JOptionPane.showMessageDialog(null, "The Average is: " + getAverage(arrayNumber)));
System.exit(0);


public static double getAverage(double[] arrayNumber)

//Declare variable

double arraySum,average;

//Initialization
arraySum = 0;



returnAverage = Double.toString(average);

//Sum array values
for(int i = 0; i < arrayZNumber.length; i++)

arraySum += arrayNumber[i];


//Calculate Average
average = arraySum / arrayNumber.length;

return average;








java average






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 6 at 23:40







Hip

















asked Mar 6 at 23:32









HipHip

265




265












  • Please edit your question to include the error you're getting and to indicate which line of your code is causing it.

    – azurefrog
    Mar 6 at 23:34











  • I have like 5 versions of the code now. But it always tells me the err is in the //Initialization and Output line, either it's 'void' type not allowed here or incompatible types: void cannot be converted to String

    – Hip
    Mar 6 at 23:38











  • The return type of JOptionPane.showMessageDialog is void: it does not return anything. You cannot use it as an argument to Double.toString(). Why do you think you need to do that?

    – Kevin Anderson
    Mar 7 at 0:30











  • That was recommend by someone on a forum post I have read so I tried this out. I also tried out to convert it like this 'variable = Double.toString(variableOne);' So how can I convert it?

    – Hip
    Mar 7 at 0:41












  • There's no point in assigning something to output immediately before exiting the program. If the last thing you want to do is pop up a dialog to display your result, then just write JOptionPane.showMessageDialog(null, "The average is: ",...);". JOptionPane.showMessageDialog' returns nothing; there's nothing to "convert".

    – Kevin Anderson
    Mar 7 at 0:54


















  • Please edit your question to include the error you're getting and to indicate which line of your code is causing it.

    – azurefrog
    Mar 6 at 23:34











  • I have like 5 versions of the code now. But it always tells me the err is in the //Initialization and Output line, either it's 'void' type not allowed here or incompatible types: void cannot be converted to String

    – Hip
    Mar 6 at 23:38











  • The return type of JOptionPane.showMessageDialog is void: it does not return anything. You cannot use it as an argument to Double.toString(). Why do you think you need to do that?

    – Kevin Anderson
    Mar 7 at 0:30











  • That was recommend by someone on a forum post I have read so I tried this out. I also tried out to convert it like this 'variable = Double.toString(variableOne);' So how can I convert it?

    – Hip
    Mar 7 at 0:41












  • There's no point in assigning something to output immediately before exiting the program. If the last thing you want to do is pop up a dialog to display your result, then just write JOptionPane.showMessageDialog(null, "The average is: ",...);". JOptionPane.showMessageDialog' returns nothing; there's nothing to "convert".

    – Kevin Anderson
    Mar 7 at 0:54

















Please edit your question to include the error you're getting and to indicate which line of your code is causing it.

– azurefrog
Mar 6 at 23:34





Please edit your question to include the error you're getting and to indicate which line of your code is causing it.

– azurefrog
Mar 6 at 23:34













I have like 5 versions of the code now. But it always tells me the err is in the //Initialization and Output line, either it's 'void' type not allowed here or incompatible types: void cannot be converted to String

– Hip
Mar 6 at 23:38





I have like 5 versions of the code now. But it always tells me the err is in the //Initialization and Output line, either it's 'void' type not allowed here or incompatible types: void cannot be converted to String

– Hip
Mar 6 at 23:38













The return type of JOptionPane.showMessageDialog is void: it does not return anything. You cannot use it as an argument to Double.toString(). Why do you think you need to do that?

– Kevin Anderson
Mar 7 at 0:30





The return type of JOptionPane.showMessageDialog is void: it does not return anything. You cannot use it as an argument to Double.toString(). Why do you think you need to do that?

– Kevin Anderson
Mar 7 at 0:30













That was recommend by someone on a forum post I have read so I tried this out. I also tried out to convert it like this 'variable = Double.toString(variableOne);' So how can I convert it?

– Hip
Mar 7 at 0:41






That was recommend by someone on a forum post I have read so I tried this out. I also tried out to convert it like this 'variable = Double.toString(variableOne);' So how can I convert it?

– Hip
Mar 7 at 0:41














There's no point in assigning something to output immediately before exiting the program. If the last thing you want to do is pop up a dialog to display your result, then just write JOptionPane.showMessageDialog(null, "The average is: ",...);". JOptionPane.showMessageDialog' returns nothing; there's nothing to "convert".

– Kevin Anderson
Mar 7 at 0:54






There's no point in assigning something to output immediately before exiting the program. If the last thing you want to do is pop up a dialog to display your result, then just write JOptionPane.showMessageDialog(null, "The average is: ",...);". JOptionPane.showMessageDialog' returns nothing; there's nothing to "convert".

– Kevin Anderson
Mar 7 at 0:54













1 Answer
1






active

oldest

votes


















1














JOptionPane.showMessageDialog()


Has a void return type. You either want



JOptionPane.showInputDialog()


Or to remove



Double output = Double.valueOf(JOptionPane.showMessageDialog...)


near the end



https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033837%2fcalculating-average-converting-double-to-string-error%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    JOptionPane.showMessageDialog()


    Has a void return type. You either want



    JOptionPane.showInputDialog()


    Or to remove



    Double output = Double.valueOf(JOptionPane.showMessageDialog...)


    near the end



    https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html






    share|improve this answer



























      1














      JOptionPane.showMessageDialog()


      Has a void return type. You either want



      JOptionPane.showInputDialog()


      Or to remove



      Double output = Double.valueOf(JOptionPane.showMessageDialog...)


      near the end



      https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html






      share|improve this answer

























        1












        1








        1







        JOptionPane.showMessageDialog()


        Has a void return type. You either want



        JOptionPane.showInputDialog()


        Or to remove



        Double output = Double.valueOf(JOptionPane.showMessageDialog...)


        near the end



        https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html






        share|improve this answer













        JOptionPane.showMessageDialog()


        Has a void return type. You either want



        JOptionPane.showInputDialog()


        Or to remove



        Double output = Double.valueOf(JOptionPane.showMessageDialog...)


        near the end



        https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 6 at 23:38









        Asa WittAsa Witt

        211




        211





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033837%2fcalculating-average-converting-double-to-string-error%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко