Results 1 to 9 of 9
Thread: why string is not showing ?
- 02-10-2017, 02:01 PM #1
Senior Member
- Join Date
- Dec 2016
- Posts
- 103
- Rep Power
- 0
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 ");
"Error : cannot find symbol"
- 02-10-2017, 02:23 PM #2
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
- 02-10-2017, 08:04 PM #3
Senior Member
- Join Date
- Dec 2016
- Posts
- 103
- Rep Power
- 0
- 02-10-2017, 08:09 PM #4
Senior Member
- Join Date
- Dec 2016
- Posts
- 103
- Rep Power
- 0
Re: why string is not showing ?
it print "wrong choice" too why...
- 02-10-2017, 08:51 PM #5
Re: why string is not showing ?
it print "wrong choice" too why...If you don't understand my response, don't ignore it, ask a question.
- 02-10-2017, 11:13 PM #6
Member
- Join Date
- Jan 2017
- Posts
- 46
- Rep Power
- 0
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
- 02-10-2017, 11:38 PM #7
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: why string is not showing ?
And don't do this:
Java Code:else System.out.println("Wrong Choice ");
Java Code:else { System.out.println("Wrong Choice "); }
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 02-11-2017, 05:58 AM #8
Senior Member
- Join Date
- Dec 2016
- Posts
- 103
- Rep Power
- 0
- 02-11-2017, 11:24 AM #9
Member
- Join Date
- Jan 2017
- Posts
- 46
- Rep Power
- 0
Similar Threads
-
Adding String to ArrayList from textfield and showing it on ComboBox
By Renxx in forum AWT / SwingReplies: 1Last Post: 12-11-2011, 12:59 PM -
Showing html in JTextPane directly from a String
By ni4ni in forum AWT / SwingReplies: 1Last Post: 04-11-2011, 01:03 PM -
String output showing "null"
By hayden06f4i in forum New To JavaReplies: 6Last Post: 11-05-2010, 11:21 AM -
Problem Showing A Big String?
By AJArmstron@aol.com in forum New To JavaReplies: 1Last Post: 05-05-2010, 03:56 AM -
Why isn't this showing?
By JToolTip in forum Java AppletsReplies: 2Last Post: 07-08-2007, 12:54 AM
Bookmarks