Results 1 to 3 of 3
  1. #1
    ikat is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default 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 :

    Java 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.");
        }
    }
    Last edited by ikat; 11-20-2011 at 04:49 PM.

  2. #2
    zirbinator is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default 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:

    Java 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.

  3. #3
    ikat is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default 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 .

    Java 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
    Last edited by ikat; 11-20-2011 at 09:37 PM.

Similar Threads

  1. Question for Java Boolean
    By jayfaxva in forum New To Java
    Replies: 0
    Last Post: 11-18-2011, 11:27 PM
  2. Boolean array Question
    By elm101 in forum New To Java
    Replies: 10
    Last Post: 06-01-2011, 07:02 AM
  3. convert boolean to string
    By newbiejava in forum New To Java
    Replies: 3
    Last Post: 07-22-2010, 03:32 PM
  4. Limiting loop in boolean string
    By jimmy-lin in forum New To Java
    Replies: 7
    Last Post: 10-10-2009, 06:31 AM
  5. boolean to string
    By otoro_java in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 05:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •