Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: numbers to words using do-while & if-else & JOptionPane

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Location
    Philippines
    Posts
    4
    My Mood
    Amazed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question numbers to words using do-while & if-else & JOptionPane

    greetings everyone.. this is my first post.. i would just like to ask how to shorten my code.. basically, this is a working code already.. i was told that there is a way to shorten this.. the given problem was to make a working code that would get a number input from 1-15 and display it as a word(one, two..., fifteen).. /*i just shortened it here*/

    our college instructor wouldnt tell us how coz he expects us to figure this out on our own.. this code was already submitted to my instructor.. so, this is not in any way a project or a school assignment anymore.. i would just like to know how to shorten my code..

    to my disadvantage, i last tackled java way back in 2004.. and havent been using it since... and now, i am stuck to figuring out how to shorten my code..

    here it is...
    import javax.swing.*;
     
    public class NumbersToWords {
    	public static void main(String[] args){
     
    		int n=0;
    		String word="";
     
    		do{
    			word = JOptionPane.showInputDialog("Enter a Number: ");
    			n = Integer.parseInt(word);
     
    			if( n == 1 ){
    				JOptionPane.showMessageDialog(null, "One");
    			}
    			else if( n == 2 ){
    				JOptionPane.showMessageDialog(null, "Two");
    			}
    			else if( n == 3 ){
    				JOptionPane.showMessageDialog(null, "Three");
    			}
    			else if( n == 4 ){
    				JOptionPane.showMessageDialog(null, "Four");
    			}
    			else if( n == 5 ){
    				JOptionPane.showMessageDialog(null, "Five");
    			}
    			else{
    				JOptionPane.showMessageDialog(null, "Invalid Number!");
    			}
    		}while((n<=5) && (n>=0));
    		System.exit(0);
    	}
    }

    im hoping you guys could give me a helping hand.. like to just point me in the right direction, and i will make the codes myself.. ^_^ thank you so much
    Last edited by mikkko; July 14th, 2011 at 10:52 AM. Reason: added [highlight][\highlight]


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,301
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: numbers to words using do-while & if-else & JOptionPane

    Just my guess, but I'd say he was hinting at using an array.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    mikkko (July 14th, 2011)

  4. #3
    Junior Member
    Join Date
    Jul 2011
    Location
    Philippines
    Posts
    4
    My Mood
    Amazed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: numbers to words using do-while & if-else & JOptionPane

    actually, there were some that used array(just found out today, coz that problem was given last tuesday(philippine time))..

    i made mine too long coz that was all i know.. ^_^

    would it be possible to assign numbers as Strings and initialize them with the equivalent words like:
    String 1="One", 2="Two"....

    to be able to use them as increments?

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,301
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: numbers to words using do-while & if-else & JOptionPane

    Quote Originally Posted by mikkko View Post
    actually, there were some that used array(just found out today, coz that problem was given last tuesday(philippine time))..

    i made mine too long coz that was all i know.. ^_^

    would it be possible to assign numbers as Strings and initialize them with the equivalent words like:
    String 1="One", 2="Two"....

    to be able to use them as increments?
    If you really want to go that route, it sounds like a job for a Map.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #5
    Junior Member
    Join Date
    Jul 2011
    Location
    Philippines
    Posts
    4
    My Mood
    Amazed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: numbers to words using do-while & if-else & JOptionPane

    can you please enlighten me as to what that is? im like level 1(EXP:0) here.. ^_^

  7. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: numbers to words using do-while & if-else & JOptionPane

    would it be possible to assign numbers as Strings and initialize them with the equivalent words
    The best way to find out is to try it - did you try it?

    However, Java naming rules won't let you have variable names starting with digits.

    There is an alternative to the array solution, and that is to use an enum. But you probably haven't looked at enums yet...

  8. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,301
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: numbers to words using do-while & if-else & JOptionPane

    Quote Originally Posted by mikkko View Post
    can you please enlighten me as to what that is? im like level 1(EXP:0) here.. ^_^
    The first two results for googling "java map":
    The Map Interface (The Java™ Tutorials > Collections > Interfaces)
    Map (Java 2 Platform SE v1.4.2)

    But really, stop worrying so much. There are always going to be ways to shorten your code this early in the game, I really wouldn't spend THAT much time trying to cut it down- unless that's your assignment, in which case asking us how to do it defeats the purpose. Does it work? Do you understand why? Then don't worry about it.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #8
    Junior Member
    Join Date
    Jul 2011
    Location
    Philippines
    Posts
    4
    My Mood
    Amazed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: numbers to words using do-while & if-else & JOptionPane

    Quote Originally Posted by dlorde View Post
    The best way to find out is to try it - did you try it?

    However, Java naming rules won't let you have variable names starting with digits.

    There is an alternative to the array solution, and that is to use an enum. But you probably haven't looked at enums yet...
    i tried that but i got an error..

    anyhoo, thank you for you help guys.. ill look into what you guys have mentioned.. thanks again..

  10. #9
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: numbers to words using do-while & if-else & JOptionPane

    Quote Originally Posted by mikkko View Post
    i tried that but i got an error..
    If you try something and get an error and you'd like help with it, post up the code you tried and the full text of the error message.

Similar Threads

  1. Counting Words in a File with a Loop
    By bengregg in forum Loops & Control Statements
    Replies: 17
    Last Post: February 11th, 2011, 09:11 AM
  2. Replies: 4
    Last Post: November 14th, 2010, 04:02 PM
  3. counting words of a text file
    By maybach230 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 6th, 2010, 03:40 PM
  4. Help: Num to words
    By shamed in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 7th, 2010, 05:55 PM
  5. Question: Converting number to words.
    By shamed in forum Java Theory & Questions
    Replies: 6
    Last Post: January 1st, 2010, 03:28 AM