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 11 of 11

Thread: Can't figure out why my program isn't outputting.

  1. #1
    Junior Member
    Join Date
    Jan 2021
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Can't figure out why my program isn't outputting.

    import javax.swing.JOptionPane;
    public class CarlysEventPriceWithMethods
    {   
        public static void main(String args [])
        {
            returnGuests();
            displayMotto();
            displayMath();
        }  
        public static int returnGuests()
        {
            int guestsString;
            guestsString = JOptionPane.showInputDialog(null, 
                "Enter the number of guests: ",
                JOptionPane.INFORMATION_MESSAGE);
            return guestsString;
        }
        public static void displayMotto()
        {
            JOptionPane.showMessageDialog(null, 
            "\n*************************************************" + 
            "\n*Carly's makes the food that makes it a party.*" + 
            "\n*************************************************");
        }
        public static void displayMath()
        {
            final int PER_PERSON = 35;
            double cost;
            int selection;
            boolean isYes;
     
            cost = Double.parseDouble(guestsString) * PER_PERSON;
     
            JOptionPane.showMessageDialog(null, "The number of guests: " + guestsString + 
            "\nThe price per guest is $" + PER_PERSON + "\nThe total cost: $" + cost);
     
            selection = JOptionPane.showConfirmDialog(null, "Will the event have 50 or more guests?");
            isYes = (selection == JOptionPane.YES_OPTION);
            JOptionPane.showMessageDialog(null, "You responded " + isYes + "\nThis is a large event!");
        }
     
    }
    Last edited by Awak3n_Dreams; January 31st, 2021 at 06:18 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Can't figure out why my program isn't outputting.

    What do you see when the program is compiled and executed? Are there any errors?
    What do you want to see?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Awak3n_Dreams (January 31st, 2021)

  4. #3
    Junior Member
    Join Date
    Jan 2021
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Can't figure out why my program isn't outputting.

    I'm trying to get my dialog box to open. I'm not getting any output. I have to use those 3 classes when I do it. I'm new to this, so I'm not sure what I'm doing wrong?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Can't figure out why my program isn't outputting.

    First you need to get a compile without any errors. If there are compiler errors, the program will not execute.
    How are you compiling the program?
    Do you get any error messages?
    Please copy the full text of all error messages and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Jan 2021
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Can't figure out why my program isn't outputting.

    I did. This is the error:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Type mismatch: cannot convert from String to int

    at CarlysEventPriceWithMethods.returnGuests(CarlysEve ntPriceWithMethods.java:13)
    at CarlysEventPriceWithMethods.main(CarlysEventPriceW ithMethods.java:6)
    The problem says:
    CarlysEventPriceWithMethods.java is a non-project file, only syntax errors are reported. Java (16) [1,1]
    Last edited by Awak3n_Dreams; January 31st, 2021 at 08:07 PM.

  7. #6
    Junior Member
    Join Date
    Jan 2021
    Location
    Canada
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Re: Can't figure out why my program isn't outputting.

    Hi,

    Well first showInputDialog returns a String, not an int. It returns the value you entered.

    Also, you need the guestsString value in more than one method so you need to move to the declaration to the class.

    public class CarlysEventPriceWithMethods {
    static String guestsString;
    ....


    public static String returnGuests() {
    guestsString = JOptionPane.showInputDialog(null, "Enter the number of guests: ", "", JOptionPane.INFORMATION_MESSAGE);
    ....

    Hope this helps.

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Can't figure out why my program isn't outputting.

    You need to copy the full text of the error messages. What you posted only gives the line number, and not the error message.
    Here are some sample errors
     
    TestCode20.java:634: error: cannot find symbol
           x = y;
           ^
      symbol:   variable x
      location: class TestCode20
    TestCode20.java:634: error: cannot find symbol
           x = y;
               ^
      symbol:   variable y
      location: class TestCode20
    2 errors


    --- Update ---

    @sproket Thanks for trying to help.
    However by giving the OP the fixes for the code without the OP learning how to read and understand the text of an error message won't help the OP be able to fix future errors.

    Take a look at this: http://www.javaprogrammingforums.com...n-feeding.html
    If you don't understand my answer, don't ignore it, ask a question.

  9. #8
    Junior Member
    Join Date
    Jan 2021
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Can't figure out why my program isn't outputting.

    Sorry about that. I didn't realize it. I just edited my error post.

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Can't figure out why my program isn't outputting.

    Type mismatch: cannot convert from String to int

    at CarlysEventPriceWithMethods.returnGuests(CarlysEve ntPriceWithMethods.java:13)
    The method called on line 13 returns a String value which can not be assigned to an int variable.
    Change the guestsString variable to String instead of int.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Awak3n_Dreams (January 31st, 2021)

  12. #10
    Junior Member
    Join Date
    Jan 2021
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Can't figure out why my program isn't outputting.

    I understand that now, but I forgot to change my code. I decided to restart and switched over to scanner instead because I need this class to use public static int, but I'm still not getting any output? Thoughts, please?
    import java.util.Scanner;
    public class CarlysEventPriceWithMethods
    {   
        public static void main(String[] args)
        {   
            getNumberOfGuests();
            displayMotto();
            displayMath();
        }
        public static  int getNumberOfGuests()
        {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter the number of guests: ");
            int guests = scanner.nextInt();
            return guests;
        }
    }
    This is the error message:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    guests cannot be resolved to a variable

  13. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Can't figure out why my program isn't outputting.

    What source line is the error on? The error message is missing that.

    Did you post all of the source code? Where is the code for the other methods?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. outputting
    By my21 in forum Collections and Generics
    Replies: 1
    Last Post: April 23rd, 2013, 11:53 PM
  2. Simple sorting program, can't figure out whats wrong with my code
    By USC CSCE in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 29th, 2012, 08:38 AM
  3. Java program inputting month name and outputting month number and number of days
    By Brovahkiin501 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 19th, 2012, 09:30 AM
  4. Cannot figure out Stone Game Program
    By VettesRus in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 12th, 2012, 02:09 AM
  5. Help with my program please :) I can't figure this out.
    By scholaryoshi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2012, 07:31 AM