+ 1

Why i am getting this error

What is missing?? package com.helloworld; import java.util.Scanner; public class main { public static void main(String[] args) { System.out.println("Please input the string:"); Scanner b = new Scanner(System.in); System.out.println("Please input the number of times to repeat the string"); Scanner a = new Scanner(System.in); for(int i=a; i>0; i--) { System.out.println(b.nextLine()); } } }

14th Jan 2018, 9:40 AM
Siddharth Mishra
Siddharth Mishra - avatar
8 ответов
+ 4
Got it! package com.helloworld; import java.lang.String; import java.util.Scanner; public class main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please input the string:"); String string = scanner.nextLine(); System.out.println("Please input the number of times to repeat the string"); int integer = scanner.nextInt(); for(int i=integer; i>0; i--) { System.out.println(string); } } }
14th Jan 2018, 10:00 AM
Siddharth Mishra
Siddharth Mishra - avatar
+ 2
you didnt take any strings from user,you just created the object of scanner. take that and put in a string variable then try your code
14th Jan 2018, 9:48 AM
Mohammad Khani
Mohammad Khani - avatar
+ 2
Try to convert the variable a into an integer first before you use it in the loop.
14th Jan 2018, 9:51 AM
Jente
+ 2
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner a =new Scanner(System.in) ; System.out.println ("enter your string"); String s=a.next() ; System.out.println("your string is: "+s); System.out.println ("how many repeats you want?"); int b=a.nextInt(); System.out.println (b); for(int i=0;i<b;i++){ System.out.println(s); } } } 😑 I WAS WRITING THIS 😂😢
14th Jan 2018, 10:08 AM
Mohammad Khani
Mohammad Khani - avatar
+ 1
a.nextInt() in for loop maybe?
14th Jan 2018, 9:59 AM
BlazingMagpie
BlazingMagpie - avatar
+ 1
blazingmagpie i think in that case,user has to enter the string for many timez
14th Jan 2018, 10:00 AM
Mohammad Khani
Mohammad Khani - avatar
+ 1
did it work?
14th Jan 2018, 10:01 AM
Mohammad Khani
Mohammad Khani - avatar
+ 1
It worked, i got the soln.
14th Jan 2018, 10:01 AM
Siddharth Mishra
Siddharth Mishra - avatar