+ 1

What is wroNG here. Is it the Order in which the Code is plAced.

import java.util.Scanner; public class Main { static void welcome() { for (int i = 0; i < num; i++) {System.out.println ("Welcome!");}} public static void main(String[] args) { Scanner read = new Scanner(System.in); int num = read.nextInt(); welcome();} } OUTPUT: CANNOT FIND SYMBOL NUM.

26th May 2022, 7:32 AM
We Doru
We Doru - avatar
8 Answers
+ 2
There is no num defined in the method welcome, neither statically in the class Main. No num, no loop. Because how long are you going to iterate if the upper limit has not been defined? The num in method main is local to that method. The method welcome does not see num in main. You can give num to welcome though: static void welcome(int num) { ... } And then call in main: int num = read.nextInt(); welcome(num); And also pay attention to your formatting. A neat structure is a pleasure to look at and read. For yiurself and others :)
26th May 2022, 7:56 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
0
But in int num=read.nextInt(), num is Already there.
26th May 2022, 7:59 AM
We Doru
We Doru - avatar
0
Please reread the amendments to my previous answer :)
26th May 2022, 8:00 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
0
The Upper Limit Should be Covered By the Input ----- int num=read.nextInt(). Is it
26th May 2022, 8:02 AM
We Doru
We Doru - avatar
0
Yes, but it is not visible in welcome. You need to pass it as argument, or make it accessible via the class (static variable).
26th May 2022, 8:06 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
0
import java.util.Scanner; public class Main { static void welcome(int num) { for (int i = 0; i < num; i++) {System.out.println ("Welcome!");}} public static void main(String[] args) { Scanner read = new Scanner(System.in); int num = read.nextInt(); welcome(num);} } OUTPUT: Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at Main.main(Main.java:10)
26th May 2022, 8:07 AM
We Doru
We Doru - avatar
0
Are you sure you entered a number in the input box?
26th May 2022, 8:10 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
0
It is fine, the problem wAs I used Another editor which shows no space for Input(And not SololeArnN) . When copy paste to Solo learn, I Had the input blank Space.
26th May 2022, 8:35 AM
We Doru
We Doru - avatar