Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: I'm tearing my hair out, I need help

  1. #1
    Junior Member
    Join Date
    Sep 2020
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default I'm tearing my hair out, I need help

     
    Scanner input = new Scanner(System.in);
     
    		System.out.print("Is your pet a dog or a cat?: ");
    		String customerInput = input.next();
     
    		System.out.print(customerInput);
     
    		if (customerInput != "dog" && customerInput != "cat"){
    			System.out.println();
    			System.out.println("Incorrect input please enter again!\n\n");
    			System.out.print("Is your pet a dog or a cat?: ");
    			customerInput = input.next();
    		}
    		else {
    			System.out.print(customerInput);
    		}
    		input.close();

    why is dog or cat triggering the first if statement? Makes no sense. I even took out the && and other condition and dog still triggers it. I even printed it out to screen and it's dog. What am I missing here? I'm trying to do a project for school and several lines of my code are being skipped and I can only assume it's the scanner method.

    --- Update ---

    Wow so apparently the operators are shit in java when it comes to strings because the only one that works is the String method of equals.
    Last edited by brazen.aden; September 20th, 2020 at 09:26 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I'm tearing my hair out, I need help

    You need to use methods when comparing the contents of objects. The arithmetic operators are for primitives.
    The logical operators work with boolean values.
    Take a look at the tutorial: https://docs.oracle.com/javase/tutor...operators.html

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    brazen.aden (September 20th, 2020)

  4. #3
    Junior Member
    Join Date
    Sep 2020
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm tearing my hair out, I need help

    Yeah I figured that out after doing some test elimination. I suppose that is what I get for expecting one language to be similar to another. I'm actually taking a java class, but my experience is in c# and python. I've never encountered this kind of problem. I appreciate the help. I also found out that scanner doesn't work when put into methods, so you have to make the method accept a scanner object as a parameter then work through it that way. Apparently the scanner gets lost when you open and close it and try to open it again. It also probably has something to do with scope.

    --- Update ---

    In case anyone needed a solution to this problem when using console input here it is.\

    //VALIDATE dogOrCat()
    	public static String dogOrCat(Scanner input) {	 
    		System.out.print("Is your pet a dog or a cat?: ");
    		String customerInput = input.next().toLowerCase();
     
    		while (!customerInput.equals("dog") && !customerInput.equals("cat")){
    			System.out.println();
    			System.out.println("Incorrect input please enter again!\n\n");
    			System.out.print("Is your pet a dog or a cat?: ");
    			customerInput = input.next().toLowerCase();
    		}
     
    		return customerInput;
    	}

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I'm tearing my hair out, I need help

    Apparently the scanner gets lost when you open and close it and try to open it again
    Are you talking about when Scanner uses System.in? When System.in is closed, it stays closed.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    brazen.aden (September 20th, 2020)

  7. #5
    Junior Member
    Join Date
    Sep 2020
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm tearing my hair out, I need help

    Yes. I was creating new objects of scanner. I was closing it at the end of each method. I was getting a warning to close it, something about potential leaks from the IDE. I thought it just closed the object.

Similar Threads

  1. GUI JAR Image 'tearing' in Windows
    By croe in forum AWT / Java Swing
    Replies: 6
    Last Post: July 3rd, 2010, 07:06 PM