Computer Science Canada Arrays |
Author: | slider203 [ Sun Apr 11, 2010 7:46 am ] |
Post subject: | Arrays |
Hello I have to make a program that takes the name of a day from the user, then output the number of letters in the name (e.g. Monday 6 letters) I must use to arrays to do this one with the day names and the second with the number of letters. How would I find the number of letters...I know I could use the .length() command but that does not make use of the second array. [syntax=""] String[] days_Week = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; int[] num_Letters = {6,6,7,9,8,6,8}; System.out.println ("Please enter a day of the week"); System.out.println ("Enter "+exit+" to end."); day = br.readLine(); [/syntax] Thats what I have so far. |
Author: | TheGuardian001 [ Sun Apr 11, 2010 9:09 am ] | ||
Post subject: | Re: Arrays | ||
For future reference, to get Java code boxes, the syntax is
On to your problem though. Assuming that the second array is required (If it's not, jut get rid of it and use length), One possible solution would be to check what they entered against each value stored in "days_Week", then output "num_Letters" with the index of whatever check matched. So use a for loop to check each value in days_Week, then output num_letters[counter of for loop] when you get a match. |
Author: | slider203 [ Sun Apr 11, 2010 9:17 am ] | ||||
Post subject: | Re: Arrays | ||||
TheGuardian001 @ Sun Apr 11, 2010 9:09 am wrote: For future reference, to get Java code boxes, the syntax is
On to your problem though. Assuming that the second array is required (If it's not, jut get rid of it and use length), One possible solution would be to check what they entered against each value stored in "days_Week", then output "num_Letters" with the index of whatever check matched. So use a for loop to check each value in days_Week, then output num_letters[counter of for loop] when you get a match. First thanks for the tip how to post the code. Second ok I added a few loops and selections to the code so it checks the input but I didnt understand what you meant about "against each value stored in "days_Week", then output "num_Letters" with the index of whatever check matched." How can I do this? Here is what I did"
|
Author: | TheGuardian001 [ Sun Apr 11, 2010 9:27 am ] | ||
Post subject: | Re: Arrays | ||
Well, right in here:
You have: 1)a bit of code telling you that you found the right day. 2)a variable (count) telling you where in the array the day they entered was. So if you have another array that matches up with the values of days_Week (like, for example, num_Letters), then whenever your program finds the day they entered (inside that if), you can just output num_Letters[count] and stop your for loop. |
Author: | slider203 [ Sun Apr 11, 2010 9:43 am ] |
Post subject: | RE:Arrays |
Thankyou TheGuardian001 I really appricate the help! This worked for me and now I have a working program! |