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