+ 6

Error

I can't understand why last line is unreachable https://code.sololearn.com/c6onDSja2429/?ref=app

27th Nov 2018, 5:24 PM
mn121
mn121 - avatar
9 odpowiedzi
+ 11
● the condition in while() loop is always true , so the loop will never over , so print statement will never be reached .
27th Nov 2018, 6:12 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 7
Exit condition from the loop is missing. Now it's always true so it always executes the loop. Tl dr: Infinite loop
27th Nov 2018, 11:48 PM
Haris
Haris - avatar
+ 2
Thanks to all of you 😊😊😊
28th Nov 2018, 12:18 AM
mn121
mn121 - avatar
+ 2
Linda the program doesn't give the desired output.. When I enter "we are learning java" It gives output we... And Bennett Post I can't use your program as I have not learn array in school till now ..
28th Nov 2018, 10:50 AM
mn121
mn121 - avatar
+ 2
import java.util.*; public class Main { public static void main(String[] args) { System.out.println("enter a line="); Scanner c = new Scanner(System.in); String s=c.nextLine(); Scanner c2=new Scanner(s); String w,lg=""; while(c2.hasNext()) { w=c2.next(); if(w.length()>lg.length()) lg=w; } System.out.println(lg+""+lg.length()); } } I found this program on internet but I cannot understand what is the use of the two extra line , I mean what is there need these two lines: " String s=c.nextLine(); Scanner c2=new Scanner(s); "
28th Nov 2018, 10:51 AM
mn121
mn121 - avatar
+ 2
Thanks to all of you 😊😊😊 Here's my updated program... https://code.sololearn.com/c1TYv7uGUQxA/?ref=app 😄😄😄
28th Nov 2018, 1:10 PM
mn121
mn121 - avatar
+ 1
But I don't know how to use break there ... Can anyone give me a program written...😅😅😅
28th Nov 2018, 1:15 AM
mn121
mn121 - avatar
0
This code is edited with adding break and perfectly run https://code.sololearn.com/cUj0oAR06Yy6/?ref=app
28th Nov 2018, 3:40 AM
Linda
Linda - avatar
0
U must add a break statement to get out of the infinite while loop
28th Nov 2018, 4:03 AM
Arunesh Sarker
Arunesh Sarker - avatar