0

I'm writing a java program that is like a physics lesson:

import javax.swing.JOptionPane;
public class PhysicsClass {


    public static void main(String[] args) {
        int g = -1;
        while (g<0){
            String input = JOptionPane.showInputDialog("Welcome! What's your name? ");
            if(input.length() > 0){
                g++;
             System.out.println("Great! Lets begin.");
            }
            else{
                 System.out.println(" ok then.");
                System.exit(0);
            }
        }

         String[] firstChoice = {"Kinematics", "Dynamics", "Impulse/Momentum", "Energy/Power"}; 
            JOptionPane.showInputDialog(null,
                    "Which one of these Topic would you like to start with?", 
                    "Please pick a topic to start with",
                    JOptionPane.INFORMATION_MESSAGE, null,
                    firstChoice, firstChoice[0]);


            int i = 0;
            while (i<0){
            String Choice = "";
            if (Choice == firstChoice[0]){
             Kinematics.IntroToKinematics();
             i++;

            }
            else if (Choice == firstChoice[1]){
                Dynamics.DynamicsIntro();
             i++;
            }
            else if (Choice == firstChoice[2]){
                ImpulseAndMomentum.ImpulseAndMomentumIntro();
             i++;
            }
            else if (Choice == firstChoice[3]){
                EnergyAndPower.EnergyAndPowerIntro();
             i++;
            }
            else{
                System.out.println("Please pick one.");
            }
            }

I want whatever choice you pick in the first choice array to then go to the respected class. So if I choose kinematics, it will call on the kinematics class, for which the first few lines are:

import javax.swing.JOptionPane;

public class Kinematics {

    public static void IntroToKinematics() {
        JOptionPane.showInternalMessageDialog(null, "Kinematics is one of the most basic"
                + " ideas of Newtonian Physics. It encompasses: speed, distance, time"
                + " displacement, acceleration and many key base concepts that form the the "
                + " foundation for most physic subjects. It will poke its head in everything"
                + "we cover.", "intro", JOptionPane.INFORMATION_MESSAGE);
    }

}

It doesn't give me any errors but when I choose one of the strings from the array, it doesn't do anything. I think it might have something to do with the if else statements I used. Thanks for any and all help, I'm still relatively new to Java so any tips would be appreciated.

James Dunn
  • 8,064
  • 13
  • 53
  • 87
  • Also looks like `Choice` might be a protected word, given the syntax highlighting – StephenTG Aug 14 '13 at 14:48
  • 1
    Please study Java naming convention... – ppeterka Aug 14 '13 at 14:48
  • Seems like Choice is always blank, even if you were using the correct string compare. – Kevin DiTraglia Aug 14 '13 at 14:49
  • 1
    You declare Choice as an empty string and then compare it to the array... of course it doesn't do anything, because "emtpy string" isn't in your array and thus, none of the if's are true. – waka Aug 14 '13 at 14:51
  • @StephenTG: It's highlighted because it starts uppercase. That's used for class names, not variable names. – jlordo Aug 14 '13 at 14:52
  • @jlordo Ok, so not the sort of thing that would actually cause issues, just bad naming convention – StephenTG Aug 14 '13 at 14:53
  • @Reimeus I don't think this is a duplicate. String comparison turned out to be the issue (at least one of them), but it wasn't the question. – James Dunn Aug 14 '13 at 14:56
  • 1
    To elaborate on the comment by @ppeterka, in Java naming convention your variables should be written in camelCase (not PascalCase), meaning the first letter should be lower case -- unless it is static and final, in which case it should be ALL_CAPS. – James Dunn Aug 14 '13 at 14:59

4 Answers4

4

Use equals to compare strings like-

if(Choice.equals(firstChoice[0])){...}
Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
  • @ZacharySpencer I'll bet this is exactly why your code isn't working. Using `.equals()` will check if the Strings are spelled the same, while `==` checks if they are the same object in memory. – James Dunn Aug 14 '13 at 14:52
1

First off you want to store the result of the input box somewhere:

String choice = JOptionPane.showInputDialog(null,
                "Which one of these Topic would you like to start with?", 
                "Please pick a topic to start with",
                JOptionPane.INFORMATION_MESSAGE, null,
                firstChoice, firstChoice[0]);

Then use .equals to do your string compares instead of ==

if (choice.equals(firstChoice[0])){
     Kinematics.IntroToKinematics();
}

The loop shouldn't be necessary.

Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
0

You're not binding Choice to selected value, it always compares it to ""

Konstantin
  • 3,254
  • 15
  • 20
0

First at the showInputDialog you should do something like this

String Choice = ""; 
     Choice=  (String) JOptionPane.showInputDialog(null,
                "Which one of these Topic would you like to start with?", 
                "Please pick a topic to start with",
                JOptionPane.INFORMATION_MESSAGE, null,
                firstChoice, firstChoice[0]);

Declaring first the variable Choice, and then asigning the inputDialog value to choice, afther that your while should be like this

    int i = 0;
    while (i<=0){
    //do something
    }

because the first time it runs i<0 , since 0 is not lower than 0 (since you are declaring int i =0;) is not going to run. And you could keep using "", but something like this should be more efficient

if (Choice.equals(firstChoice[0])){
             Kinematics.IntroToKinematics();
             i++;

            }

Finally try to use lowercase first at your variables eg: tableSize

Abstract
  • 664
  • 1
  • 5
  • 15