Java button going to wrong place in code.
so i'm making a project for my science class but the questions arent working. What im trying to do is when i click the answer for the first question i want it to go to the 2nd question but it goes to the last question instead. I am very new to java (i started last week) but i do have basic experience in other languages. I would appreciate any help i could get, thank you.
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Window
JFrame window;
Container con;
JPanel titlenamePanel, startButtonPanel, mainTextPanel, ChoiceButtonPanel;
JLabel titlenameLabel;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 28);
Font normalFont = new Font("Times New Roman", Font.PLAIN, 18);
JButton startButton, choice1, choice2, choice3, choice4, choice5, choice6;
JTextArea mainTextArea;
String Progress, yourChoice, Wind, Rain, Location, Cloud;
int Temp, Hum;
TitleScreenHandler tsHandler = new TitleScreenHandler();
ChoiceHandler chandler = new ChoiceHandler();
public static void main(String args)
new Window();
public Window()
window = new JFrame();
//width x height
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.black);
window.setLayout(null);
window.setVisible(true);
con = window.getContentPane();
titlenamePanel = new JPanel();
titlenamePanel.setBounds(100, 100, 600, 150);
titlenamePanel.setBackground(Color.black);
titlenameLabel = new JLabel("Weather Genie");
titlenameLabel.setForeground(Color.WHITE);
titlenameLabel.setFont(titleFont);
startButtonPanel = new JPanel();
startButtonPanel.setBounds(300, 400, 200, 100);
startButtonPanel.setBackground(Color.black);
startButton = new JButton("Start");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
startButton.addActionListener(tsHandler);
titlenamePanel.add(titlenameLabel);
startButtonPanel.add(startButton);
con.add(titlenamePanel);
con.add(startButtonPanel);
public void createWindowScreen()
titlenamePanel.setVisible(false);
startButtonPanel.setVisible(false);
// Text Panel
mainTextPanel = new JPanel();
mainTextPanel.setBounds(100, 100, 600, 250);
mainTextPanel.setBackground(Color.black);
con.add(mainTextPanel);
// Add Texts
mainTextArea = new JTextArea("Please answer the following questions.");
mainTextArea.setBounds(100, 100, 600, 250);
mainTextArea.setBackground(Color.black);
mainTextArea.setForeground(Color.white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextPanel.add(mainTextArea);
// Button Panel
ChoiceButtonPanel = new JPanel();
ChoiceButtonPanel.setBounds(250, 350, 300, 150);
ChoiceButtonPanel.setBackground(Color.black);
ChoiceButtonPanel.setLayout(new GridLayout(3,2));
con.add(ChoiceButtonPanel);
// Buttons
choice1 = new JButton("Choice 1");
choice1.setBackground(Color.DARK_GRAY);
choice1.setForeground(Color.white);
choice1.setFont(normalFont);
choice1.addActionListener(chandler);
ChoiceButtonPanel.add(choice1);
choice1.setActionCommand("c1");
choice2 = new JButton("Choice 2");
choice2.setBackground(Color.DARK_GRAY);
choice2.setForeground(Color.white);
choice2.setFont(normalFont);
choice2.addActionListener(chandler);
ChoiceButtonPanel.add(choice2);
choice2.setActionCommand("c2");
choice3 = new JButton("Choice 3");
choice3.setBackground(Color.DARK_GRAY);
choice3.setForeground(Color.white);
choice3.setFont(normalFont);
choice3.addActionListener(chandler);
ChoiceButtonPanel.add(choice3);
choice3.setActionCommand("c3");
choice4 = new JButton("Choice 4");
choice4.setBackground(Color.DARK_GRAY);
choice4.setForeground(Color.white);
choice4.setFont(normalFont);
choice4.addActionListener(chandler);
ChoiceButtonPanel.add(choice4);
choice4.setActionCommand("c4");
choice5 = new JButton("Choice 5");
choice5.setBackground(Color.DARK_GRAY);
choice5.setForeground(Color.white);
choice5.setFont(normalFont);
choice5.addActionListener(chandler);
ChoiceButtonPanel.add(choice5);
choice5.setActionCommand("c5");
choice6 = new JButton("Choice 6");
choice6.setBackground(Color.DARK_GRAY);
choice6.setForeground(Color.white);
choice6.setFont(normalFont);
choice6.addActionListener(chandler);
ChoiceButtonPanel.add(choice6);
choice6.setActionCommand("c6");
varSetup();
public void varSetup()
Temp = 80;
Hum = 65;
Location = "South";
Rain = "No";
Wind = "No";
Cloud = "Clear";
CloudQuestion();
public void CloudQuestion()
Progress = "CloudQ";
mainTextArea.setText("What do the clouds look like?");
choice1.setText("High and Dark");
choice2.setText("High and Bright");
choice3.setText("Low and Dark");
choice4.setText("Low and Bright");
choice5.setText("Clear");
choice6.setText("Foggy");
public void TempQuestion()
Progress = "TempQ";
mainTextArea.setText("What tmperature is it outside?");
choice1.setText("90-110");
choice2.setText("70-89");
choice3.setText("50-69");
choice4.setText("30-49");
choice5.setText("10-29");
choice6.setText("0-10");
public void HumQuestion()
Progress = "HumQ";
mainTextArea.setText("How Humid is it outside? nEx. The Average Household humidity is 65");
choice1.setText("90-100");
choice2.setText("70-89");
choice3.setText("50-69");
choice4.setText("30-49");
choice5.setText("10-29");
choice6.setText("");
public void WindQuestion()
Progress = "WindQ";
mainTextArea.setText("Has it been windy outside today?");
choice1.setText("Yes");
choice2.setText("No");
choice3.setText("");
choice4.setText("");
choice5.setText("");
choice6.setText("");
public void RainQuestion()
Progress = "RainQ";
mainTextArea.setText("Has it Rained this week?");
choice1.setText("1-2 Times");
choice2.setText("3-4 Times");
choice3.setText("5 or more Times");
choice4.setText("None");
choice5.setText("");
choice6.setText("");
public void LocQuestion()
Progress = "LocQ";
mainTextArea.setText("What part of the US are you in? nExample, florida would be East nSomebody in Nebraska or Kansas would say Central. ");
choice1.setText("North");
choice2.setText("South");
choice3.setText("East");
choice4.setText("West");
choice5.setText("Central");
choice6.setText("");
public class ChoiceHandler implements ActionListener
public void actionPerformed(ActionEvent event)
String yourChoice = event.getActionCommand();
switch (Progress)
case "CloudQ":
switch(yourChoice)
case"c1":
Cloud = "HD";
TempQuestion();
break;
case"c2":
Cloud = "HB";
TempQuestion();
break;
case"c3":
Cloud = "LD";
TempQuestion();
break;
case"c4":
Cloud = "LB";
TempQuestion();
break;
case"c5":
Cloud = "Clear";
TempQuestion();
break;
case"c6":
Cloud = "Foggy";
TempQuestion();
break;
case "TempQ":
switch(yourChoice)
case"c1":
Temp = 90110;
HumQuestion();
break;
case"c2":
Temp = 7089;
HumQuestion();
break;
case"c3":
Temp = 5069;
HumQuestion();
break;
case"c4":
Temp = 3049;
HumQuestion();
break;
case"c5":
Temp = 1029;
HumQuestion();
break;
case"c6":
Temp = 010;
HumQuestion();
break;
case "HumQ":
switch(yourChoice)
case"c1":
Hum = 90100; WindQuestion();
break;
case"c2":
Hum = 7089; WindQuestion();
break;
case"c3":
Hum = 5069; WindQuestion();
break;
case"c4":
Hum = 3049; WindQuestion();
break;
case"c5":
Hum = 1029;
WindQuestion();
break;
case"c6":
HumQuestion();
break;
case "WinQ":
switch(yourChoice)
case"c1":
Wind = "Yes";
RainQuestion();
break;
case"c2":
Wind = "No";
RainQuestion();
break;
case"c3":
WindQuestion();
break;
case"c4":
WindQuestion();
break;
case"c5":
WindQuestion();
break;
case"c6":
WindQuestion();
break;
case "RainQ":
switch(yourChoice)
case"c1":
Rain = "12";
LocQuestion();
break;
case"c2":
Rain = "34";
LocQuestion();
break;
case"c3":
Rain = "5+";
LocQuestion();
break;
case"c4":
Rain = "None";
LocQuestion();
break;
case"c5":
RainQuestion();
break;
case"c6":
RainQuestion();
break;
case "LocQ":
switch(yourChoice)
case"c1":
Location = "North"; results();break;
case"c2":
Location = "South"; results();break;
case"c3":
Location = "East"; results();break;
case"c4":
Location = "West"; results();break;
case"c5":
Location = "Central"; results();break;
case"c6":
LocQuestion();
break;
break;
public void results()
public class TitleScreenHandler implements ActionListener
public void actionPerformed(ActionEvent event)
createWindowScreen();
java swing switch-statement
add a comment |
so i'm making a project for my science class but the questions arent working. What im trying to do is when i click the answer for the first question i want it to go to the 2nd question but it goes to the last question instead. I am very new to java (i started last week) but i do have basic experience in other languages. I would appreciate any help i could get, thank you.
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Window
JFrame window;
Container con;
JPanel titlenamePanel, startButtonPanel, mainTextPanel, ChoiceButtonPanel;
JLabel titlenameLabel;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 28);
Font normalFont = new Font("Times New Roman", Font.PLAIN, 18);
JButton startButton, choice1, choice2, choice3, choice4, choice5, choice6;
JTextArea mainTextArea;
String Progress, yourChoice, Wind, Rain, Location, Cloud;
int Temp, Hum;
TitleScreenHandler tsHandler = new TitleScreenHandler();
ChoiceHandler chandler = new ChoiceHandler();
public static void main(String args)
new Window();
public Window()
window = new JFrame();
//width x height
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.black);
window.setLayout(null);
window.setVisible(true);
con = window.getContentPane();
titlenamePanel = new JPanel();
titlenamePanel.setBounds(100, 100, 600, 150);
titlenamePanel.setBackground(Color.black);
titlenameLabel = new JLabel("Weather Genie");
titlenameLabel.setForeground(Color.WHITE);
titlenameLabel.setFont(titleFont);
startButtonPanel = new JPanel();
startButtonPanel.setBounds(300, 400, 200, 100);
startButtonPanel.setBackground(Color.black);
startButton = new JButton("Start");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
startButton.addActionListener(tsHandler);
titlenamePanel.add(titlenameLabel);
startButtonPanel.add(startButton);
con.add(titlenamePanel);
con.add(startButtonPanel);
public void createWindowScreen()
titlenamePanel.setVisible(false);
startButtonPanel.setVisible(false);
// Text Panel
mainTextPanel = new JPanel();
mainTextPanel.setBounds(100, 100, 600, 250);
mainTextPanel.setBackground(Color.black);
con.add(mainTextPanel);
// Add Texts
mainTextArea = new JTextArea("Please answer the following questions.");
mainTextArea.setBounds(100, 100, 600, 250);
mainTextArea.setBackground(Color.black);
mainTextArea.setForeground(Color.white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextPanel.add(mainTextArea);
// Button Panel
ChoiceButtonPanel = new JPanel();
ChoiceButtonPanel.setBounds(250, 350, 300, 150);
ChoiceButtonPanel.setBackground(Color.black);
ChoiceButtonPanel.setLayout(new GridLayout(3,2));
con.add(ChoiceButtonPanel);
// Buttons
choice1 = new JButton("Choice 1");
choice1.setBackground(Color.DARK_GRAY);
choice1.setForeground(Color.white);
choice1.setFont(normalFont);
choice1.addActionListener(chandler);
ChoiceButtonPanel.add(choice1);
choice1.setActionCommand("c1");
choice2 = new JButton("Choice 2");
choice2.setBackground(Color.DARK_GRAY);
choice2.setForeground(Color.white);
choice2.setFont(normalFont);
choice2.addActionListener(chandler);
ChoiceButtonPanel.add(choice2);
choice2.setActionCommand("c2");
choice3 = new JButton("Choice 3");
choice3.setBackground(Color.DARK_GRAY);
choice3.setForeground(Color.white);
choice3.setFont(normalFont);
choice3.addActionListener(chandler);
ChoiceButtonPanel.add(choice3);
choice3.setActionCommand("c3");
choice4 = new JButton("Choice 4");
choice4.setBackground(Color.DARK_GRAY);
choice4.setForeground(Color.white);
choice4.setFont(normalFont);
choice4.addActionListener(chandler);
ChoiceButtonPanel.add(choice4);
choice4.setActionCommand("c4");
choice5 = new JButton("Choice 5");
choice5.setBackground(Color.DARK_GRAY);
choice5.setForeground(Color.white);
choice5.setFont(normalFont);
choice5.addActionListener(chandler);
ChoiceButtonPanel.add(choice5);
choice5.setActionCommand("c5");
choice6 = new JButton("Choice 6");
choice6.setBackground(Color.DARK_GRAY);
choice6.setForeground(Color.white);
choice6.setFont(normalFont);
choice6.addActionListener(chandler);
ChoiceButtonPanel.add(choice6);
choice6.setActionCommand("c6");
varSetup();
public void varSetup()
Temp = 80;
Hum = 65;
Location = "South";
Rain = "No";
Wind = "No";
Cloud = "Clear";
CloudQuestion();
public void CloudQuestion()
Progress = "CloudQ";
mainTextArea.setText("What do the clouds look like?");
choice1.setText("High and Dark");
choice2.setText("High and Bright");
choice3.setText("Low and Dark");
choice4.setText("Low and Bright");
choice5.setText("Clear");
choice6.setText("Foggy");
public void TempQuestion()
Progress = "TempQ";
mainTextArea.setText("What tmperature is it outside?");
choice1.setText("90-110");
choice2.setText("70-89");
choice3.setText("50-69");
choice4.setText("30-49");
choice5.setText("10-29");
choice6.setText("0-10");
public void HumQuestion()
Progress = "HumQ";
mainTextArea.setText("How Humid is it outside? nEx. The Average Household humidity is 65");
choice1.setText("90-100");
choice2.setText("70-89");
choice3.setText("50-69");
choice4.setText("30-49");
choice5.setText("10-29");
choice6.setText("");
public void WindQuestion()
Progress = "WindQ";
mainTextArea.setText("Has it been windy outside today?");
choice1.setText("Yes");
choice2.setText("No");
choice3.setText("");
choice4.setText("");
choice5.setText("");
choice6.setText("");
public void RainQuestion()
Progress = "RainQ";
mainTextArea.setText("Has it Rained this week?");
choice1.setText("1-2 Times");
choice2.setText("3-4 Times");
choice3.setText("5 or more Times");
choice4.setText("None");
choice5.setText("");
choice6.setText("");
public void LocQuestion()
Progress = "LocQ";
mainTextArea.setText("What part of the US are you in? nExample, florida would be East nSomebody in Nebraska or Kansas would say Central. ");
choice1.setText("North");
choice2.setText("South");
choice3.setText("East");
choice4.setText("West");
choice5.setText("Central");
choice6.setText("");
public class ChoiceHandler implements ActionListener
public void actionPerformed(ActionEvent event)
String yourChoice = event.getActionCommand();
switch (Progress)
case "CloudQ":
switch(yourChoice)
case"c1":
Cloud = "HD";
TempQuestion();
break;
case"c2":
Cloud = "HB";
TempQuestion();
break;
case"c3":
Cloud = "LD";
TempQuestion();
break;
case"c4":
Cloud = "LB";
TempQuestion();
break;
case"c5":
Cloud = "Clear";
TempQuestion();
break;
case"c6":
Cloud = "Foggy";
TempQuestion();
break;
case "TempQ":
switch(yourChoice)
case"c1":
Temp = 90110;
HumQuestion();
break;
case"c2":
Temp = 7089;
HumQuestion();
break;
case"c3":
Temp = 5069;
HumQuestion();
break;
case"c4":
Temp = 3049;
HumQuestion();
break;
case"c5":
Temp = 1029;
HumQuestion();
break;
case"c6":
Temp = 010;
HumQuestion();
break;
case "HumQ":
switch(yourChoice)
case"c1":
Hum = 90100; WindQuestion();
break;
case"c2":
Hum = 7089; WindQuestion();
break;
case"c3":
Hum = 5069; WindQuestion();
break;
case"c4":
Hum = 3049; WindQuestion();
break;
case"c5":
Hum = 1029;
WindQuestion();
break;
case"c6":
HumQuestion();
break;
case "WinQ":
switch(yourChoice)
case"c1":
Wind = "Yes";
RainQuestion();
break;
case"c2":
Wind = "No";
RainQuestion();
break;
case"c3":
WindQuestion();
break;
case"c4":
WindQuestion();
break;
case"c5":
WindQuestion();
break;
case"c6":
WindQuestion();
break;
case "RainQ":
switch(yourChoice)
case"c1":
Rain = "12";
LocQuestion();
break;
case"c2":
Rain = "34";
LocQuestion();
break;
case"c3":
Rain = "5+";
LocQuestion();
break;
case"c4":
Rain = "None";
LocQuestion();
break;
case"c5":
RainQuestion();
break;
case"c6":
RainQuestion();
break;
case "LocQ":
switch(yourChoice)
case"c1":
Location = "North"; results();break;
case"c2":
Location = "South"; results();break;
case"c3":
Location = "East"; results();break;
case"c4":
Location = "West"; results();break;
case"c5":
Location = "Central"; results();break;
case"c6":
LocQuestion();
break;
break;
public void results()
public class TitleScreenHandler implements ActionListener
public void actionPerformed(ActionEvent event)
createWindowScreen();
java swing switch-statement
That's a lot of code you want us to debug for you. Consider this a good learning opportunity for you to familiarize yourself with the use of a debugger. With a debugger you can step through the code line by line as it executes and directly observe the changing runtime values of your variables, runtime behavior of your logic, etc. When you do this, where specifically does the behavior change from what you expected it to do? Which specific operation or line of code produced an unexpected result. What were the values? What result was expected? Why?
– David
Nov 10 '18 at 16:23
Title is misleading, try editing it to a specfic one.
– Hrudayanath
Nov 10 '18 at 16:23
1
1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Please learn common Java nomenclature (naming conventions - e.g.EachWordUpperCaseClass
,firstWordLowerCaseMethod()
,firstWordLowerCaseAttribute
unless it is anUPPER_CASE_CONSTANT
) and use it consistently. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or ..
– Andrew Thompson
Nov 10 '18 at 16:26
.. combinations of them along with layout padding and borders for white space.
– Andrew Thompson
Nov 10 '18 at 16:28
add a comment |
so i'm making a project for my science class but the questions arent working. What im trying to do is when i click the answer for the first question i want it to go to the 2nd question but it goes to the last question instead. I am very new to java (i started last week) but i do have basic experience in other languages. I would appreciate any help i could get, thank you.
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Window
JFrame window;
Container con;
JPanel titlenamePanel, startButtonPanel, mainTextPanel, ChoiceButtonPanel;
JLabel titlenameLabel;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 28);
Font normalFont = new Font("Times New Roman", Font.PLAIN, 18);
JButton startButton, choice1, choice2, choice3, choice4, choice5, choice6;
JTextArea mainTextArea;
String Progress, yourChoice, Wind, Rain, Location, Cloud;
int Temp, Hum;
TitleScreenHandler tsHandler = new TitleScreenHandler();
ChoiceHandler chandler = new ChoiceHandler();
public static void main(String args)
new Window();
public Window()
window = new JFrame();
//width x height
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.black);
window.setLayout(null);
window.setVisible(true);
con = window.getContentPane();
titlenamePanel = new JPanel();
titlenamePanel.setBounds(100, 100, 600, 150);
titlenamePanel.setBackground(Color.black);
titlenameLabel = new JLabel("Weather Genie");
titlenameLabel.setForeground(Color.WHITE);
titlenameLabel.setFont(titleFont);
startButtonPanel = new JPanel();
startButtonPanel.setBounds(300, 400, 200, 100);
startButtonPanel.setBackground(Color.black);
startButton = new JButton("Start");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
startButton.addActionListener(tsHandler);
titlenamePanel.add(titlenameLabel);
startButtonPanel.add(startButton);
con.add(titlenamePanel);
con.add(startButtonPanel);
public void createWindowScreen()
titlenamePanel.setVisible(false);
startButtonPanel.setVisible(false);
// Text Panel
mainTextPanel = new JPanel();
mainTextPanel.setBounds(100, 100, 600, 250);
mainTextPanel.setBackground(Color.black);
con.add(mainTextPanel);
// Add Texts
mainTextArea = new JTextArea("Please answer the following questions.");
mainTextArea.setBounds(100, 100, 600, 250);
mainTextArea.setBackground(Color.black);
mainTextArea.setForeground(Color.white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextPanel.add(mainTextArea);
// Button Panel
ChoiceButtonPanel = new JPanel();
ChoiceButtonPanel.setBounds(250, 350, 300, 150);
ChoiceButtonPanel.setBackground(Color.black);
ChoiceButtonPanel.setLayout(new GridLayout(3,2));
con.add(ChoiceButtonPanel);
// Buttons
choice1 = new JButton("Choice 1");
choice1.setBackground(Color.DARK_GRAY);
choice1.setForeground(Color.white);
choice1.setFont(normalFont);
choice1.addActionListener(chandler);
ChoiceButtonPanel.add(choice1);
choice1.setActionCommand("c1");
choice2 = new JButton("Choice 2");
choice2.setBackground(Color.DARK_GRAY);
choice2.setForeground(Color.white);
choice2.setFont(normalFont);
choice2.addActionListener(chandler);
ChoiceButtonPanel.add(choice2);
choice2.setActionCommand("c2");
choice3 = new JButton("Choice 3");
choice3.setBackground(Color.DARK_GRAY);
choice3.setForeground(Color.white);
choice3.setFont(normalFont);
choice3.addActionListener(chandler);
ChoiceButtonPanel.add(choice3);
choice3.setActionCommand("c3");
choice4 = new JButton("Choice 4");
choice4.setBackground(Color.DARK_GRAY);
choice4.setForeground(Color.white);
choice4.setFont(normalFont);
choice4.addActionListener(chandler);
ChoiceButtonPanel.add(choice4);
choice4.setActionCommand("c4");
choice5 = new JButton("Choice 5");
choice5.setBackground(Color.DARK_GRAY);
choice5.setForeground(Color.white);
choice5.setFont(normalFont);
choice5.addActionListener(chandler);
ChoiceButtonPanel.add(choice5);
choice5.setActionCommand("c5");
choice6 = new JButton("Choice 6");
choice6.setBackground(Color.DARK_GRAY);
choice6.setForeground(Color.white);
choice6.setFont(normalFont);
choice6.addActionListener(chandler);
ChoiceButtonPanel.add(choice6);
choice6.setActionCommand("c6");
varSetup();
public void varSetup()
Temp = 80;
Hum = 65;
Location = "South";
Rain = "No";
Wind = "No";
Cloud = "Clear";
CloudQuestion();
public void CloudQuestion()
Progress = "CloudQ";
mainTextArea.setText("What do the clouds look like?");
choice1.setText("High and Dark");
choice2.setText("High and Bright");
choice3.setText("Low and Dark");
choice4.setText("Low and Bright");
choice5.setText("Clear");
choice6.setText("Foggy");
public void TempQuestion()
Progress = "TempQ";
mainTextArea.setText("What tmperature is it outside?");
choice1.setText("90-110");
choice2.setText("70-89");
choice3.setText("50-69");
choice4.setText("30-49");
choice5.setText("10-29");
choice6.setText("0-10");
public void HumQuestion()
Progress = "HumQ";
mainTextArea.setText("How Humid is it outside? nEx. The Average Household humidity is 65");
choice1.setText("90-100");
choice2.setText("70-89");
choice3.setText("50-69");
choice4.setText("30-49");
choice5.setText("10-29");
choice6.setText("");
public void WindQuestion()
Progress = "WindQ";
mainTextArea.setText("Has it been windy outside today?");
choice1.setText("Yes");
choice2.setText("No");
choice3.setText("");
choice4.setText("");
choice5.setText("");
choice6.setText("");
public void RainQuestion()
Progress = "RainQ";
mainTextArea.setText("Has it Rained this week?");
choice1.setText("1-2 Times");
choice2.setText("3-4 Times");
choice3.setText("5 or more Times");
choice4.setText("None");
choice5.setText("");
choice6.setText("");
public void LocQuestion()
Progress = "LocQ";
mainTextArea.setText("What part of the US are you in? nExample, florida would be East nSomebody in Nebraska or Kansas would say Central. ");
choice1.setText("North");
choice2.setText("South");
choice3.setText("East");
choice4.setText("West");
choice5.setText("Central");
choice6.setText("");
public class ChoiceHandler implements ActionListener
public void actionPerformed(ActionEvent event)
String yourChoice = event.getActionCommand();
switch (Progress)
case "CloudQ":
switch(yourChoice)
case"c1":
Cloud = "HD";
TempQuestion();
break;
case"c2":
Cloud = "HB";
TempQuestion();
break;
case"c3":
Cloud = "LD";
TempQuestion();
break;
case"c4":
Cloud = "LB";
TempQuestion();
break;
case"c5":
Cloud = "Clear";
TempQuestion();
break;
case"c6":
Cloud = "Foggy";
TempQuestion();
break;
case "TempQ":
switch(yourChoice)
case"c1":
Temp = 90110;
HumQuestion();
break;
case"c2":
Temp = 7089;
HumQuestion();
break;
case"c3":
Temp = 5069;
HumQuestion();
break;
case"c4":
Temp = 3049;
HumQuestion();
break;
case"c5":
Temp = 1029;
HumQuestion();
break;
case"c6":
Temp = 010;
HumQuestion();
break;
case "HumQ":
switch(yourChoice)
case"c1":
Hum = 90100; WindQuestion();
break;
case"c2":
Hum = 7089; WindQuestion();
break;
case"c3":
Hum = 5069; WindQuestion();
break;
case"c4":
Hum = 3049; WindQuestion();
break;
case"c5":
Hum = 1029;
WindQuestion();
break;
case"c6":
HumQuestion();
break;
case "WinQ":
switch(yourChoice)
case"c1":
Wind = "Yes";
RainQuestion();
break;
case"c2":
Wind = "No";
RainQuestion();
break;
case"c3":
WindQuestion();
break;
case"c4":
WindQuestion();
break;
case"c5":
WindQuestion();
break;
case"c6":
WindQuestion();
break;
case "RainQ":
switch(yourChoice)
case"c1":
Rain = "12";
LocQuestion();
break;
case"c2":
Rain = "34";
LocQuestion();
break;
case"c3":
Rain = "5+";
LocQuestion();
break;
case"c4":
Rain = "None";
LocQuestion();
break;
case"c5":
RainQuestion();
break;
case"c6":
RainQuestion();
break;
case "LocQ":
switch(yourChoice)
case"c1":
Location = "North"; results();break;
case"c2":
Location = "South"; results();break;
case"c3":
Location = "East"; results();break;
case"c4":
Location = "West"; results();break;
case"c5":
Location = "Central"; results();break;
case"c6":
LocQuestion();
break;
break;
public void results()
public class TitleScreenHandler implements ActionListener
public void actionPerformed(ActionEvent event)
createWindowScreen();
java swing switch-statement
so i'm making a project for my science class but the questions arent working. What im trying to do is when i click the answer for the first question i want it to go to the 2nd question but it goes to the last question instead. I am very new to java (i started last week) but i do have basic experience in other languages. I would appreciate any help i could get, thank you.
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Window
JFrame window;
Container con;
JPanel titlenamePanel, startButtonPanel, mainTextPanel, ChoiceButtonPanel;
JLabel titlenameLabel;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 28);
Font normalFont = new Font("Times New Roman", Font.PLAIN, 18);
JButton startButton, choice1, choice2, choice3, choice4, choice5, choice6;
JTextArea mainTextArea;
String Progress, yourChoice, Wind, Rain, Location, Cloud;
int Temp, Hum;
TitleScreenHandler tsHandler = new TitleScreenHandler();
ChoiceHandler chandler = new ChoiceHandler();
public static void main(String args)
new Window();
public Window()
window = new JFrame();
//width x height
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.black);
window.setLayout(null);
window.setVisible(true);
con = window.getContentPane();
titlenamePanel = new JPanel();
titlenamePanel.setBounds(100, 100, 600, 150);
titlenamePanel.setBackground(Color.black);
titlenameLabel = new JLabel("Weather Genie");
titlenameLabel.setForeground(Color.WHITE);
titlenameLabel.setFont(titleFont);
startButtonPanel = new JPanel();
startButtonPanel.setBounds(300, 400, 200, 100);
startButtonPanel.setBackground(Color.black);
startButton = new JButton("Start");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
startButton.addActionListener(tsHandler);
titlenamePanel.add(titlenameLabel);
startButtonPanel.add(startButton);
con.add(titlenamePanel);
con.add(startButtonPanel);
public void createWindowScreen()
titlenamePanel.setVisible(false);
startButtonPanel.setVisible(false);
// Text Panel
mainTextPanel = new JPanel();
mainTextPanel.setBounds(100, 100, 600, 250);
mainTextPanel.setBackground(Color.black);
con.add(mainTextPanel);
// Add Texts
mainTextArea = new JTextArea("Please answer the following questions.");
mainTextArea.setBounds(100, 100, 600, 250);
mainTextArea.setBackground(Color.black);
mainTextArea.setForeground(Color.white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextPanel.add(mainTextArea);
// Button Panel
ChoiceButtonPanel = new JPanel();
ChoiceButtonPanel.setBounds(250, 350, 300, 150);
ChoiceButtonPanel.setBackground(Color.black);
ChoiceButtonPanel.setLayout(new GridLayout(3,2));
con.add(ChoiceButtonPanel);
// Buttons
choice1 = new JButton("Choice 1");
choice1.setBackground(Color.DARK_GRAY);
choice1.setForeground(Color.white);
choice1.setFont(normalFont);
choice1.addActionListener(chandler);
ChoiceButtonPanel.add(choice1);
choice1.setActionCommand("c1");
choice2 = new JButton("Choice 2");
choice2.setBackground(Color.DARK_GRAY);
choice2.setForeground(Color.white);
choice2.setFont(normalFont);
choice2.addActionListener(chandler);
ChoiceButtonPanel.add(choice2);
choice2.setActionCommand("c2");
choice3 = new JButton("Choice 3");
choice3.setBackground(Color.DARK_GRAY);
choice3.setForeground(Color.white);
choice3.setFont(normalFont);
choice3.addActionListener(chandler);
ChoiceButtonPanel.add(choice3);
choice3.setActionCommand("c3");
choice4 = new JButton("Choice 4");
choice4.setBackground(Color.DARK_GRAY);
choice4.setForeground(Color.white);
choice4.setFont(normalFont);
choice4.addActionListener(chandler);
ChoiceButtonPanel.add(choice4);
choice4.setActionCommand("c4");
choice5 = new JButton("Choice 5");
choice5.setBackground(Color.DARK_GRAY);
choice5.setForeground(Color.white);
choice5.setFont(normalFont);
choice5.addActionListener(chandler);
ChoiceButtonPanel.add(choice5);
choice5.setActionCommand("c5");
choice6 = new JButton("Choice 6");
choice6.setBackground(Color.DARK_GRAY);
choice6.setForeground(Color.white);
choice6.setFont(normalFont);
choice6.addActionListener(chandler);
ChoiceButtonPanel.add(choice6);
choice6.setActionCommand("c6");
varSetup();
public void varSetup()
Temp = 80;
Hum = 65;
Location = "South";
Rain = "No";
Wind = "No";
Cloud = "Clear";
CloudQuestion();
public void CloudQuestion()
Progress = "CloudQ";
mainTextArea.setText("What do the clouds look like?");
choice1.setText("High and Dark");
choice2.setText("High and Bright");
choice3.setText("Low and Dark");
choice4.setText("Low and Bright");
choice5.setText("Clear");
choice6.setText("Foggy");
public void TempQuestion()
Progress = "TempQ";
mainTextArea.setText("What tmperature is it outside?");
choice1.setText("90-110");
choice2.setText("70-89");
choice3.setText("50-69");
choice4.setText("30-49");
choice5.setText("10-29");
choice6.setText("0-10");
public void HumQuestion()
Progress = "HumQ";
mainTextArea.setText("How Humid is it outside? nEx. The Average Household humidity is 65");
choice1.setText("90-100");
choice2.setText("70-89");
choice3.setText("50-69");
choice4.setText("30-49");
choice5.setText("10-29");
choice6.setText("");
public void WindQuestion()
Progress = "WindQ";
mainTextArea.setText("Has it been windy outside today?");
choice1.setText("Yes");
choice2.setText("No");
choice3.setText("");
choice4.setText("");
choice5.setText("");
choice6.setText("");
public void RainQuestion()
Progress = "RainQ";
mainTextArea.setText("Has it Rained this week?");
choice1.setText("1-2 Times");
choice2.setText("3-4 Times");
choice3.setText("5 or more Times");
choice4.setText("None");
choice5.setText("");
choice6.setText("");
public void LocQuestion()
Progress = "LocQ";
mainTextArea.setText("What part of the US are you in? nExample, florida would be East nSomebody in Nebraska or Kansas would say Central. ");
choice1.setText("North");
choice2.setText("South");
choice3.setText("East");
choice4.setText("West");
choice5.setText("Central");
choice6.setText("");
public class ChoiceHandler implements ActionListener
public void actionPerformed(ActionEvent event)
String yourChoice = event.getActionCommand();
switch (Progress)
case "CloudQ":
switch(yourChoice)
case"c1":
Cloud = "HD";
TempQuestion();
break;
case"c2":
Cloud = "HB";
TempQuestion();
break;
case"c3":
Cloud = "LD";
TempQuestion();
break;
case"c4":
Cloud = "LB";
TempQuestion();
break;
case"c5":
Cloud = "Clear";
TempQuestion();
break;
case"c6":
Cloud = "Foggy";
TempQuestion();
break;
case "TempQ":
switch(yourChoice)
case"c1":
Temp = 90110;
HumQuestion();
break;
case"c2":
Temp = 7089;
HumQuestion();
break;
case"c3":
Temp = 5069;
HumQuestion();
break;
case"c4":
Temp = 3049;
HumQuestion();
break;
case"c5":
Temp = 1029;
HumQuestion();
break;
case"c6":
Temp = 010;
HumQuestion();
break;
case "HumQ":
switch(yourChoice)
case"c1":
Hum = 90100; WindQuestion();
break;
case"c2":
Hum = 7089; WindQuestion();
break;
case"c3":
Hum = 5069; WindQuestion();
break;
case"c4":
Hum = 3049; WindQuestion();
break;
case"c5":
Hum = 1029;
WindQuestion();
break;
case"c6":
HumQuestion();
break;
case "WinQ":
switch(yourChoice)
case"c1":
Wind = "Yes";
RainQuestion();
break;
case"c2":
Wind = "No";
RainQuestion();
break;
case"c3":
WindQuestion();
break;
case"c4":
WindQuestion();
break;
case"c5":
WindQuestion();
break;
case"c6":
WindQuestion();
break;
case "RainQ":
switch(yourChoice)
case"c1":
Rain = "12";
LocQuestion();
break;
case"c2":
Rain = "34";
LocQuestion();
break;
case"c3":
Rain = "5+";
LocQuestion();
break;
case"c4":
Rain = "None";
LocQuestion();
break;
case"c5":
RainQuestion();
break;
case"c6":
RainQuestion();
break;
case "LocQ":
switch(yourChoice)
case"c1":
Location = "North"; results();break;
case"c2":
Location = "South"; results();break;
case"c3":
Location = "East"; results();break;
case"c4":
Location = "West"; results();break;
case"c5":
Location = "Central"; results();break;
case"c6":
LocQuestion();
break;
break;
public void results()
public class TitleScreenHandler implements ActionListener
public void actionPerformed(ActionEvent event)
createWindowScreen();
java swing switch-statement
java swing switch-statement
edited Nov 10 '18 at 16:34
Java Cactaur
asked Nov 10 '18 at 16:17
Java CactaurJava Cactaur
34
34
That's a lot of code you want us to debug for you. Consider this a good learning opportunity for you to familiarize yourself with the use of a debugger. With a debugger you can step through the code line by line as it executes and directly observe the changing runtime values of your variables, runtime behavior of your logic, etc. When you do this, where specifically does the behavior change from what you expected it to do? Which specific operation or line of code produced an unexpected result. What were the values? What result was expected? Why?
– David
Nov 10 '18 at 16:23
Title is misleading, try editing it to a specfic one.
– Hrudayanath
Nov 10 '18 at 16:23
1
1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Please learn common Java nomenclature (naming conventions - e.g.EachWordUpperCaseClass
,firstWordLowerCaseMethod()
,firstWordLowerCaseAttribute
unless it is anUPPER_CASE_CONSTANT
) and use it consistently. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or ..
– Andrew Thompson
Nov 10 '18 at 16:26
.. combinations of them along with layout padding and borders for white space.
– Andrew Thompson
Nov 10 '18 at 16:28
add a comment |
That's a lot of code you want us to debug for you. Consider this a good learning opportunity for you to familiarize yourself with the use of a debugger. With a debugger you can step through the code line by line as it executes and directly observe the changing runtime values of your variables, runtime behavior of your logic, etc. When you do this, where specifically does the behavior change from what you expected it to do? Which specific operation or line of code produced an unexpected result. What were the values? What result was expected? Why?
– David
Nov 10 '18 at 16:23
Title is misleading, try editing it to a specfic one.
– Hrudayanath
Nov 10 '18 at 16:23
1
1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Please learn common Java nomenclature (naming conventions - e.g.EachWordUpperCaseClass
,firstWordLowerCaseMethod()
,firstWordLowerCaseAttribute
unless it is anUPPER_CASE_CONSTANT
) and use it consistently. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or ..
– Andrew Thompson
Nov 10 '18 at 16:26
.. combinations of them along with layout padding and borders for white space.
– Andrew Thompson
Nov 10 '18 at 16:28
That's a lot of code you want us to debug for you. Consider this a good learning opportunity for you to familiarize yourself with the use of a debugger. With a debugger you can step through the code line by line as it executes and directly observe the changing runtime values of your variables, runtime behavior of your logic, etc. When you do this, where specifically does the behavior change from what you expected it to do? Which specific operation or line of code produced an unexpected result. What were the values? What result was expected? Why?
– David
Nov 10 '18 at 16:23
That's a lot of code you want us to debug for you. Consider this a good learning opportunity for you to familiarize yourself with the use of a debugger. With a debugger you can step through the code line by line as it executes and directly observe the changing runtime values of your variables, runtime behavior of your logic, etc. When you do this, where specifically does the behavior change from what you expected it to do? Which specific operation or line of code produced an unexpected result. What were the values? What result was expected? Why?
– David
Nov 10 '18 at 16:23
Title is misleading, try editing it to a specfic one.
– Hrudayanath
Nov 10 '18 at 16:23
Title is misleading, try editing it to a specfic one.
– Hrudayanath
Nov 10 '18 at 16:23
1
1
1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Please learn common Java nomenclature (naming conventions - e.g.
EachWordUpperCaseClass
, firstWordLowerCaseMethod()
, firstWordLowerCaseAttribute
unless it is an UPPER_CASE_CONSTANT
) and use it consistently. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or ..– Andrew Thompson
Nov 10 '18 at 16:26
1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Please learn common Java nomenclature (naming conventions - e.g.
EachWordUpperCaseClass
, firstWordLowerCaseMethod()
, firstWordLowerCaseAttribute
unless it is an UPPER_CASE_CONSTANT
) and use it consistently. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or ..– Andrew Thompson
Nov 10 '18 at 16:26
.. combinations of them along with layout padding and borders for white space.
– Andrew Thompson
Nov 10 '18 at 16:28
.. combinations of them along with layout padding and borders for white space.
– Andrew Thompson
Nov 10 '18 at 16:28
add a comment |
1 Answer
1
active
oldest
votes
You have a switch statement inside another switch statement, in the inner switch statement you're correctly using break
, however you're never breaking after the inner switch statement, see the code below (only a part of your code) to see what you're missing. I've marked the break
statements with // Add this
.
Not using the break
there is causing it to execute every case
under the case
that it matched against. It will do so until the first break
is reached or the switch
has ended.
switch (Progress)
case "CloudQ":
switch (yourChoice)
case "c1":
Cloud = "HD";
TempQuestion();
break;
case "c2":
Cloud = "HB";
TempQuestion();
break;
case "c3":
Cloud = "LD";
TempQuestion();
break;
case "c4":
Cloud = "LB";
TempQuestion();
break;
case "c5":
Cloud = "Clear";
TempQuestion();
break;
case "c6":
Cloud = "Foggy";
TempQuestion();
break;
break; // Add this
case "TempQ":
switch (yourChoice)
case "c1":
Temp = 90110;
HumQuestion();
break;
case "c2":
Temp = 7089;
HumQuestion();
break;
case "c3":
Temp = 5069;
HumQuestion();
break;
case "c4":
Temp = 3049;
HumQuestion();
break;
case "c5":
Temp = 1029;
HumQuestion();
break;
case "c6":
Temp = 010;
HumQuestion();
break;
break; // Add this
1
Thank you so much! Mark you are a life saver. I dont know what i woulve done without you. I will 100% credit you in my science fair. You are a saint <3
– Java Cactaur
Nov 10 '18 at 16:48
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240891%2fjava-button-going-to-wrong-place-in-code%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
You have a switch statement inside another switch statement, in the inner switch statement you're correctly using break
, however you're never breaking after the inner switch statement, see the code below (only a part of your code) to see what you're missing. I've marked the break
statements with // Add this
.
Not using the break
there is causing it to execute every case
under the case
that it matched against. It will do so until the first break
is reached or the switch
has ended.
switch (Progress)
case "CloudQ":
switch (yourChoice)
case "c1":
Cloud = "HD";
TempQuestion();
break;
case "c2":
Cloud = "HB";
TempQuestion();
break;
case "c3":
Cloud = "LD";
TempQuestion();
break;
case "c4":
Cloud = "LB";
TempQuestion();
break;
case "c5":
Cloud = "Clear";
TempQuestion();
break;
case "c6":
Cloud = "Foggy";
TempQuestion();
break;
break; // Add this
case "TempQ":
switch (yourChoice)
case "c1":
Temp = 90110;
HumQuestion();
break;
case "c2":
Temp = 7089;
HumQuestion();
break;
case "c3":
Temp = 5069;
HumQuestion();
break;
case "c4":
Temp = 3049;
HumQuestion();
break;
case "c5":
Temp = 1029;
HumQuestion();
break;
case "c6":
Temp = 010;
HumQuestion();
break;
break; // Add this
1
Thank you so much! Mark you are a life saver. I dont know what i woulve done without you. I will 100% credit you in my science fair. You are a saint <3
– Java Cactaur
Nov 10 '18 at 16:48
add a comment |
You have a switch statement inside another switch statement, in the inner switch statement you're correctly using break
, however you're never breaking after the inner switch statement, see the code below (only a part of your code) to see what you're missing. I've marked the break
statements with // Add this
.
Not using the break
there is causing it to execute every case
under the case
that it matched against. It will do so until the first break
is reached or the switch
has ended.
switch (Progress)
case "CloudQ":
switch (yourChoice)
case "c1":
Cloud = "HD";
TempQuestion();
break;
case "c2":
Cloud = "HB";
TempQuestion();
break;
case "c3":
Cloud = "LD";
TempQuestion();
break;
case "c4":
Cloud = "LB";
TempQuestion();
break;
case "c5":
Cloud = "Clear";
TempQuestion();
break;
case "c6":
Cloud = "Foggy";
TempQuestion();
break;
break; // Add this
case "TempQ":
switch (yourChoice)
case "c1":
Temp = 90110;
HumQuestion();
break;
case "c2":
Temp = 7089;
HumQuestion();
break;
case "c3":
Temp = 5069;
HumQuestion();
break;
case "c4":
Temp = 3049;
HumQuestion();
break;
case "c5":
Temp = 1029;
HumQuestion();
break;
case "c6":
Temp = 010;
HumQuestion();
break;
break; // Add this
1
Thank you so much! Mark you are a life saver. I dont know what i woulve done without you. I will 100% credit you in my science fair. You are a saint <3
– Java Cactaur
Nov 10 '18 at 16:48
add a comment |
You have a switch statement inside another switch statement, in the inner switch statement you're correctly using break
, however you're never breaking after the inner switch statement, see the code below (only a part of your code) to see what you're missing. I've marked the break
statements with // Add this
.
Not using the break
there is causing it to execute every case
under the case
that it matched against. It will do so until the first break
is reached or the switch
has ended.
switch (Progress)
case "CloudQ":
switch (yourChoice)
case "c1":
Cloud = "HD";
TempQuestion();
break;
case "c2":
Cloud = "HB";
TempQuestion();
break;
case "c3":
Cloud = "LD";
TempQuestion();
break;
case "c4":
Cloud = "LB";
TempQuestion();
break;
case "c5":
Cloud = "Clear";
TempQuestion();
break;
case "c6":
Cloud = "Foggy";
TempQuestion();
break;
break; // Add this
case "TempQ":
switch (yourChoice)
case "c1":
Temp = 90110;
HumQuestion();
break;
case "c2":
Temp = 7089;
HumQuestion();
break;
case "c3":
Temp = 5069;
HumQuestion();
break;
case "c4":
Temp = 3049;
HumQuestion();
break;
case "c5":
Temp = 1029;
HumQuestion();
break;
case "c6":
Temp = 010;
HumQuestion();
break;
break; // Add this
You have a switch statement inside another switch statement, in the inner switch statement you're correctly using break
, however you're never breaking after the inner switch statement, see the code below (only a part of your code) to see what you're missing. I've marked the break
statements with // Add this
.
Not using the break
there is causing it to execute every case
under the case
that it matched against. It will do so until the first break
is reached or the switch
has ended.
switch (Progress)
case "CloudQ":
switch (yourChoice)
case "c1":
Cloud = "HD";
TempQuestion();
break;
case "c2":
Cloud = "HB";
TempQuestion();
break;
case "c3":
Cloud = "LD";
TempQuestion();
break;
case "c4":
Cloud = "LB";
TempQuestion();
break;
case "c5":
Cloud = "Clear";
TempQuestion();
break;
case "c6":
Cloud = "Foggy";
TempQuestion();
break;
break; // Add this
case "TempQ":
switch (yourChoice)
case "c1":
Temp = 90110;
HumQuestion();
break;
case "c2":
Temp = 7089;
HumQuestion();
break;
case "c3":
Temp = 5069;
HumQuestion();
break;
case "c4":
Temp = 3049;
HumQuestion();
break;
case "c5":
Temp = 1029;
HumQuestion();
break;
case "c6":
Temp = 010;
HumQuestion();
break;
break; // Add this
answered Nov 10 '18 at 16:24
MarkMark
3,42711026
3,42711026
1
Thank you so much! Mark you are a life saver. I dont know what i woulve done without you. I will 100% credit you in my science fair. You are a saint <3
– Java Cactaur
Nov 10 '18 at 16:48
add a comment |
1
Thank you so much! Mark you are a life saver. I dont know what i woulve done without you. I will 100% credit you in my science fair. You are a saint <3
– Java Cactaur
Nov 10 '18 at 16:48
1
1
Thank you so much! Mark you are a life saver. I dont know what i woulve done without you. I will 100% credit you in my science fair. You are a saint <3
– Java Cactaur
Nov 10 '18 at 16:48
Thank you so much! Mark you are a life saver. I dont know what i woulve done without you. I will 100% credit you in my science fair. You are a saint <3
– Java Cactaur
Nov 10 '18 at 16:48
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240891%2fjava-button-going-to-wrong-place-in-code%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
That's a lot of code you want us to debug for you. Consider this a good learning opportunity for you to familiarize yourself with the use of a debugger. With a debugger you can step through the code line by line as it executes and directly observe the changing runtime values of your variables, runtime behavior of your logic, etc. When you do this, where specifically does the behavior change from what you expected it to do? Which specific operation or line of code produced an unexpected result. What were the values? What result was expected? Why?
– David
Nov 10 '18 at 16:23
Title is misleading, try editing it to a specfic one.
– Hrudayanath
Nov 10 '18 at 16:23
1
1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Please learn common Java nomenclature (naming conventions - e.g.
EachWordUpperCaseClass
,firstWordLowerCaseMethod()
,firstWordLowerCaseAttribute
unless it is anUPPER_CASE_CONSTANT
) and use it consistently. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or ..– Andrew Thompson
Nov 10 '18 at 16:26
.. combinations of them along with layout padding and borders for white space.
– Andrew Thompson
Nov 10 '18 at 16:28