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 2 of 2

Thread: I'm confused...

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question I'm confused...

    I was writing a code on how to say a number but my code was horrible so I asked for help. A lot helped but this code caught my attention:

    import javax.swing.*;
    public class word {
     
    	private static final String[] simple= { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
    											"ten", "evelen", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
    											"eightteen", "nineteen" };
     
    	private static final String[] tens= { null, null, "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" };
     
    	public static String say(int number) { 
    		return say(number, true); //default true
     
    	}
     
    	private static String say(int number, boolean sayZero) {
    		//simulate 123
    		if (number < 0)				return "negative "+say(-number, sayZero); //is 123 < 0? False
     
    		if (number == 0)			return sayZero?simple[number]:""; //is 123 < 0 ? False
     
    		if (number < 20) 			return simple[number];  //is 123 < 0? false
     
    		if (number < 100)			return tens[number/10]+say(number%10, false); //is 100 < 100? false
     
    		if (number < 1000) 			return say(number/100, false)+" hundred "+say(number%100); //is 100 < 1000? true--execute say(1) hundred   
     
    		if (number < 1000000) 		return say(number/1000, false)+" thousand"+say(number%1000, false);
     
    		if (number < 1000000000)	return say(number/1000000, false)+" million "+say(number%1000000, false);
     
    		return say(number/1000000000, false)+"billion "+say(number%1000000000, false);
    	}
     
    	public static void main(String[] args) {
     
    		String say = JOptionPane.showInputDialog("Enter a number");
    		int speak = Integer.parseInt(say);
    		JOptionPane.showMessageDialog(null,say(speak));
     
    	}
     
    }

    This code is 100% effective but confusing. So here are my questions:
    How does the say() method work?
    Why is it that it works even if its not on a loop?
    What is the role of boolean here?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I'm confused...

    Have you stepped through this with a debugger? That's going to help you understand much more effectively than somebody else explaining it will.

    What do you mean "how does it work"?
    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!

Similar Threads

  1. Confused about my Career
    By DanBrown in forum The Cafe
    Replies: 10
    Last Post: February 9th, 2011, 07:47 AM
  2. confused on loop
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 11th, 2010, 03:26 PM
  3. Confused with Arrays
    By Solidius in forum Collections and Generics
    Replies: 3
    Last Post: October 29th, 2010, 09:54 AM
  4. Confused about setting up JSP
    By mjpam in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: September 17th, 2010, 05:14 AM
  5. Confusion with C/C++
    By Eric in forum Java Applets
    Replies: 0
    Last Post: December 22nd, 2008, 02:18 PM

Tags for this Thread