473,473 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to avoid this problem?

7 New Member
I have an object receiving a short in argument. This argument is a code for a product and I want this number to be only between 1 and 1000.

If I do this:

Class a = new Class((short)965);
System.out.println(a.getArgument());

This will return 965 as supposed

Class a = new Class((short)1200);
System.out.println(a.getArgument());

This will cause my exception to pop and tell the user to use a code between 1 and 1000. Until here everything is fine. Now comes my problem:

Class a = new Class((short)66000);
System.out.println(a.getArgument());

This will return 464. I want my exception to pop here too. I want to keep my short typevbecause it is useless to take 32 bytes for only a number in the 4 digits mamxium.

How to solve this? For reference, this is my Class. Sorry it's in French but you will be able to understand easily my code don't worry !

Thanks a lot

package TP1;

public class Article
{
public static void main(String[] args)
{
Article a = new Article((short)66000,"Pen",(float)23.45);
System.out.println(a.getNumeroArticle());
}

private short numeroArticle;
private String descriptionArticle;
private float prixArticle;

public Article(short pNumeroArticle, String pDescriptionArticle, float pPrixArticle)
{
setNumeroArticle(pNumeroArticle);
setDescriptionArticle(pDescriptionArticle);
setPrixArticle(pPrixArticle);
}

public short getNumeroArticle()
{
return numeroArticle;
}

public String getDescriptionArticle()
{
return descriptionArticle;
}

public float getPrixArticle()
{
return prixArticle;
}

public void setNumeroArticle(short pNumeroArticle)
{
if(pNumeroArticle > 1000 || pNumeroArticle <= 0)
throw new IllegalArgumentException("Le numéro de l'article doit être compris entre 1 et 1000");
else
numeroArticle = pNumeroArticle;
}

public void setDescriptionArticle(String pDescriptionArticle)
{
descriptionArticle = pDescriptionArticle;
}

public void setPrixArticle(float pPrixArticle)
{
prixArticle = pPrixArticle;
}
}
Sep 14 '07 #1
5 1428
madhoriya22
252 Contributor
I have an object receiving a short in argument. This argument is a code for a product and I want this number to be only between 1 and 1000.

If I do this:

Class a = new Class((short)965);
System.out.println(a.getArgument());

This will return 965 as supposed

Class a = new Class((short)1200);
System.out.println(a.getArgument());

This will cause my exception to pop and tell the user to use a code between 1 and 1000. Until here everything is fine. Now comes my problem:

Class a = new Class((short)66000);
System.out.println(a.getArgument());

This will return 464. I want my exception to pop here too. I want to keep my short typevbecause it is useless to take 32 bytes for only a number in the 4 digits mamxium.

How to solve this? For reference, this is my Class. Sorry it's in French but you will be able to understand easily my code don't worry !

Thanks a lot
Expand|Select|Wrap|Line Numbers
  1. package TP1;
  2.  
  3. public class Article 
  4. {
  5. public static void main(String[] args) 
  6. {
  7. Article a = new Article((short)66000,"Pen",(float)23.45);
  8. System.out.println(a.getNumeroArticle());
  9. }
  10.  
  11. private short numeroArticle;
  12. private String descriptionArticle;
  13. private float prixArticle;
  14.  
  15. public Article(short pNumeroArticle, String pDescriptionArticle, float pPrixArticle)
  16. {
  17. setNumeroArticle(pNumeroArticle);
  18. setDescriptionArticle(pDescriptionArticle);
  19. setPrixArticle(pPrixArticle);
  20. }
  21.  
  22. public short getNumeroArticle()
  23. {
  24. return numeroArticle;
  25. }
  26.  
  27. public String getDescriptionArticle()
  28. {
  29. return descriptionArticle;
  30. }
  31.  
  32. public float getPrixArticle()
  33. {
  34. return prixArticle;
  35. }
  36.  
  37. public void setNumeroArticle(short pNumeroArticle)
  38. {
  39. if(pNumeroArticle > 1000 || pNumeroArticle <= 0)
  40. throw new IllegalArgumentException("Le numéro de l'article doit être compris entre 1 et 1000");
  41. else
  42. numeroArticle = pNumeroArticle;
  43. }
  44.  
  45. public void setDescriptionArticle(String pDescriptionArticle)
  46. {
  47. descriptionArticle = pDescriptionArticle;
  48. }
  49.  
  50. public void setPrixArticle(float pPrixArticle)
  51. {
  52. prixArticle = pPrixArticle;
  53. }
  54. }
Hi,
Use code tags while posting code.
Sep 14 '07 #2
r035198x
13,262 MVP
Do not cast to short here
Expand|Select|Wrap|Line Numbers
  1.          Article a = new Article((short)66000,"Pen",(float)23.45);
