Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Translate a Numeric value from Database to a text  RSS feed

 
syed yousuf sadat
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I am writing some data from the database to the CSV file. One of the fields in the database is an int and definitely it prints a number on the csv file. I want to somehow translate those number to a text and then show it to csv file. For instance I want to display "Completed" if the field is 7 in the db. Can I do some kind of switch or if else statement to achieve this.

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can. Show us the code you are having trouble with.
 
syed yousuf sadat
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the way I am trying to achieve this is that I have made constants for the numbers 1- 9 and their corresponding values. Right now I get 1 in my csv file but I want it to to be Created.



In the DAO Layer, I am trying to convert it but I am getting NumberFormatException because the setter expects an int and I am passing a String.

 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:

This assumes that there is a signature of setEsignTransStatusId that accepts a String.
 
syed yousuf sadat
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
takes an int, so therefore I am doing the parsing.
 
syed yousuf sadat
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get compile time error if I don't parse the String to an int
 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
syed yousuf sadat wrote:I have made constants for the numbers 1- 9 and their corresponding values.
How about an array of constants
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:How about an array of constants

Or, if the numbers aren't sequential, a Map!
 
syed yousuf sadat
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey. You haven't even understood the problem. Please read the question again.
 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
syed yousuf sadat wrote:Hi. I am writing some data from the database to the CSV file. One of the fields in the database is an int and definitely it prints a number on the csv file. I want to somehow translate those number to a text and then show it to csv file. For instance I want to display "Completed" if the field is 7 in the db. Can I do some kind of switch or if else statement to achieve this.

Thanks
Instead of a switch,

If I "don't get it" then maybe you need clearer requirements.
 
syed yousuf sadat
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not what I am asking.
 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
syed yousuf sadat wrote:This is not what I am asking.
You'll have to be clearer about what you are asking. Your requirements are vague.
 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course you could do this with a switch() or else-if's, but (with the little you gave me to go on) there's probably a better way.
 
Ron McLeod
Saloon Keeper
Posts: 1600
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
syed yousuf sadat wrote:In the DAO Layer, I am trying to convert it but I am getting NumberFormatException because the setter expects an int and I am passing a String.



It is not possible to convert a String which does not represent a numeric value to an Integer or int.

Shouldn't you be using the Integer constant rather than the String constant?
 
syed yousuf sadat
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron, you are correct but I need to translate 1 to Created. 1 is stored in the db and in my csv file instead of 1 I want to display Created. That's why I was trying to convert it. I am not sure what is the best approach or if it is even achievable.
 
syed yousuf sadat
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this but it throws.
 
Ron McLeod
Saloon Keeper
Posts: 1600
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I didn't understand your original post.  Is your question something like: How do I convert a set of numeric values to corresponding set text values?

For example:
    1 => Created
    2 => Failed
    3 => Aborted
 
syed yousuf sadat
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's correct Ron.
 
Ron McLeod
Saloon Keeper
Posts: 1600
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well - you have lots of choices:
    an array of Strings (as suggested earlier)
    a Map with Integer keys and String values
    an enum with fields
    a switch statement
    if-else statements

If you're not sure which method to use or how to begin, we can help/
 
syed yousuf sadat
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I am doing it is



This gives me. I know that I am getting an int from the db but I am converting it then why?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
syed yousuf sadat wrote:
This gives me. I know that I am getting an int from the db but I am converting it then why?


Well, the parseInt() method requires a string that can be converted to an int. The word "created" can't be converted back to an int by parsing. If you need to convert it back to the original value, you will need to that yourself as well.

Henry
 
syed yousuf sadat
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry. I don't want to convert it back to its original value. I want to print "Created" and the the reason I was parsing it was because the setter take an int not a String. How would I go about printing the word "Created" then?
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have been told several times in this thread how you can print “created” if the status is 7. Why are you using numbers for status rather than an enumerated type?
 
syed yousuf sadat
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason I am asking this is because I cannot change the model of the project. Status is being defined as int and I cannot change it. This status is coming from the db and in the UI I need to change it to a text. Everyone says you can do that but no one has posted a single piece of code to give a hint.

You tell me how could you change the number to a text and display it in a csv file. I am using Apache commons CSV API.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
syed yousuf sadat wrote: but no one has posted a single piece of code to give a hint.

You been given numerous different ideas on how to accomplish this; but no one here is going to write the code for you. You write the code, and we help you with it. That's the way things work.
 
syed yousuf sadat
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for your idea.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave you my idea days ago. We have yet to see you try and put any of the ideas given to you into code.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
syed yousuf sadat wrote:You tell me how could you change the number to a text and display it in a csv file.

This is a simplistic way. Better to use an enum or map to determine the text that corresponds to the db values:
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
syed yousuf sadat wrote:The reason I am asking this is because I cannot change the model of the project. Status is being defined as int and I cannot change it. This status is coming from the db and in the UI I need to change it to a text.


If your database isn't changed, and the model that the database feeds can't be changed, then why are you trying to change code between the database and the model? Isn't it obvious that this isn't the place to do it?

You need to change the code from the last point (which isn't changed) to the point where you need it changed. This is either between the model and the GUI that needs the change, or the model and the CSV file that you need generated.

Henry
 
syed yousuf sadat
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henry for you advice and this is what I did. I changed it on the UI layer.
 
syed yousuf sadat
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how I did it.



Thank you all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!