+ 1

Something is wrong with my loop

I tried making a code where the user is able to choose which numbers should be included in the loop but for some reason that code doesnt understand what I'm trying to do. https://code.sololearn.com/c126hCzRL0sB/?ref=app

21st Nov 2017, 3:10 AM
jalen
jalen - avatar
29 ответов
+ 5
It is working you have to input 3 times with a line space like this: loop 1 20 40
21st Nov 2017, 4:04 AM
Peerzada Burhan
Peerzada Burhan - avatar
+ 4
line 14 delete the ; at the end
21st Nov 2017, 3:15 AM
John Wells
John Wells - avatar
+ 3
I get output with: loop 1 5 20
21st Nov 2017, 3:28 AM
John Wells
John Wells - avatar
+ 2
I suspect the same thing.
21st Nov 2017, 1:59 PM
David Carroll
David Carroll - avatar
+ 1
@Jalen - It's working for me as well. What are you entering into the input dialog box when you run the code? You must enter 3 separate values, each on a separate line: ------------ loop 1 5 20 ------------
21st Nov 2017, 5:50 AM
David Carroll
David Carroll - avatar
+ 1
Actual only 'loop 1' must be on a line by itself because nextLine reads the entire line. nextInt stops when it hits whitespace and skips whitespace to find the start of a number so the two numbers can be on the same line or even: 5 20
21st Nov 2017, 6:02 AM
John Wells
John Wells - avatar
+ 1
@John... Good point. But, for some reason, when I attempted with my two integers on the second input line, I got "No Output" message. Not sure what was going on.
21st Nov 2017, 8:01 AM
David Carroll
David Carroll - avatar
+ 1
did u input them like this? loop 1 20
21st Nov 2017, 8:22 AM
jalen
jalen - avatar
+ 1
No... I originally entered this: ------------ loop 1 5 20 ------------
21st Nov 2017, 1:09 PM
David Carroll
David Carroll - avatar
+ 1
Most likely it had an extra space on first line to prevent match. I got 2 between it once I'm guessing, since it failed once for me. After which, I double checked and caught it once before hitting submit. Huge fingers, tiny keys and if you hover near the screen it is the same as touching. I get double key presses occasionally, when I think too much and type too slow.
21st Nov 2017, 1:24 PM
John Wells
John Wells - avatar
+ 1
If you want them forced to be on the same line, you are not going to like the programming. If you don't care about spacing and you wish to force two words, use two next calls each getting a word. If the second word is optional, call hasNextInt after the first word. If it is true, there is a number next so no second word. If it false, call next to get the second word.
22nd Nov 2017, 12:04 PM
John Wells
John Wells - avatar
0
I fixed line 14 but it says theres no output
21st Nov 2017, 3:27 AM
jalen
jalen - avatar
0
I'm entering this: loop 1 1 20
21st Nov 2017, 6:00 AM
jalen
jalen - avatar
0
You have 'loop 1 ' which fails your case test.
21st Nov 2017, 6:07 AM
John Wells
John Wells - avatar
0
In the future, add debugging. If you added, default: cout<<"'"<<type<<"'"<<endl; // two " around a ' break; You would have seen your issue immediately.
21st Nov 2017, 6:11 AM
John Wells
John Wells - avatar
0
import java.util.Scanner; import java.util.Random; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); String type = input.next(); switch(type) { case "loop": int a = input.nextInt(); int b = input.nextInt(); for(int x=a; x<=b; x++) System.out.println(x); } } } try this input should in single line o changed "loop 1" to "loop" the line break is issue. eg input:loop 1 20
21st Nov 2017, 6:50 AM
ASHMIL HUSSAIN
ASHMIL HUSSAIN - avatar
0
thanks I fixed the problem by changing nextLine() to next() and got it to work.
21st Nov 2017, 7:38 AM
jalen
jalen - avatar
0
if we solved the previous problem is there a way to write multiple words for the first line with multiple inputs? for example: first loop 1 20
22nd Nov 2017, 9:24 AM
jalen
jalen - avatar
0
so if I wanted the code to do what I said previously I need to add a second next(); ?
22nd Nov 2017, 4:51 PM
jalen
jalen - avatar
0
Yes, as long as all commands require two words. I'm assuming you plan on making a second command. Note: first loop first loop first loop are all the same using next because whitespace (spaces, tabs, and new lines) are skipped.
22nd Nov 2017, 4:56 PM
John Wells
John Wells - avatar