Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Print Statement help

 
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 ] );
}

}
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • 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.
 
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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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
 
lowercase baba
Posts: 12766
51
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.
 
Are you okay? You look a little big. Maybe this tiny ad will help:
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!