Question: Java I posted this qustion before and need a little more help on understanding this. Java is very hard for me and I struggled in

Java

I posted this qustion before and need a little more help on understanding this. Java is very hard for me and I struggled in this class so help will be greatly apprecated.

Orginal Queston:

Java Question About Code stopping midway

Code: here and at the end of the code here is the errors

When the GUI pop up i can select the food no problem but when i chose the condments the program stops and errors pop up.

Here is the code i posted and around line 90 he answerd back and i placed it in the code in the bottom at the place it was entered. If at all possable i would like a little more understanding if possable? Thanks for all your help

package j.hot.dog;

import java.awt.Checkbox; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel;

public class JHotDog extends JFrame implements ActionListener { String[] food = {"Regular dog", "Corn dog", "Chili dog"}; JComboBox msg = new JComboBox(food); JLabel lbl = new JLabel(); int OrderPrice = 0; public static void main(String[] args) { JHotDog ft = new JHotDog(); ft.setVisible(true); } public JHotDog() { setLayout(new FlowLayout()); setSize(400,300); setTitle("Hot Dog Stand"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); msg.setSelectedIndex(0); msg.addActionListener(this); add(msg); add(lbl); Checkbox checkbox1 = new Checkbox("Mustard"); checkbox1.setBounds(120,110,50,50); Checkbox checkbox2 = new Checkbox("Ketchup"); checkbox2.setBounds(120,110,50,50); Checkbox checkbox3 = new Checkbox("Onions"); checkbox3.setBounds(120,110,50,50); Checkbox checkbox4 = new Checkbox("Relish"); checkbox4.setBounds(120,110,50,50); Checkbox checkbox5 = new Checkbox("Mayo"); checkbox5.setBounds(120,110,50,50); Checkbox checkbox6 = new Checkbox("Pico de Gallo"); checkbox6.setBounds(120,110,50,50); Checkbox checkbox7 = new Checkbox("fry sauce"); checkbox7.setBounds(120,110,50,50); add(checkbox1); add(checkbox2); add(checkbox3); add(checkbox4); add(checkbox5); add(checkbox6); add(checkbox7); checkbox1.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == 1) OrderPrice += 1; else OrderPrice -= 1; lbl.setText(lbl.getText().substring(0, 18) + (e.getStateChange() == 1 ? "Mustard Added and Order Price: " + OrderPrice : OrderPrice)); } }); checkbox2.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == 1) OrderPrice += 1; else OrderPrice -= 1;

// 1. before setText, you are trying to getText, that's why you getting error. Here, substring(0,18) is not required. Or If you want substring(0,18) then

// create JLabel parameterized Constructor object (JLabel lbl = new JLabel("Hello, Please select the order!!"); ) , any string morethan 18 characters.

lbl.setText(lbl.getText().substring(0, 18)+ (e.getStateChange()==1 ? "Ketchup Added and Order Price: " + OrderPrice : OrderPrice)); } }); checkbox3.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==1) OrderPrice += 1; else OrderPrice -= 1; lbl.setText(lbl.getText().substring(0, 18)+ (e.getStateChange()==1 ? "Onions Added and Order Price: " + OrderPrice : OrderPrice)); } }); checkbox4.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == 1) OrderPrice += 1; else OrderPrice -= 1; lbl.setText(lbl.getText().substring(0, 18) + (e.getStateChange() == 1 ? "Relish Added and Order Price: " + OrderPrice : OrderPrice)); } }); checkbox5.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == 1) OrderPrice += 1; else OrderPrice -= 1; lbl.setText(lbl.getText().substring(0, 18)+ (e.getStateChange()==1 ? "Mayo Added and Order Price: " + OrderPrice : OrderPrice)); } }); checkbox6.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==1) OrderPrice += 1; else OrderPrice -= 1; lbl.setText(lbl.getText().substring(0, 18)+ (e.getStateChange()==1 ? "Pico de Gallo Added and Order Price: " + OrderPrice : OrderPrice)); } }); checkbox7.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==1) OrderPrice += 1; else OrderPrice -= 1; lbl.setText(lbl.getText().substring(0, 18)+ (e.getStateChange()==1 ? "Fry sauce Added and Order Price: " + OrderPrice : OrderPrice)); } }); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == msg) { JComboBox cb = (JComboBox)e.getSource(); String item = (String)cb.getSelectedItem(); switch(item) { case "Regular Dog": OrderPrice = 0; OrderPrice += 3; lbl.setText("You selected Regular Dog. Order Price: " + OrderPrice); break; case "Corn Dog": OrderPrice = 0; OrderPrice += 5; lbl.setText("You selected Corn Dog. Order Price: " + OrderPrice); break; case "Chili Dog": OrderPrice = 0; OrderPrice += 6; lbl.setText("You selected Chili Dog. Order Price: " + OrderPrice); break; default: break; } } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!