• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Beginner help with arrays

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to figure out how to use an array, I was hoping someone could decipher what i've been doing and perhaps point me int he right direction






public class java4 {


static String number[] = {
null, "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twleve", "thriteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eightteen", "nineteen", "twenty", "twentyone", "twentytwo", "twentythree", "twentyfour", "twentyfive", "twentysix",
"twentyseven", "twentyeight", "twentynine", "thirty", "thrityone", "thirtytwo", "thritythree", "thirtyfour", "thirtyfive", "thirtysix", "thirtyseven",
"thirtyeight", "thirtynine", "fourty", "fourtyone", "fourtytwo", "foutrythree", "fourtyfour", "fourtyfive", "fourtysix", "fourtyseven",
"fourtyeight", "foutrynine", "fifty", "fiftyone", "fiftytwo", "fiftythree", "fiftryfour", "fiftyfive", "fiftysix", "fiftyseven", "fiftyeight", "fiftynine",
"sixty", "sixtyone", "sixtytwo", "sixtythree", "sixtyfour", "sixtyfive", "sixtysix", "sixtyseven", "sixtyeight", "sixtynine", "seventy", "seventyone", "seventytwo",
"seventythree", "seventyfour", "seventyfive", "seventysix", "seventyseven", "seventyeight", "seventynine", "eighty", "eightyone", "eightytwo", "eightythree",
"eightyfour", "eightyfive", "eightysix", "eightyseven", "eightyeight", "eightynine", "ninety", "ninetyone", "ninety", "ninetythree", "ninetyfour",
"ninetyfive", "ninetysix", "ninetyseven", "ninetyeight", "nintynine", "one hundred"


};

public static void main(String[] args ){
int m = Integer.parseInt( args[0]);

System.out.println (number [m] );

}
}


..lol and yea i did write all those numbers 1 at a time.. I know theres probably a better way to write that but hey, im still learning.

btw the objective is to enter in a number ie: "22" and have the program respond with "twentytwo".
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One could find things to criticize about that code... but at a quick glance it looks like it should work. Assuming valid input and so on. So do you have a specific question about it?
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why did you use null instead of "zero"?
 
Ranch Hand
Posts: 59
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello & Welcome to the Ranch!
You really should use some code tags the readability will improve a lot.
I would like to agree with Paul. Now, what would you like to know?

 
Greenhorn
Posts: 24
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


you should do like this.
 
Vigneswaran Marimuthu
Greenhorn
Posts: 24
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
args[0] is throwing ArrayIndexOutOfBounceException for me !!!
 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's because you have not specified any argument while running you'r program.
 
Vigneswaran Marimuthu
Greenhorn
Posts: 24
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now i got it. while i executed the .jar file in cmd prompt i gave the command line argument and am getting the output !!!


Thanks Zandis.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

As you have been told, it is not a good idea to let nulls into your code. You should change that to "zero" or "nought" or similar.
 
r jones
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot guys for the help, it was args 0 that was throwing everything out of balance.
Also I replaced "null" with "Zero".

It sure does suck being off by something so little..

Btw im throwing in a scanner class for user input I think I should be able to do it but if I can't figure it out you'll be the first to know.

Thanks again for the advice!
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome

It's not scanner but Scanner. You should find enough information on the java.util.Scanner documentation page to get input from the keyboard. Beware of closing the Scanner, however, because you will close System.in and can only reopen it by starting a new JVM And careful with nextLine(), which has a little pitfall.
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

r jones wrote: It sure does suck being off by something so little..



That's programming for you. Yesterday I had something which didn't work because I put "itemStatus" instead of "itemstatus" in my code.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's why I said Scanner not scanner. Computers have an IQ of approximately this (hint: click on constant field values), so the tiniest spelling error will throw them.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The great thing with computers is that they always do exactly what you tell them to do.

The problem with us is that we don't always tell the computer exactly what we mean to do...
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And just now I fixed another bug where I had "profileStatus" instead of "profilestatus" in my code.

(In Javascript... so it just silently does nothing instead of telling my I have an undefined variable.)
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

r jones wrote: It sure does suck being off by something so little..


That's why you NEVER NEVER NEVER write more than 2-3 lines of code before compiling. I would even have compiled after only entering the first 3-4 elements in my array, and tested it with appropriate values, before entering all 101 elements.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic