• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

convert number input into word

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to make a code that print out the number the user type in the terminal to print out it in letter. For example if you type in "1" the output will be "one" etc. Right now I get this message in the terminal when it prints out 1 "1[Ljava.lang.String;@4783da3f". the numbers the user is able to type in is 1-1000 but I start off right now with only 1-9.

When I type in for example "1" in the terminal it proints out the following: 1[Ljava.lang.String;@4783da3f
it's supposed to print out "one"




thank you in advance
 
Master Rancher
Posts: 4371
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This prints out the array,.  Arrays have no toString method of their own, relying on the basic Object one, which prints the hashcode (the output you see).

Use:
 
Ulrika Vildblomma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:
This prints out the array,.  Arrays have no toString method of their own, relying on the basic Object one, which prints the hashcode (the output you see).

Use:



Thank you, but it now prints out "one", "two", "three" etc when I type in one number in the terminal. I want to make it so that when I type in one number say 2 in the terminal it will print out only the word "two"
 
Marshal
Posts: 66637
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You mean you want to print “three” for 3? How can you get “three” out of your array? What does strl mean? If it isn't a well‑known word, call your array something different. And remember to write the [] earlier: String[] strl.... not String strl[].... please. It is better style to have the [] as part of the type.

By the way: if you search, you will find old threads discussing similar problems, but you will probably have to go back at least a year to find anything.
 
Master Rancher
Posts: 2250
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope this helps
 
Dave Tolls
Master Rancher
Posts: 4371
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulrika Vildblomma wrote:
Thank you, but it now prints out "one", "two", "three" etc when I type in one number in the terminal. I want to make it so that when I type in one number say 2 in the terminal it will print out only the word "two"



Well, you asked it to print the whole array, not just one part of it.

So you need to select the correct entry in the array to print out.

If the user enters '1' then how can you use that value to select the correct text from the array?
 
Campbell Ritchie
Marshal
Posts: 66637
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik, Ms Vildblomma was so close; please don't give her a complete solution. Though I think there are much simpler solutions than what you posted.

By the way, UV: please confine yourself to numbers in the range 0...9 for the time being. Only consider numbers > 9 later.
 
Swastik Dey
Master Rancher
Posts: 2250
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that.
 
Campbell Ritchie
Marshal
Posts: 66637
251
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies accepted
 
Ulrika Vildblomma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

You mean you want to print “three” for 3? How can you get “three” out of your array? What does strl mean? If it isn't a well‑known word, call your array something different. And remember to write the [] earlier: String[] strl.... not String strl[].... please. It is better style to have the [] as part of the type.

By the way: if you search, you will find old threads discussing similar problems, but you will probably have to go back at least a year to find anything.



Thank you!

Yes, when the user types in for example 3 in the terminal the program will print out "three"

okay so like this?

 
Ulrika Vildblomma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Swastik, Ms Vildblomma was so close; please don't give her a complete solution. Though I think there are much simpler solutions than what you posted.

By the way, UV: please confine yourself to numbers in the range 0...9 for the time being. Only consider numbers > 9 later.



Yes for now I focus on being able to print out 1-9 but when that is done I have to continue and do up to 1000.....
 
Ulrika Vildblomma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:

Ulrika Vildblomma wrote:
Thank you, but it now prints out "one", "two", "three" etc when I type in one number in the terminal. I want to make it so that when I type in one number say 2 in the terminal it will print out only the word "two"



Well, you asked it to print the whole array, not just one part of it.

So you need to select the correct entry in the array to print out.

If the user enters '1' then how can you use that value to select the correct text from the array?



No where in my post did I write that? Sorry for the misunderstanding in that case. I want to program to only print out "one" if the user types in 1 in the terminal
 
Campbell Ritchie
Marshal
Posts: 66637
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulrika Vildblomma wrote:. . .. . .

I suggest you change that to read. . . because you can cope with an input of 0;

Thank you!

That's a pleasure

okay so like this?

No. You are using a local variable, so you can't use the keyword static. You need a name for the array after [] and before =. The following would work:-
 
Ulrika Vildblomma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Okay I get it thank you! How do I print them out? For example if I type in 2 in the terminal how can it print out "two"?

 
Campbell Ritchie
Marshal
Posts: 66637
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pease don't quote the whole of the preceding post.

How would you normally get an element out of an array? Java™ Tutorials link.
 
Swastik Dey
Master Rancher
Posts: 2250
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You already have the number as input.  How about using this as index for the array.  For e.g. the input is num=0.  Now if you use this as array index str1[num] give you zero.
 
Ulrika Vildblomma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:How would you normally get an element out of an array? Java™ Tutorials link.



I've been reading this over and over but I don't understand how to use it with mine...
 
Ulrika Vildblomma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:You already have the number as input.  How about using this as index for the array.  For e.g. the input is num=0.  Now if you use this as array index str1[num] give you zero.



would that work so when the user types in 1 in the terminal it will print out "one"?
 
Swastik Dey
Master Rancher
Posts: 2250
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulrika Vildblomma wrote:

Swastik Dey wrote:You already have the number as input.  How about using this as index for the array.  For e.g. the input is num=0.  Now if you use this as array index str1[num] give you zero.



would that work so when the user types in 1 in the terminal it will print out "one"?



Why not when the number is 1 and the value in str1 array in index 1 position is one.
 
Ulrika Vildblomma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:Why not when the number is 1 and the value in str1 array in index 1 position is one.



okay, so my array is like this now



how should I change it into num[0]?
 
Swastik Dey
Master Rancher
Posts: 2250
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its not num[0] , it should be numberwords[num] where num is the variable where you are taking the user input.  For e.g.

user enters 0 means in num variable you have 0

numberwords[num i.e. 0] is zero

user enters 1 means in num variable you have 1

numberwords[num i.e 1] is one
 
Ulrika Vildblomma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:Its not num[0] , it should be numberwords[num] where num is the variable where you are taking the user input.



I see, it doesn't work I get error "cannot resolve symbol num
 
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you choose names like wordFor and number, then you can write:

Now, doesn't that say exactly what you want the program to do? Choose names that make your code tell a story.
 
Swastik Dey
Master Rancher
Posts: 2250
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulrika Vildblomma wrote:

Swastik Dey wrote:Its not num[0] , it should be numberwords[num] where num is the variable where you are taking the user input.



I see, it doesn't work I get error "cannot resolve symbol num



I have used num as an example, it should be replaced by the variable in which you are taking the user input.  It would be good if you could please post the latest code.
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if for some reason you have an aversion to a name like wordFor, then think of it in terms of this joke: "Dang, this smells like updog!" "What's updog?" "Nothing much, what's up with you?"

That is, the weirdness of a name like wordFor in the declaration:

is acceptable because it's just the setup for the punchline:
 
Ulrika Vildblomma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:I have used num as an example, it should be replaced by the variable in which you are taking the user input.  It would be good if you could please post the latest code.



 
Ulrika Vildblomma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:And if for some reason you have an aversion to a name like wordFor, then think of it in terms of this joke: "Dang, this smells like updog!" "What's updog?" "Nothing much, what's up with you?"



okay I understand
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So did you try using this program? What did it make you do as a user? It looks to me like it prompts you first "Skrive in ett tal" and then you have to type in a number. After that, the program again attempts to read the input for a string (line 24) which you assign to the variable word. That means the user will not only have to enter a number, they will also have to type in something that will be assigned to word. Is that really how you want the user interaction to be? It doesn't make sense to me given what you said you needed this program to do.
 
Swastik Dey
Master Rancher
Posts: 2250
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So did you get your desired output?
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This:

is basically the same thing as this:

Both numberwords and str1 are String arrays, not Strings.
 
Ulrika Vildblomma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:So did you try using this program? What did it make you do as a user? It looks to me like it prompts you first "Skrive in ett tal" and then you have to type in a number. After that, the program again attempts to read the input for a string (line 24) which you assign to the variable word. That means the user will not only have to enter a number, they will also have to type in something that will be assigned to word. Is that really how you want the user interaction to be? It doesn't make sense to me given what you said you needed this program to do.



Oh! Okay, what I thought it was doing was that it prints out the number the user typed in and that's it but okay than it doesn't yet do what I want it do do. I want to instead of printing out the number it should be replaced with the word
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulrika Vildblomma wrote:Oh! Okay, what I thought it was doing was that it prints out the number the user typed in and that's it but okay than it doesn't yet do what I want it do do. I want to instead of printing out the number it should be replaced with the word


If you only want the user to enter one thing, then only call Scanner.nextWhatever() one time. For example, if you need the user to enter a single number, then call scanner.nextInt() a single time. Any other calls to nextWhatever() methods will require the user to provide further input to match each call.

You don't actually need to ask for any other input after the user enters a digit that can be translated. Use the tal variable directly after the user enters a valid value.
 
Swastik Dey
Master Rancher
Posts: 2250
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 66637
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulrika Vildblomma wrote:. . .

At least that is what you wrote at first. Then you changed it to:-Please only change what is wrong. You had the input for the number working nicely, with what I thought was a good technique. Then you confused things by adding all that code. Hiding in there somewhere is the real answer to your problem, but you have confused things by asking for more input, which you don't need. If you had only left the good parts of the code alone, you would have a working solution by now. By the way: where does the variable i come from in line [edit]33[edit]? the compiler won't be able to understand it.

Please explain what you didn't understand from the tutorial link I showed you.
Please run a cut‑down version of your program, like this:-I have moved the variable declarations into the method, so they become local variables. I have removed the package declaration and some of the imports because you weren't using them. I have left only those parts of the code I think will work. You need one line and one line only to get it to print, “5fem”. Hint: it goes after line 11 and it starts, “System.out.println(....
If you get that to work, you wll have to work out how to get numbers > 9 to print.

[edit]Add = sign to line 5.
 
Ulrika Vildblomma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:So did you get your desired output?



No I get this when I type in 1 for example: 1[Ljava.lang.String;@7e6cbb7a
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulrika Vildblomma wrote:No I get this when I type in 1 for example: 1[Ljava.lang.String;@7e6cbb7a


Because you are trying to print an array of Strings, not a single String. Please read previous replies.
 
Campbell Ritchie
Marshal
Posts: 66637
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:If you choose names like wordFor and number . . .

They're better names than my suggestions from yesterday.
 
Ulrika Vildblomma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update:
I tried my best with doing arrays but I never got it to work so I tried a different approach that has been working with printing out numbers up to 20. But when I try to print out TWENTY ONE it just prints out TWENTY..if you please could check my code once more I'd be very grateful...I know there is something I'm missing but I don't know what.

 
Campbell Ritchie
Marshal
Posts: 66637
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go back to printing numbers between 0 and 9 only. Don't try 10+ until you have that working.
You will need to work out how to tease a number apart, so 456 can be separated into 4 5 6 (or 6 5 4). I don't think you hae got that worked out yet. You will probably use % 10 and / 10, but there is a risk of printing the numbers backwards. Find out about the StringBuilder class and how to use myStringBullder.insert(0, ...)
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 81 you have a break statement. That terminates the while loop before it can do anything else.
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!