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 3 of 3

Thread: Scanner and if

  1. #1
    Junior Member
    Join Date
    Jan 2023
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Scanner and if

    Hi All,
    i build a superclass called Shoes, and some subclasses. One of them is "askerayakkabi" .
    what i want to make is, i use scanner system.in and making user to enter askerayakkabi if they enter after they have to enter size and color. if they choose the askerayakkabi. but the problem is first scanner method for ' String ayakkabituru = cesit.nextLine();' is working. and i can enter the askerayakkabi... after wrote to askerayakkabi in the consule , i would like to if is working and request more data like size of shoes.. color of shoes.. but i didnt get it why that if is not working..thank you in advance
    package denemeleredo;
     
    import java.util.Scanner;
     
    public class Ayakkabisecimi {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		Scanner cesit = new Scanner(System.in);
    		System.out.println("Lutfen istediginiz ayakkabiyi giriniz. askerayakkabi,Babet,Iskarpin");
    		String ayakkabituru = cesit.nextLine();
    		System.out.println(ayakkabituru);
     
    		if (ayakkabituru == "askerayakkabi") {
    		    askerayakkabi secilen = new askerayakkabi();
    		    System.out.println("Lutfen istediginiz rengi yaziniz.");
    		    String taleprenk = cesit.nextLine();
    			System.out.println("Lutfen talep edilen olcuyu giriniz");
    			int talepolcu = cesit.nextInt();
    			System.out.println("Talep ettiginiz ayakkabi " + ayakkabituru + " istediginiz renk "+ taleprenk + " ve olcusu " + talepolcu);
    		}
     
     
     
     
    	}
     
    }
    Last edited by erenbayir; January 21st, 2023 at 07:07 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: Scanner and if

    One problem I see is using == to compare String objects. You need to use the equals method to compare String objects.

    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:

    erenbayir (January 21st, 2023)

  4. #3
    Junior Member
    Join Date
    Feb 2023
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scanner and if

    The error is in the line

    if (ayakkabituru == "askerayakkabi") {

    This line uses the == operator to compare two strings. The == operator compares the references to the objects, not the content of the objects. To compare the content of two strings, you should use the .equals() method.

    So, the corrected code would be:

    if (ayakkabituru.equals("askerayakkabi")) {

Similar Threads

  1. Scanner
    By chunkymonkey in forum Java Theory & Questions
    Replies: 9
    Last Post: September 29th, 2018, 07:47 PM
  2. Scanner Class
    By showdawg in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2017, 09:16 AM
  3. scanner.nextLine()
    By R0bsterz in forum Java Theory & Questions
    Replies: 13
    Last Post: April 25th, 2013, 06:26 AM
  4. Scanner help
    By sp11k3t3ht3rd in forum Java Theory & Questions
    Replies: 3
    Last Post: June 22nd, 2011, 11:55 AM
  5. Help With Scanner
    By jtphenom in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2009, 08:49 PM