question about boolean when compareing String to arrayString
I would like to use a boolean to compare a String that is input by user to a String array. If I write my code this way I get the check (true/false) and the answers are printed 7 time (the number of items in the array). Can I some how limit the number of times the answer is printed.
public void getWeekDay(String anyWeekDay)
{
boolean anyDay;
for(int i=0;i<weekDays.length;i++)
if(anyWeekDay.equalsIgnoreCase(weekDays[i]))
System.out.println("The day you selected was " +anyWeekDay);
else
System.out.println("Please enter a day of the week. "+anyWeekDay +" is not a day of the week!");
I also tried the code this way it limited the answer to one line but I can not get the else (or false part of the statement to print)
any help or explanation of why would be greatly appriciated.
public void getWeekDay(String anyWeekDay)
{
boolean anyDay;
for(int i=0;i<weekDays.length;i++)
if(anyDay=(anyWeekDay.equalsIgnoreCase(weekDays[i])))
if(anyDay=true)
System.out.println("The day you selected was " +anyWeekDay);
else
if(anyDay=false)
System.out.println("Please enter a day of the week. "+anyWeekDay +" is not a day of the week!");
//I am not able to get the false part of this statement to print. :(
}
thank you.
ikat
incase your need the entire code to answer my question :
Code:
public class WeekDays
{
// fields
private String[] weekDays;
/**
* Constructor for objects of class WeekDays
*/
public WeekDays()
{
weekDays= new String [7];
weekDays[0]="Monday";
weekDays[1]="Tuesday";
weekDays[2]="Wednesday";
weekDays[3]="Thursday";
weekDays[4]="Friday";
weekDays[5]="Saturday";
weekDays[6]="Sunday";
printIndexValue();
}
/**
* A method to allow the caller to retrieve any day;
* using a boolean true statement.
*/
public void getWeekDay(String anyWeekDay)
{
boolean anyDay;
for(int i=0;i<weekDays.length;i++)
if(anyWeekDay.equalsIgnoreCase(weekDays[i]))
// if(anyDay=true)
System.out.println("The day you selected was " +anyWeekDay);
//printLength();
else
//if(anyDay=false)
System.out.println("Please enter a day of the week. "+anyWeekDay +" is not a day of the week!");
// printIndexValue();
//I am not able to get the false part of this statement to work. :(
}
/**
* a method to print the days of the week
*/
public void printIndexValue()
{
System.out.println("Please select from one of the days of the week.");
for(int index=0;index < weekDays.length; index++){
System.out.println(weekDays[index]);
}
}
/**
* method to print the length of the array
*/
public void printLength()
{
System.out.println("There are "+ weekDays.length+ " days in a week.");
}
}
Re: question about boolean when compareing String to arrayString
I'm not sure exactly what you're trying to do, but I think you just want it to check if the input (you called it "anyWeekDay") was included in the array. The way you coded it in the complete code, every time that it runs through the for loop, it prints whether or not the input matches a weekday. I think you just want this printed once: if it matches one of the entries in the array then print that, otherwise tell the user to enter a day of the week. The code would look like this:
Code:
public static String[] weekdays = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
public static void getWeekday(String input)
{
String weekday = null;
for (int i = 0; i < weekdays.length; i++)
{
if (input.equalsIgnoreCase(weekdays[i]))
{
weekday = weekdays[i];
System.out.println("The day you selected was " + weekday + ".");
break;
}
}
if (weekday == null)
{
System.out.println("Please enter a day of the week. " + input + " is not a day of the week!");
}
}
With the commented-out code, it looks like you are trying to do something like what I did here, but it's not quite working. For one thing, you never initialized the boolean anyDay, so referencing it will not work. Some clarification about your exact purpose would be nice if the code above isn't what you want.
Re: question about boolean when compareing String to arrayString
zirbinator,
Thank you, for responding. I have been trying to figure this one out with no success at some point I did initialize the boolean. But it still did not print my secondstatement properly. I had not thought of using a break. I had an additional question if i wanted to find the index from the array that matched the input would it be possible to save this information for later use?
I figured this out by adding a line to your code .
Code:
public class WeekDays
{
// fields
private String[] weekDays;
private int userIndex;
/**
* Constructor for objects of class WeekDays
*/
public WeekDays()
{
userIndex=0;
weekDays= new String [7];
weekDays[0]="Monday";
weekDays[1]="Tuesday";
weekDays[2]="Wednesday";
weekDays[3]="Thursday";
weekDays[4]="Friday";
weekDays[5]="Saturday";
weekDays[6]="Sunday";
printIndexValue();
}
/**
* A method to allow the caller to retrieve any day;
* using a boolean true statement.
*/
/** public void getWeekDay(String anyWeekDay)
{
boolean anyDay;
for(int i=0;i<weekDays.length;i++)
if(anyDay=true(anyWeekDay.equalsIgnoreCase(weekDays[i])))
// if(anyDay=true)
System.out.println("The day you selected was " +anyWeekDay);
//printLength();
else
//if(anyDay=false)
System.out.println("Please enter a day of the week. "+anyWeekDay +" is not a day of the week!");
// printIndexValue();
//I am not able to get the false part of this statement to work. :(
}*/
public void getWeekday(String input)
{
String weekday = null;
for(int i=0;i<weekDays.length;i++)
{
if (input.equalsIgnoreCase(weekDays[i]))
{
userIndex=(i+1);
weekday = weekDays[i];
System.out.println("The day you selected was " + weekday + ".");
break;
}
}
if (weekday == null)
{
System.out.println("Please enter a day of the week. " + input + " is not a day of the week!");
}
}
/**
* a method to print the days of the week
*/
public void printIndexValue()
{
System.out.println("Please select from one of the days of the week.");
for(int index=0;index < weekDays.length; index++){
System.out.println(weekDays[index]);
}
}
/**
* method to print the length of the array
*/
public void printLength()
{
System.out.println("There are "+ weekDays.length+ " days in a week.");
System.out.println("The user selected day number "+ userIndex+ ".");
}
}
Thank you again for your help, this really was driving me crazy.
kat