Your setMethod that you are calling from the constructor is already doing the check so there is no need to check there as well.
Sep 14 '07 #3
Nepomuk
3,112 Recognized Expert Specialist
(Is there an error in the QUOTE-Tag?)
Quote from kinghippo423:
Expand|Select|Wrap|Line Numbers
  1. package TP1;
  2.  
  3. public class Article 
  4. {
  5.     public static void main(String[] args) 
  6.     {
  7.         Article a = new Article((short)66000,"Pen",(float)23.45);
  8.         System.out.println(a.getNumeroArticle());
  9.     }
  10.  
  11.     private short numeroArticle;
  12.     private String descriptionArticle;
  13.     private float prixArticle;
  14.  
  15.     public Article(short pNumeroArticle, String pDescriptionArticle, float pPrixArticle)
  16.     {
  17.         setNumeroArticle(pNumeroArticle);
  18.         setDescriptionArticle(pDescriptionArticle);
  19.         setPrixArticle(pPrixArticle);
  20.     }
  21.  
  22.     public short getNumeroArticle()
  23.     {
  24.         return numeroArticle;
  25.     }
  26.  
  27.     public String getDescriptionArticle()
  28.     {
  29.         return descriptionArticle;
  30.     }
  31.  
  32.     public float getPrixArticle()
  33.     {
  34.         return prixArticle;
  35.     }
  36.  
  37.     public void setNumeroArticle(short pNumeroArticle)
  38.     {
  39.         if(pNumeroArticle > 1000 || pNumeroArticle <= 0)
  40.             throw new IllegalArgumentException("Le numéro de l'article doit être compris entre 1 et 1000");
  41.         else
  42.             numeroArticle = pNumeroArticle;
  43.     }
  44.  
  45.     public void setDescriptionArticle(String pDescriptionArticle)
  46.     {
  47.         descriptionArticle = pDescriptionArticle;
  48.     }
  49.  
  50.     public void setPrixArticle(float pPrixArticle)
  51.     {
  52.         prixArticle = pPrixArticle;
  53.     }
  54. }
(End of Quote)

As I'm sure you've guessed, it's a problem with the type short. A simple
Expand|Select|Wrap|Line Numbers
  1. System.out.println("Max: " + Short.MAX_VALUE);
  2. System.out.println("Min: " + Short.MIN_VALUE);
  3.  
will tell you, that the maximum value for short numbers is 32767 and the minimum is -32768. So the short number 32768 equals the short number -32768, as the line
Expand|Select|Wrap|Line Numbers
  1. System.out.println((short)32768 == (short) -32768);
  2.  
will prove.

To solve this problem, I'd change the class, so that it takes integers and then checks, if they are larger than Short.MAX_VALUE. If so, they should be set to a short number, which is too big but in range.

Greetings,
Nepomuk
Sep 14 '07 #4
kinghippo423
7 New Member
I tried what you siad but it doesn't work. It is possible to give a working little example of using short arguments and controlled its value to be only between 1 and 1000 no matter what is the value of the arguments?

I'm so lost right now. BTW, if I transform my short into int, I get the same problem because if i go over limit i will have the same problem. I will give a int over 1000 but still be between 1 and 1000 for Java.
Sep 14 '07 #5
Nepomuk
3,112 Recognized Expert Specialist
I tried what you siad but it doesn't work. It is possible to give a working little example of using short arguments and controlled its value to be only between 1 and 1000 no matter what is the value of the arguments?

I'm so lost right now. BTW, if I transform my short into int, I get the same problem because if i go over limit i will have the same problem. I will give a int over 1000 but still be between 1 and 1000 for Java.
Here's an example:
Expand|Select|Wrap|Line Numbers
  1. public void setNumeroArticle(int value) throws IllegalArgumentException
  2. {
  3.     if(value >= 1 && value <= 1000)
  4.     {
  5.         // set the value, which is converted to short
  6.     }
  7.     else // throw an exception
  8. }
  9.  
It's a very simple test, but it does the job.

The trick is, to cast it to short after checking the size.

Greetings,
Nepomuk
Sep 14 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: harry | last post by:
Hi, I have a program that runs on multiple client pc's. Occasionally one or more of those pc's use VPN to connect to another corporate network. When using VPN they need to set proxy server in...
6
by: | last post by:
I have a class with overloading operator new. (Because, if an identical object exists, return a pointer to existed object instead of a new pointer) It has no sense (it is dangerous) to allocate an...
44
by: Carlos Andr?s | last post by:
Hi everybody. I've got a problem. I'd like to avoid opening a new window when you have pressed the shift key and you click in the left button of the mouse. I've tried the next solution, in the...
17
by: Pushkar Pradhan | last post by:
I want to time my matrix multiply code (in MFLOPS). I want to run the code 100,000 times or some other big number. This can be done 2 ways (for and while loops): timer1 = time(NULL); for(n = 0;...
1
by: Scott Yost | last post by:
I have a managed class A which I import via a DLL. public __gc class A { public B otherClass; } And another class C which is just in a CPP file - not in the DLL. #include <b.h> class C
5
by: rover8898 | last post by:
Hello all, I have a programming problem/challenge. Let explain my scenario: I have a C program (distributed accross many files and functions) that is responsible for handling hardware. This...
4
by: gzaxar | last post by:
Hi to all in forum. It is my first post here. I am quite new in MsAccess programming. Here is a problem which i am facing to. I want to keep records of employees CV's. More specifically i want...
3
by: visu | last post by:
I am currently working on my personl website a completely DB driven web application. in that i ve updating the images thru my admin panel .. but i am getting the old images not the update one when...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
6
by: howa | last post by:
Since it is part fo the standard, why I always heard that we should avoid iframe? any comments? thanks.
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.