Results 1 to 9 of 9
  1. #1
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default why string is not showing ?

    Java Code:
    if(choice==1)
    	  {		  
                 System.out.println("Enter Client Name : ");
                 Scanner cn=new Scanner(System.in);
                 String client=cn.nextLine();
    	  }
                 else
                    System.out.println("Wrong Choice ");				 
      
           if(choice==2)
    	  {		  
                 System.out.println(client);
    	  }
      
             else
                    System.out.println("Wrong Choice ");
    why it is not print "client"

    "Error : cannot find symbol"

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: why string is not showing ?

    Variable client is defined in the block of if(choice ==1){...}. In other words, that's the scope. A variable can't be seen outside the scope. So you should define it one block higher, somewhere above the first if.
    Java Code:
    String client = null;
    if(choice==1) {       
        System.out.println("Enter Client Name : ");
        Scanner cn=new Scanner(System.in);
        client=cn.nextLine();
    }
    else {
        System.out.println("Wrong Choice ");                 
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Re: why string is not showing ?

    Quote Originally Posted by SurfMan View Post
    Variable client is defined in the block of if(choice ==1){...}. In other words, that's the scope. A variable can't be seen outside the scope. So you should define it one block higher, somewhere above the first if.
    Java Code:
    String client = null;
    if(choice==1) {       
        System.out.println("Enter Client Name : ");
        Scanner cn=new Scanner(System.in);
        client=cn.nextLine();
    }
    else {
        System.out.println("Wrong Choice ");                 
    }
    but when i use string befor if statement ...

    result showing is wrong choice even i choice correct and enter client name it cannot print client name....

  4. #4
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Re: why string is not showing ?

    it print "wrong choice" too why...

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: why string is not showing ?

    it print "wrong choice" too why...
    What is the value in choice when it prints that message? Add it to the println statement to see.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default Re: why string is not showing ?

    You need to read up on the difference between stringing if statements and the use of if..else. That's the reason why 'wrong choice' is shown even when the choice is 1

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: why string is not showing ?

    And don't do this:

    Java Code:
    else 
      System.out.println("Wrong Choice ");
    Do this - always!
    Java Code:
    else {
          System.out.println("Wrong Choice "); 
     }
    For any construct that requires {}, always use them, even for single lines.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Re: why string is not showing ?

    Quote Originally Posted by Norm View Post
    What is the value in choice when it prints that message? Add it to the println statement to see.
    i have two option in choices

    1 >>Enter User
    2 >>Check list

    when user choose 1
    my if code run and it asked "Enter client name"

    den i want after enter the name name will show ....

    but wrong choice also prints along with the name...

  9. #9
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default Re: why string is not showing ?

    I've already given you a big hint as to why that happens. You need to study the use of if..else

Similar Threads

  1. Replies: 1
    Last Post: 12-11-2011, 12:59 PM
  2. Showing html in JTextPane directly from a String
    By ni4ni in forum AWT / Swing
    Replies: 1
    Last Post: 04-11-2011, 01:03 PM
  3. String output showing "null"
    By hayden06f4i in forum New To Java
    Replies: 6
    Last Post: 11-05-2010, 11:21 AM
  4. Problem Showing A Big String?
    By AJArmstron@aol.com in forum New To Java
    Replies: 1
    Last Post: 05-05-2010, 03:56 AM
  5. Why isn't this showing?
    By JToolTip in forum Java Applets
    Replies: 2
    Last Post: 07-08-2007, 12:54 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
  •