Arrays
Author |
Message |
slider203
|
Posted: 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. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TheGuardian001
|
Posted: Sun Apr 11, 2010 9:09 am Post subject: Re: Arrays |
|
|
For future reference, to get Java code boxes, the syntax is
code: |
[code="java"]
[/code]
|
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. |
|
|
|
|
|
slider203
|
Posted: 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
code: |
[code="java"]
[/code]
|
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"
code: |
[code="java"]
String[] days_Week = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
int[] name_Letters = {6,6,7,9,8,6,8};
int length = 0;
String day = "";
final int MAX_COUNT = 7;
final String exit = "exit";
boolean dayCorrect = false;
do
{
System.out.println ("Please enter a day of the week");
System.out.println ("Enter "+exit+" to end.");
day = br.readLine();
if (!day.equalsIgnoreCase (exit))
{
for (int count = 0; count < MAX_COUNT; count++)
{
if (day.equalsIgnoreCase (days_Week[count]))
{
// This is where I want to put the number of leters
System.out.println ("There are " + length + " letters in " + day + ".");
dayCorrect = true;
}
}
if (!dayCorrect)
{
System.out.println ("Error, input is not a day of the week.");
System.out.println ("Please try again.");
System.out.println (" ");
dayCorrect = false;
}
}
}
while (!day.equalsIgnoreCase (exit));
}
}
[/code]
|
|
|
|
|
|
|
TheGuardian001
|
Posted: Sun Apr 11, 2010 9:27 am Post subject: Re: Arrays |
|
|
Well, right in here:
Java: |
for (int count = 0; count < MAX_COUNT; count++ )
{
if (day. equalsIgnoreCase (days_Week [count ]))
{
// This is where I want to put the number of leters
System. out. println ("There are " + length + " letters in " + day + ".");
dayCorrect = true;
}
}
|
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. |
|
|
|
|
|
slider203
|
Posted: 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! |
|
|
|
|
|
|
|