Question
JAVA CODE ERROR AND NO GUI SHOWING UP My GUI is not coming up and shows in console only and has text of enter population
JAVA CODE ERROR AND NO GUI SHOWING UP
My GUI is not coming up and shows in console only and has text of "enter population" when any number is enter nothing happen but when any letter is enter the error pop up which i will paste at the bottom.
package j.states;
import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Scanner; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel;
public class JStates extends JFrame implements ActionListener { String[] states = {"Az", "Ca", "Nv", "Ut", "Nm"}; static int [] JStates = new int[5]; static double [] area = new double[5]; JComboBox statelist = new JComboBox(states); JLabel lbl = new JLabel(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Population of 5 state(\"Az\", \"Ca\", \"Nv\"," + " \"Ut\", \"Nm\") "); for(int i =0; i < 5; i++) { JStates[i] = sc.nextInt(); } System.out.println("Enter area of 5 state(\"Az\", \"Ca\", \"Nv\", " + "\"Ut\", \"Nm\") "); for(int i = 0; i < 5; i++) { area[i] = sc.nextDouble(); } JStates ft = new JStates(); ft.setVisible(true); } public JStates() { setLayout(new FlowLayout()); setSize(400,300); setTitle("Population"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); statelist.setSelectedIndex(0); statelist.addActionListener(this); add(statelist); add(lbl); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == statelist) { JComboBox cb = (JComboBox) e.getSource(); String item = (String) cb.getSelectedItem(); switch (item) { case "Az": lbl.setText("Population of Az is " + JStates[0] + " and area: " + area[0]); break; case "Ca": lbl.setText("Population of Az is " + JStates[0] + " and area: " + area[0]); break; case "Nv": lbl.setText("Population of Az is " + JStates[0] + " and area: " + area[0]); break; case "Ut": lbl.setText("Population of Az is " + JStates[0] + " and area: " + area[0]); break; case "Nm": lbl.setText("Population of Az is " + JStates[0] + " and area: " + area[0]); break; } } } }
//ERROR
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at j.states.JStates.main(JStates.java:32) /Users/azgoat/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1 BUILD FAILED (total time: 2 minutes 25 seconds)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started