• Post Reply Bookmark Topic Watch Topic
  • New Topic

matching number from set of numbers.  RSS feed

 
sam subas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,


i want to check whether there is a "o" in 2nd in set of number.
for example:
customer no: 102345

my system check and decided what type of customer based on the number. if the index number has 'o' as 2nd value then that customer is a business customer. else if it does not contain 'o' then personal customer. both type of customer are defined based on the value in the 2nd digit.

to do this what can i do. i have seen substring is used for string. for number is there any way to do.. i don't want to convert the digit in to string to identify it.

i'm have basic knowledge in java. i am a beginner.

please help me to solve the issue.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first, are you searching for an '0' (digit) or an 'o' (letter)? Not that it makes much difference in how you do it, but these details are important. You should get in the habit of typing/saying what you really mean.

Next...the first step whenever you write ANY code is to NOT think in Java, but in English (or whatever natural language you prefer). So...if you had to write down the steps so that a child could understand and follow them, what would you say?

I mean for you to literally write them down, either on paper or electronically. The idea is that by having to write them, you are forcing yourself to REALLY think about the problem.
 
Alex Petsche
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see you mention substring to find a position of a character in a string, but you want to do this with integers and not have to convert it to a string. Why do you not want to convert the number to a string? It isn't hard to convert to a string and then back to a number. For example:



I don't know if there is a way to index integer positions as you do in a string with substring and a quick google search didn't reveal anything to me either. If the reason is that you need it to be an integer after you find what is in the second integer you can parse the string back to an int with Integer.parseInt(String);.

 
Tony Docherty
Bartender
Posts: 3268
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't hard to convert to a string and then back to a number. For example: ...

Sorry but that's a horrible way to convert an int to a String. Why not just use:
 
Alex Petsche
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
It isn't hard to convert to a string and then back to a number. For example: ...

Sorry but that's a horrible way to convert an int to a String. Why not just use:


Ah thanks for the constructive criticism! I didn't know that method existed and it saves me from having to initialize it as empty as well. I guess I showed my "green horns" there haha.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
It isn't hard to convert to a string and then back to a number. For example: ...

Sorry but that's a horrible way to convert an int to a String. Why not just use:


I generally prefer since the single method name can accept any type. Makes copy/paste easier.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I generally prefer
String.valueOf(a)

Yes, that is equally as good.
I showed the Integer.toString() method as it is the reverse of the Integer.parseInt() method already shown.

On other forums I've seen people get quite pedantic about which is the 'correct' approach ie to use Integer.toString() or String.valueOf() personally I use the one that is most appropriate at the time. What's the general view here?
 
sam subas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:first, are you searching for an '0' (digit) or an 'o' (letter)? Not that it makes much difference in how you do it, but these details are important. You should get in the habit of typing/saying what you really mean.

Next...the first step whenever you write ANY code is to NOT think in Java, but in English (or whatever natural language you prefer). So...if you had to write down the steps so that a child could understand and follow them, what would you say?

I mean for you to literally write them down, either on paper or electronically. The idea is that by having to write them, you are forcing yourself to REALLY think about the problem.
Hi

hi,

It is zero. i want to find whether the customer number contain zero as its 2nd value from left side. based on that there is some functionality to be added to the system. sorry i forgot to mention what i am referring to '0'.
Thank you for the guidance.
 
sam subas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

converting the ID to string and then checking the number for zero (from left side 2nd value) and then converting back to integer when storing it to database can be best way.
i want to know whether we can archive by keeping it as integer and check for value. now based on the post now i feel it is better to do it by converting to string.

what i really want to do is:
when user enter the customer number and other fields and submit the form.
- certain value need to be updated in the database based on the type of customer number. only different between business and personal customer is the zero in the 2nd position from left side.
to do this i thought to detect the value and check it contain zero if contain database will be updated based on the customer number.

i have a opinion the way i can archived it. i don't know whether it is possible to do.
- get the customer id text field value and then store it variable.
- using sub string and get the 2nd value from left side and store it variable.
-finally using the if condition to check ID has zero.

I haven't done any coding in java with database. so i am bit uncertain to proceed with the code. i really love to get improved skill in java + database application.
Is there any free e-books available . please help me

thanks in advance.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
I generally prefer
String.valueOf(a)

Yes, that is equally as good.
I showed the Integer.toString() method as it is the reverse of the Integer.parseInt() method already shown.

On other forums I've seen people get quite pedantic about which is the 'correct' approach ie to use Integer.toString() or String.valueOf() personally I use the one that is most appropriate at the time. What's the general view here?


In a lot of contexts, they produce the same results, so it doesn't really matter.

As for which one is actually correct, if your problem is, "I have this int, and I want to convert it to a String," then Integer.toString(i) is exactly what you need. I wouldn't say it's any better than String.valueOf(i), but if you want to be pedantic about the absolute best fit, then technically that's it. On the other hand, if one of my programmers when around changing existing String.valueOf(i) calls to Integer.toString(i), he and I would have to have a talk.

If your problem is, "I have some stuff (which might be an int) and I want a String representation of it," then String.valueOf(x) is the way to go. And even if you know it's an int, if you have a bunch of code close together that's doing the same thing with values of different types, I'd prefer a bunch of String.valueof(x) calls rather than an Integer.toString(i) here and a Double.toString(d) there. It's easier to add more cases with copy paste, with one less thing to forget to change, and when you're reading the code, it's easier to see that all those String.valueOf(x) calls are doing the same thing, so your eye and mind can ignore them and focus on what's different about the handling for the various types.

 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice to see we are in agreement on this.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to know whether we can archive by keeping it as integer and check for value. now based on the post now i feel it is better to do it by converting to string.

You can do this keeping it as an int, whether it's better or worse than converting it to a string depends on your requirements.

To do it as an int do the following (Note this assume the customer number contains at least 2 digits):
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!