0

How to write this code properly

While (input.hasNextDouble() || ! (input.hasNext() ) || (input.nextDouble()!=0)) { Listofstuff.add(input.nextDouble()); } It reads as while input has doubles or doesn't have string or doesn't have double =0 keep the loop going on But the last part of the whole condition i.e (input.nextDouble()!=0)) Isn't working.. I don't know the right format to make this work.

30th Mar 2019, 8:17 PM
kuku Debbarma
kuku Debbarma - avatar
3 Respostas
+ 12
Can you give a link to your code and/or tell more precisely what are you trying to accomplish? You are using not exclusive OR operator, so the loop will run as long as at least one of the conditions evaluates to "true" (hasNextDouble is true OR hasNext (token) is false OR nextDouble is not equal to 0).
30th Mar 2019, 9:05 PM
1Lory☕
1Lory☕ - avatar
+ 5
Do you mean something like this? take the double value --> if statement: if(input !=0){ add value to list } To make sure that you have only double values I would use try/catch (exception handling) https://code.sololearn.com/cvTpWWd7Z7Hg/?ref=app in the catch part you can decide what to do: end program, print an error message ... https://www.sololearn.com/learn/Java/2175/?ref=app
30th Mar 2019, 9:36 PM
Denise Roßberg
Denise Roßberg - avatar
+ 4
kuku Debbarma It is better to add the link to the code (in the codeplayground, works also with private codes) so we can run the code and see which error message you get. I think you get memory limit exceed --> your loop runs infinity hasNext() --> is always true if you input something. It takes the input until a white space. In my code (see my other post) I used a if statement. For example: while(input.hasNextDouble && input.nextDouble() != 0){ --> input = 0 --> stops the while loop (while condition = false) while(input.hasNextDouble()){ if(input != 0){ add input to list --> if input = 0, ignore input, jump to the next value While(input.hasNextDouble || input.nextDouble() != 0){ --> or: one of both condition have to be true, that the whole condition is true --> input = 0: hasNextDouble() returns true; nextDouble != 0 returns false --> true || false = true. https://www.sololearn.com/learn/Java/2144/
30th Mar 2019, 10:31 PM
Denise Roßberg
Denise Roßberg - avatar