Two Laptop Bag*
The moose likes Beginning Java and the fly likes Beginner help with arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Beginner help with arrays" Watch "Beginner help with arrays" New topic
Author

Beginner help with arrays

r jones
Greenhorn

Joined: Sep 07, 2011
Posts: 2
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".
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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?
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
Why did you use null instead of "zero"?


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Kristjan Toots
Ranch Hand

Joined: Jun 03, 2011
Posts: 59

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?


Please correct my english.
Vigneswaran Marimuthu
Greenhorn

Joined: Aug 30, 2011
Posts: 24



you should do like this.

Regards,

Vigneswaran.M
Vigneswaran Marimuthu
Greenhorn

Joined: Aug 30, 2011
Posts: 24

args[0] is throwing ArrayIndexOutOfBounceException for me !!!
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

that's because you have not specified any argument while running you'r program.
Vigneswaran Marimuthu
Greenhorn

Joined: Aug 30, 2011
Posts: 24

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

Joined: Sep 07, 2011
Posts: 2
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
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11156
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beginner help with arrays
 
Similar Threads
Assignment 1.4 (Say)
Trouble trying to increase the scale of my program
can anyone solve this ?
Beer Song
Can Someone Explain to Me This Code?