Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By penguinCoder

Thread: problems with logical and

  1. #1
    Iltaqi is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default problems with logical and

    I am working on an assignment for my high school java class and i want to make sure that the user enters a y or n to determine their marital status however i get stuck in the loop even when i enter a y or n





    Java Code:
    import java.util.Scanner;
    
    public class Taxes {
    	static Scanner scan = new Scanner(System.in);
            static String store;
    	
    	
    	public static void main(String[] args) {
    		System.out.println("are you married?(y/n)");
    		store = scan.nextLine();
    		
    		
    		
    		for( ; store != "y" && store!= "n"; ){	
    		
    			System.out.println("please respond with a y for yes and n for no");
    			System.out.println("are you married?(y/n)");
    			store = scan.nextLine();
                     }

  2. #2
    Keith Jackson is offline Member
    Join Date
    Sep 2012
    Location
    Maryland, USA
    Posts
    14
    Rep Power
    0

    Default Re: problems with logical and

    I can't remember ever having seen a for loop used in this way. While it may work, there is probably another type of loop that would work better and be easier to understand. Also, a logical AND is not what you want to use in this case.

  3. #3
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default Re: problems with logical and

    I think using a while loop is a better option for what you want to do. Also to compare strings you need to probably do something like !(store.equals("y")) && !(store.equals("n"). Or try changing your variable to a char variable for the "y" and "n".
    Sincerely, Joshua Green
    Please REP if I help :)

  4. #4
    Iltaqi is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Re: problems with logical and

    Quote Originally Posted by joshdgreen View Post
    I think using a while loop is a better option for what you want to do. Also to compare strings you need to probably do something like !(store.equals("y")) && !(store.equals("n"). Or try changing your variable to a char variable for the "y" and "n".
    Thanks for helping me changing it to store.equals and it works

  5. #5
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: problems with logical and

    If you are just using 'y' and 'n', why not just use the type 'char'? It seems like you are wasting a byte of memory for nothing..
    joshdgreen likes this.

Similar Threads

  1. Logical Operators
    By Everyman in forum New To Java
    Replies: 9
    Last Post: 03-26-2012, 04:56 PM
  2. >>> and >> Logical operators
    By Playagood in forum New To Java
    Replies: 3
    Last Post: 02-19-2012, 05:07 AM
  3. Some Logical problem
    By MuslimCoder in forum New To Java
    Replies: 4
    Last Post: 03-01-2010, 09:12 AM
  4. Logical Gates
    By lingz89 in forum New To Java
    Replies: 1
    Last Post: 08-17-2009, 02:11 AM

Tags for this Thread

Posting Permissions

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