java - Input Mismatch Exception (i think its a dumb mistake) -
i'm getting inputmismatchexception when press "next" button. here code panel has button:
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class panelsampleyard extends jpanel { private jlabel label; private displaysampleyard display; int times = 0; public panelsampleyard() { setlayout(new borderlayout()); setpreferredsize(new dimension(200, 125)); label = new jlabel("green , grow mowing company", swingconstants.center); add(label, borderlayout.north); display = new displaysampleyard(); add(display, borderlayout.center); jpanel subpanel = new jpanel(); subpanel.setlayout(new flowlayout()); jbutton next = new jbutton("next"); jbutton quit = new jbutton("quit"); next.addactionlistener(new listener1()); quit.addactionlistener(new listener2()); subpanel.add(next); subpanel.add(quit); add(subpanel, borderlayout.south); } private class listener1 implements actionlistener { public void actionperformed(actionevent e) { display.next(display.contents(times)); times++; } } private class listener2 implements actionlistener { public void actionperformed(actionevent e) { system.exit(0); } } }
and code display inside panel:
import javax.swing.*; import java.awt.*; import java.util.*; public class displaysampleyard extends jpanel { private jlabel label; private jtextfield last, first, size, cost, total; double sum = 0; int index = 0; public displaysampleyard() { setlayout(new gridlayout(5,2)); last = new jtextfield(""); add(new jlabel("last name:")); add(last); first = new jtextfield(""); add(new jlabel("first name:")); add(first); size = new jtextfield(""); add(new jlabel("lawn size:")); add(size); cost = new jtextfield(""); add(new jlabel("total cost:")); add(cost); total = new jtextfield(""); add(new jlabel("running total:")); add(total); } public void next(customer c) { last.settext(c.getlastname()+""); first.settext(c.getfirstname()+""); size.settext(c.getsize()+""); cost.settext(c.getcost()+""); sum = sum + c.getcost(); total.settext(""+sum); } public customer contents(int index) { scanner infile = new scanner("greengrow.txt"); int reps = infile.nextint(); customer[] array = new customer[reps]; for(int x = 0; x<reps; x++) { array[x].setlastname(infile.next()); array[x].setfirstname(infile.next()); array[x].setsize(infile.nextint()); } return array[index]; } }
the data file scanner reading contains:
5 jeffers tom 5000 smith sam 10000 nevar tina 15000 kim lisa 20000 black kim 30000
what's wrong code? in advance!
p.s. there few more classes, don't think causing problem
when use scanner
read file, should construct file
type parameter, scan values specified file. like:
new scanner(new file("greengrow.txt"));
if construct string
, scanner
scan values specified string
Comments
Post a Comment