Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Help with an Array

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Help with an Array

    Hi Java World,

    I am trying to learn Java on my own, and I am having trouble with an assignment I found on the internet. Here is the assignment:
    Write a program that will read in a number from 0 to 99 and spell out that number. The program must also report any values that are out of range.

    In other words, I want to type

    java Say 22

    and see

    twenty-two

    I thought about making two different arrays as you can see in my code. I can follow this pattern I have here with What If statements all the way to 99, but I am sure that there is a more effective way of doing this. My goal is two learn Java and not take the easy way out.

    Some things I have thought of doing was two divide the user input by 10, maybe use modulus, or just use checks. Please don't give me the answer, as I would like to rack my brain and figure it out. I have been doing this problem all day, and I think i got a headache.

    I also created methods for the arrays. I know that this all can be done in one method, but I wanted to learn on how to pass arrays to other methods.

    Any advice without giving me the answer would be much appreciated.

    Thanks

    public class Array {
    static Scanner scanner = new Scanner(System.in);
     
     
     
     
     
     public Array(){
          String number []= {"null","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};
           String numbers []= {"Twenty","Thirty","forty","Fifthy","Sixty","Seventy","Eighty","Ninety"};
     
            myArray(number,numbers);
     
     
      }
     
    public static void myArray(String num [],String num1[]){
        int array = scanner.nextInt();
        if(array<10)
        {
      String nums = Integer.toString(array);
     
     
      if(nums.equals("1"))
      {
      System.out.println(num[1]);
      }
      else if(nums.equals("2"))
      {
      System.out.println(num[2]);
      }
      else if (nums.equals("3"))
      {
      System.out.println(num[3]);
      }
      else if(nums.equals("4"))
      {
       System.out.println(num[4]);   
      }
      else if (nums.equals("5"))
      {
      System.out.println(num[5]);   
      }
      else if (nums.equals("6"))
      {
      System.out.println(num[6]); 
      }
     else if (nums.equals("7"))
     {
     System.out.println(num[7]); 
     }
     else if (nums.equals("8"))
     {
     System.out.println(num[8]);
     }
     else if (nums.equals("9"))
     {
     System.out.println(num[9]);
     }
     
     }
    if(array>19&&array<29)
    {
        System.out.println(num1[0]);
        System.out.println("hi");
     
    }
    }
     
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with an Array

    You forgot the numbers 11 through 19.

    Rather than manually converting a String "22" to "twenty-two", it's much simpler to convert the integer 22 to "twenty-two" through a little clever math.

    You can convert the String "22" to the integer 22 using the parseInt method of the Integer class.

    Then, using the division / and modulo (a.k.a. remainder) operators % you can extract each digit one by one.

    For example:

    25 / 10 = 2 (we're doing integer math so all decimals get dropped/truncated)
    25 % 10 = 5

    Now you just need to create some simple logic to map each digit to what gets displayed, independent of each other.

    There is one or two gotcha's I can think of:

    1. How are you going to handle the numbers 0 through 19 (There is a simple answer, ask if you can't figure it out)?
    2. How are you going to handle numbers which are out of the range 0 through 99?

    As a last note, I would recommend naming your variables with something more meaningful. For example, instead of having the variables num and nums, pick something more meaningful like singles and tens.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    loui345 (August 22nd, 2012)

  4. #3
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Help with an Array

    Thank you

    For your quick prompt and quite effective response. Much Thanks.

    Okay, now down to business, for the two gotcha numbers 0 -19, I can use a an IF and Then statement for the logic and make Java make a decision. For example, if the user input is in the range of 0-19 I don't need to use the modulus. Am I on the right track or did I completely miss the boat on that?

    Thanks.

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with an Array

    Yep, that's the simple solution. The same mechanism can be used to filter out numbers out of the range.

  6. #5
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Help with an Array

    Alright, my final question. I thought of trying to associate a variable with an index of an array to. So once I get my remainder from using modulus I can associate it with the appropriate array index. I hope this makes sense.

    I am having trouble associating it with an index.

    int m = Integer.parseInt(number[1]);

    It thows an exception.

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with an Array

    What's the exception it's throwing?

  8. #7
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Help with an Array

    Exception in thread "main" java.lang.NumberFormatException: For input string: "Three"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)

  9. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with an Array

    parseInt converts the string "3" to an integer 3, it can't convert the string "three" to an integer 3.

  10. #9
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Help with an Array

    Alright, I was able to use modulus and get the code to print out in the tenths part, then I ran a check using my array and yield me back fifty, forty, etc...

    My problem is getting it to read the first part of an integer divided by 10 using modulus. Since I setting my variable to an integer it drops off the tenths part when I set it to "0". I am also having trouble getting them to print independently of each-other. I don't want to try if and else statements 99 times. I know their is a more intuitive way of doing this problem. I don' want to miss the lesson I am suppose to learn.

    I have been at this problem all day today and still not having success. In your infinite wisdom can you help me without giving me the answer.

    Thanks again.

  11. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with an Array

    Work your problem into smaller parts. Figure out a way to solve for one of the items and get that code working.
    Then figure out a way to solve for each item.

  12. #11
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Help with an Array

    Quote Originally Posted by jps View Post
    Work your problem into smaller parts. Figure out a way to solve for one of the items and get that code working.
    Then figure out a way to solve for each item.
    This was great advice and I got the program working.

    @ Helloworld, I got the program to work perfectly and I feel great. My self esteem is very high at the moment. Okay, so I have been studying Java since the start of the summer and doing that homework from Javaranch was at the time out of my grasp. I could have mapped everything out, but I wanted to use "clever math" to do it aka Modulus. I bought a book called Java head first, and I studied it everyday. After doing problems in the book, and coming back to the page and rereading what you and JPS said it all clicked and bam the program worked. I felt like in my small world, I was on top of it. Just like the scene from Titanic. Thank you, Thank you, and Thank you.

    Now, I in my 30s and I studying for my A+ certification and I don't get the same type of Euphoria from it. But in today's economy, I have to make a living. Just like an artist working as waiter, and at night working on his or her skills. I want to be a programmer I have the IT degree from college, but we really didn't learn anything. So I am on this journey of self education. Can you help steer me in the right path of books and websites. I do intend on eventually getting my certification in Java and taking a programming classes in community college,but before I do I want to be very well versed in it. Thanks so much, again, Helloworld, you response to me was very kind and informative. I am so eagerly waiting your response.

    Thanks so much for taking the time to help me. At the time, I couldn't get the program to work without entirely mapping it out, but I bought Head Start in Java and studied a bit. Came back, and did the program with your advice and jps advice. Words can't explain on how good I felt getting the program to do excatly w

  13. #12
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with an Array

    I can't really recommend any good Java books in particular, but I'm sure there are some good ones out there (sounds like you found one already). I learned Java mostly through classes, the internet, and practicing. By far the best way to learn anything is to use it a lot, and Java is no exception.

    If you have the ability to, I would recommend taking a class even if you aren't well versed in Java yet. You could even consider "sitting in" on a class for free (going to class and listening but not getting for it), check with the professor to see if that's ok with them. The reason is the purpose of the class is to teach you something you didn't know before, not necessarily re-affirm what you've already learned (there's very few things more boring than sitting through a class in which you already know the material).

    I wish you the best of luck on your goals.

  14. #13
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with an Array

    @loul345
    Your story does not differ too far from my own. I wish you the best of luck. I will give you some links to some of the things I found useful so far in my journey of programming.

    I stumbled upon ROBOCODE some years back, but I was past the point of really learning anything from it, though it did prove to be entertaining and has been fun at times. Honestly I am not even sure if that is the exact one I found, but it seems like it is.

    Stanford has classes like CS106a set up where you can do them from the web complete with class materials and software to go with the class. There are many classes, the one linked here is more of an intro to programming class, but there are more advanced classes. There is no charge for the classes, but I think they make you register to download materials now. Maybe not though. Another benefit, in my opinion anyway, they are a good school and have quality instructors who know how to present material as well as the material they are presenting. The videos are decent or better quality, some of the old ones look like they were made as many years ago as they were made (lol).

    MyFavoriteTopic for Dummies books. Where MyFavoriteTopic is the current subject. I find those books for dummies to be a starting point for myself when I want to learn a new subject. I read that book as an outline of what I have decided to learn. Those books usually have a wide area of coverage on the topic, spawn thoughts to research, and many times have good links to check out (some of which are outdated and no longer exist, but some of the books I have are many years (10+) old, because some of them were worth more to me as a quick cheat-sheet and have found a home on my shelf.

  15. #14
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Help with an Array

    Thanks for helping me.

Similar Threads

  1. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 05:12 PM
  2. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 11:50 AM
  3. Replies: 2
    Last Post: May 13th, 2011, 04:08 AM
  4. Replies: 2
    Last Post: May 6th, 2011, 06:19 PM
  5. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 05:29 PM

Tags for this Thread