Search code examples
javaswingjoptionpane

Can't get JOptionPane and System.out.println to work in tuition calculator


import java.io.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Tuition
{
    public static void main(String[] args)
    {
        //Declareing the variables
        int credits;
        double costPerCredit;
        double tuition;

        //calling methods
        credits = getCredits();
        costPerCredit = getCostPerCredit(credits);
        tuition = calcTuition(credits, costPerCredit);
        displayCredits(costPerCredit);
        displayTotal(tuition);


        //Dialog box that is popping out to ask the questions and get imput based on questions
        JOptionPane.showMessageDialog(null, "Java Tuition Calculator Program");
    }

    public static int getCredits()
    {
        //declare hours variable
        String input;
        int credits = 0;
        input = JOptionPane.showInputDialog(null,"Number of Credits?");

        return credits;
    }

    public static double getCostPerCredit(int credits)
    {
        String input1;
        double costPerCredit;
        costPerCredit = credits * 107.78;
        input1 = JOptionPane.showInputDialog(null,"Cost Per Credit?");

        return costPerCredit;
    }

    public static double calcTuition(int credits, double costPerCredit)
    {
        double tuition;

        tuition = credits * costPerCredit;
        return tuition;
    }


    public static void displayCredits(double costPerCredit)
    {
        double credits;
        credits = costPerCredit;

        DecimalFormat twoDigits = new DecimalFormat("$#000.00");
        System.out.println("Cost for Credits are " + credits);
    }

    public static void displayTotal(double tuition) 
    {
        double total;
        total = tuition;

        DecimalFormat twoDigits = new DecimalFormat("$#000.00");
        System.out.println("Your Tuition is " + total);
    }
}

When I execute the code it should ask me to type the number of credits I am currently going to take and cost per credit. After I input those the total result should come up in a new window with the phrase "Cost for credits are *(the answer should come out from the equation "tuition = credits * costPerCredit;")*. That number result should be the number I put in the first dialog box multiping with the pre-set $107.78, and I want those answers to be in the new window that pops out in the end. But all i get is

Cost for Credits are 0.0
Your Tuition is 0.0

Why can't I get System.out.println to print out the answer from public static double getCostPerCredit(int credits) and public static int getCredits()? The dialog box pops up for both of them and asks the questions in the field but I want those answers to be printed out and not just result in the answer being "0.0".

Note: I took out String input1; and input1 = JOptionPane.showInputDialog(null,"Cost Per Credit?");. The code seems to work the same way w/o it but the dialog box is removed.


Solution

  • Can't get JOptionPane and System.out.println to work in tuition calculator

    First: Cost for Credits are 0.0 because of int credits = 0; and you return credits 0 as type int
    
    Second: Any thing multiply by 0.0 is 0.0 as type double, So Your Tuition is 0.0
    
    public static int getCredits()
        {
            //declare hours variable
            String input;
            int credits = 0;
            input = JOptionPane.showInputDialog(null,"Number of Credits?");
    
            return Integer.parseInt(input);
        }