Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java (blueJ)

 
sarah wilson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, can someone help me?! I am reallly desperate, I have this task for uni to do, and I cant gets passed this error.... well its not really an error, it compiles fine, but when run doesnt do what its supposed to do.

The task... consists of 6 classes, product, inventory, client, client list, regular client and system boundary (where all the methods go) I have NOT to make any changes to product or inventory as they work perfectly. It is basically a system that holds information on products and clients, you can add products, check their quantity etc... Now the problem I am having is that... I am trying to get it to display how much a Client owes,(you need to add a new client first) and its not, instead i am getting a new windows with all this weird code. the code I have entered is:

in class Client List: public int checkOwing(String nm){
Client target = find(nm);
if (target != null)
return target.checkOwing();
else
return 0;

in class Client: public int checkOwing(){
return owing;
}

public boolean check(String nm) {
return name.equals(nm);
}

and in system boudnary: int ow = Integer.parseInt(owing.getText());
if (event.getSource() == amowing){
message.setText(theClients.checkOwing (nm) + "owes" + ow);
}


THe next problem I have, is I should be able to allow a client to pay some or all of the amount owed, and that is not working either. The code is:

Class Client List: public void decreaseOwing(String nm, int ow){
//Client target = find(ow);
Client target = find("" + ow );
if (target != null)
target.decreaseOwing(ow);
}

Class Client: public void decreaseOwing(int ow){
owing -= ow;
}

Class System Boundary: if(event.getSource() == pay){
try {
theClients.decreaseOwing(nm, ow);
message.setText("you have paid of your amount owing");
}
catch (Exception ex) {
message.setText (ex.toString());
}
}

Please if anyone has any idea, can you help me out. contact me at i_love_winegums@yahoo.co.uk

Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instead i am getting a new windows with all this weird code.


From a quick glance at your code, it looks like that "windows with all this weird code" is probably a printout of the exception. If would probably help, if you mention what the exceptions (printout) are.

Henry
 
sarah wilson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The weird code that appears when i press the amount owed buttons is:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:468)
at java.lang.Integer.parseInt(Integer.java:497)
at SystemBoundary.actionPerformed(SystemBoundary.java:146)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for posting the exception print-out. The name of the exception gives a hint as to what it is. Number Format Exception means there is something you are trying to read as a number which isn't; the mention of "" suggests you have not entered anything where your number ought to be. Do any of your classes feature in the list of exceptions? If they do, the first-named class of your is where the problem is.
Is SystemBoundary one of yours? [Line 5] Look at where you are entering a number, and do you have a number at all or is it in the wrong format? Your printout gives you another useful hint as wo where to look; there are line numbers.
CR
 
sarah wilson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Client I have this: protected int owing;
protected String name;

public Client(int initow, String nm){
owing = initow;
name = nm;

} // constructor

//return amount a client owes
public int checkOwing(){
return owing;
}

public boolean check(String nm) {
return name.equals(nm);
}

As part of my check owing... and its ints... which i have in my method in system boundary.

and again in client list: public int checkOwing(String nm){
Client target = find(nm);
if (target != null)
return target.checkOwing();
else
return 0;
everything is an int that needs to be

and again in system boundary: int ow = Integer.parseInt(owing.getText());
if (event.getSource() == amowing){
message.setText(theClients.checkOwing (nm) + "owes" + ow);
}

I am sooo confused by this. would I be able to send my actual classes to anyone to see if they understnad?
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The exception is happening somewhere around line 146 in the SystemBoundary.java file.

Henry
 
sarah wilson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone I have now got it working, I changed the int to a string, although i really thoguht it would be an int. What is now confusing that ow is a string in my system boundary mbut still an int everywhere else... but changing it to a string is the only way i could get it to work?

The problem I have now, is that it is not taking off any money when i enter an amount and then press pay... it is bringing up the statement when i press the jbutton pay.... but not actually taking the money i enter away from their amount owed.

System Boundary: if(event.getSource() == pay){
try {
message.setText("you have paid of your amount owing");
}
catch (Exception ex) {
message.setText (ex.toString());
}
}

Client List: public void decreaseOwing(String nm, int ow){
//Client target = find(ow);
Client target = find("" + ow );
if (target != null)
target.decreaseOwing(ow);
}

Client: public void decreaseOwing(int ow){
owing -= ow;
}
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This line is correct, *but* it only works if owing contains a String that represents a number, otherwise it throws the exception as seen above.

You have to either add some exception handling (a try-catch block) around this line of code, or you could use a JSpinner instead of the JTextField that owing probably is now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic