Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Print Statement help

 
Shae Weathers
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Shae Weathers
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much! I eventually came up with the solution shortly after posting the question
 
David Patrzeba
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Patrzeba wrote:

Is this how you compare strings ?
 
David Patrzeba
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Patrzeba
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good to know, I'll avoid giving out direct answers like that in the future.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic