wood burning stoves*
The moose likes Beginning Java and the fly likes Print Statement help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Print Statement help" Watch "Print Statement help" New topic
Author

Print Statement help

Shae Weathers
Greenhorn

Joined: Jun 26, 2010
Posts: 8
I recently wrote a program will print out the spelling of numbers between 0-25 when inputted, now I need to figure out how to get the program to print out the statement "out of range" when a number is entered outside of that range. I tried using the if else statement, but I kept getting an error message. This is what I have so far:

public class Say
{

static String number[];

public static void main( String[] args )
{
number = new String[26];
number[0] = "zero";
number[1] = "one";
number[2] = "two";
number[3] = "three";
number[4] = "four";
number[5] = "five";
number[6] = "six";
number[7] = "seven";
number[8] = "eight";
number[9] = "nine";
number[10] = "ten";
number[11] = "eleven";
number[12] = "twelve";
number[13] = "thirteen" ;
number[14] = "fourteen";
number[15] = "fifteen";
number[16] = "sixteen";
number[17] = "seventeen";
number[18] = "eighteen";
number[19] = "nineteen";
number[20] = "twenty";
number[21] = "twenty-one";
number[22] = "twenty-two";
number[23] = "twenty-three";
number[24] = "twenty-four";
number[25] = "twenty-five";



int n = Integer.parseInt( args[0] );
System.out.println( number[ n ] );
}

}
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

your code doesnt handle the Exception.

Note : please use code tag while posting your code, so that it can be easy to read.
David Patrzeba
Greenhorn

Joined: Jun 08, 2010
Posts: 14
Shae Weathers
Greenhorn

Joined: Jun 26, 2010
Posts: 8
Thank you so much! I eventually came up with the solution shortly after posting the question
David Patrzeba
Greenhorn

Joined: Jun 08, 2010
Posts: 14
Your welcome.

Obviously this is the simple answer which you can improve by adding a while loop around the statement that allows the user to retry his input if he fails the evaluation.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

David Patrzeba wrote:

Is this how you compare strings ?


[My Blog]
All roads lead to JavaRanch
David Patrzeba
Greenhorn

Joined: Jun 08, 2010
Posts: 14
Oh wow....yea way wrong...i'll fix that right now. I really shouldn't post when I'm tired
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11257
    
  16

Also, we prefer if you don't just hand out the solution. Many of the questions here are homework assignments, and giving someone the solution doesn't help them learn. Even worse, if they submit your solution as their own, or if it even LOOKS like they handed in your solution, many schools would consider that a breach of ethics (plagiarism/cheating) and the student could be disciplined and potentially expelled.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
David Patrzeba
Greenhorn

Joined: Jun 08, 2010
Posts: 14
Good to know, I'll avoid giving out direct answers like that in the future.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Print Statement help