I have this massively long(to me) code and I get so excited that it's finally finish so I go to compile it to only get 18 errors then after figuring out almost all the errors, I run the program and nothing shows...can anyone help??? I don't know whether I should post the entire code or not...
Can't figure out why nothing shows
Collapse
X
-
maybe it's stuck in an endless loop. Or you skipped the output with an if-statement. Or you returned from your function too early.
Just use a debugger and trace it step by step to see what's going on.
If you don't have a debugger, insert many
Code:System.out.println("breakpoint X")
between every 5 lines or so, choosing another number every time for "X".
Then run the program and see see which one was the last that was printed. This is the hot spot.
Posting the code would be very helpful, but if it's too long nobody would read it. Just post only the code snippet around the hot spot. -
I have this massively long(to me) code and I get so excited that it's finally finish so I go to compile it to only get 18 errors then after figuring out almost all the errors, I run the program and nothing shows...can anyone help??? I don't know whether I should post the entire code or not...
You fell for a beginners' trap: you kept on typing code for a long time, tried to compile the entire shebang, fixed all your typos and now you're stuck with some program that doesn't do what you want it to do.
Better take baby steps: design one or two small methods and test them in isolation. True, you have to write a bit of code for the tests but afterwards you can be pretty sure those few methods work. Then add some more methods and repeat the testing.
Maybe you have typed a bit more characters in total but you don't end up pulling your hair out in despair. Remember this.
kind regards,
JosComment
-
I just don't know what to do cause I write it by pieces and put it all together...and all my other programs are similar...I just don't know why nothing shows still...It's awfully long and deals with GUIs...multiple JPanels, blah blah blah...I'm just trying to learn how to use different layout types and it's just crashing on me. I suppose me posting the code wouldn't hurt.
Code:import java.io.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.DecimalFormat; import java.util.*; import java.lang.*; public class grid extends JPanel implements ActionListener { JFrame controllingFrame; JTextField atf,btf,ctf,dtf,etf,ftf,gtf,htf,itf,jtf,ktf,ltf,mtf,ntf,otf,ptf,qtf,rtf,stf,ttf; JLabel w,la,lb,lc,ld,le,w1,w2,w3,w4; Container contentPane; static final int AREA = 5; static final double RATE = 5.00; static final double TAX = 0.20; private static String CALC = "calculate"; private static String EXIT = "exit"; private static String nline= "\n"; DecimalFormat twoDigits = new DecimalFormat("0.00"); public grid(JFrame f) { controllingFrame = f; f.setLayout(new GridLayout(3,1)); //f.setVgap(5); JPanel labels = createLabels(); JPanel textgrid = createTextgrid(); JComponent buttons = createButtonPanel(); add(labels); add(textgrid); add(buttons); } public JPanel createLabels() { JPanel l = new JPanel(new GridLayout(0,6)); //l.setVgap(3); w = new JLabel("Week"); la = new JLabel("Hours"); lb = new JLabel("Tips"); lc = new JLabel("Gross"); ld = new JLabel("Taxes"); le = new JLabel("Net Pay"); l.add(w); l.add(la); l.add(lb); l.add(lc); l.add(ld); l.add(le); return l; } public JPanel createTextgrid() { JPanel tg = new JPanel(new GridLayout(4,6)); //tg.setVgap(3); w1 = new JLabel("One"); w2 = new JLabel("Two"); w3 = new JLabel("Three"); w4 = new JLabel("Four"); atf = new JTextField(AREA); atf.setEditable(true); btf = new JTextField(AREA); btf.setEditable(true); ctf = new JTextField(AREA); ctf.setEditable(false); dtf = new JTextField(AREA); dtf.setEditable(false); etf = new JTextField(AREA); etf.setEditable(false); ftf = new JTextField(AREA); ftf.setEditable(true); gtf = new JTextField(AREA); gtf.setEditable(true); htf = new JTextField(AREA); htf.setEditable(false); itf = new JTextField(AREA); itf.setEditable(false); jtf = new JTextField(AREA); jtf.setEditable(false); ktf = new JTextField(AREA); ktf.setEditable(true); ltf = new JTextField(AREA); ltf.setEditable(true); mtf = new JTextField(AREA); mtf.setEditable(false); ntf = new JTextField(AREA); ntf.setEditable(false); otf = new JTextField(AREA); otf.setEditable(false); ptf = new JTextField(AREA); ptf.setEditable(true); qtf = new JTextField(AREA); qtf.setEditable(true); rtf = new JTextField(AREA); rtf.setEditable(false); stf = new JTextField(AREA); stf.setEditable(false); ttf = new JTextField(AREA); ttf.setEditable(false); tg.add(w1); tg.add(atf); tg.add(btf); tg.add(ctf); tg.add(dtf); tg.add(etf); tg.add(w2); tg.add(ftf); tg.add(gtf); tg.add(htf); tg.add(itf); tg.add(jtf); tg.add(w3); tg.add(ktf); tg.add(ltf); tg.add(mtf); tg.add(ntf); tg.add(otf); tg.add(w4); tg.add(ptf); tg.add(qtf); tg.add(rtf); tg.add(stf); tg.add(ttf); return tg; } protected JComponent createButtonPanel() { JPanel bp = new JPanel(new FlowLayout()); JButton calcbutton = new JButton("Calculate"); calcbutton.setActionCommand(CALC); calcbutton.addActionListener(this); JButton exitbutton = new JButton("Exit"); exitbutton.setActionCommand(EXIT); exitbutton.addActionListener(this); bp.add(calcbutton); bp.add(exitbutton); return bp; } public void actionPerformed(ActionEvent event) { double a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,gross,paytotal,monthly; String cmd = event.getActionCommand(); a = Double.parseDouble(atf.getText()); a = a * RATE; b = Double.parseDouble(btf.getText()); c = a+b; f = Double.parseDouble(ftf.getText()); f = f * RATE; g = Double.parseDouble(gtf.getText()); h = f+g; k = Double.parseDouble(ktf.getText()); k = k * RATE; l = Double.parseDouble(ltf.getText()); m = k+l; p = Double.parseDouble(ptf.getText()); p = p * RATE; q = Double.parseDouble(qtf.getText()); r = p+q; ctf.setText(twoDigits.format(c)); htf.setText(twoDigits.format(h)); mtf.setText(twoDigits.format(m)); rtf.setText(twoDigits.format(r)); d = (c * TAX); i = (h * TAX); n = (m * TAX); s = (r * TAX); dtf.setText(twoDigits.format(d)); itf.setText(twoDigits.format(i)); ntf.setText(twoDigits.format(n)); stf.setText(twoDigits.format(s)); e = c-d; j = h-i; o = m-n; t = r-s; etf.setText(twoDigits.format(e)); jtf.setText(twoDigits.format(j)); otf.setText(twoDigits.format(o)); ttf.setText(twoDigits.format(t)); if(CALC.equals(cmd)) { gross = c+h+m+r; paytotal = e+j+o+t; monthly = paytotal+b+g+l+q; JOptionPane.showMessageDialog(controllingFrame, "Monthly Gross: $"+gross+nline+"Montly Paycheck Total: $"+paytotal+nline+"Montly Income: $"+monthly, "Calculations", JOptionPane.INFORMATION_MESSAGE); } else { System.exit(0); } } public static void createAndShowGUI() { JFrame frame = new JFrame("Paycheck Simple Calculations"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane(); frame.pack(); frame.setSize(700,700); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Comment
-
Okay, figured that problem out...but it doesn't do exactly what I want it to do...
I want it to be where the user inputs the number of hours worked in a week, their tips accumulated through the week, and the rate is stored in the program where it'll calculate the gross, take out a percent estimated as the tax and show net pay for the week. Then when you want to, you can hit the calculate button and make it show gross amount for the month, and net pay for the month. My buttons aren't working right and well, nothing is working right...I have the code right to where my components will show but I can't get it to be where I want it to be, kinda...Comment
-
Comment
-
New problems...
My buttons don't exactly work. My fields are empty, so I understand my calculate button not working, but why wouldn't the exit button work? I fixed it to where all my required fields have a starting entry of 0, but it just confuses me on why my exit button wouldn't work.Comment
Comment