简体   繁体   中英

Unable to complete a simple function using scanner class

I am trying to solve a question on the online platform of Infosys Ltd InfyTQ platform. The problem is in java. The problem description is as follows:

Implement a program to display the geometric sequence as given below for a given value n, where n is the number of elements in the sequence.

-------> 1, 2, 4, 8, 16, 32, 64, ______, 1024

Sample Input and Output

-------> 5:::: 1, 2, 4, 8, 16 -------> 8:::: 1, 2, 4, 8, 16, 32, 64, 128

What I am given is the following:

class Tester {
    public static void main(String[] args) {
        // Implement your code here 
    }
}

What I am trying to implement is the following

import java.util.Scanner;

class Tester {
    public static void main(String[] args) {
        // Implement your code here
        int sequence=1;
        
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the length of sequence: ");      

        // input the lenght of the sequence
        int n = sc.nextInt();
        while(n!=0)
        {
           System.out.print(sequence);
           System.out.print(" ");
           sequence*=2;
           n-=1;
        }
    }
}

I have tried the above code in an online compiler and on my system on the Visual Studio Code. While in the online compiler it's working flawlessly. On my system it's not giving any errors. The errors while compiling on their platform are as follows:

Enter the length of sequence: Runtime Exception

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 Tester.main(myCode.java:12)

I am unable to modify anything on the online platform that I am working on. So nothing deep can be done. Please help solve the above problem so that it's accepted by the platform, so that I may proceed further.

The errors while compiling on their platform are as given above.

The logic is flawless. The only problem is with their system I think. But maybe I am going wrong somewhere here. Maybe the SC is leaking. I don't know for sure. The errors are the only ones that are given above. Please help in the regard.

It seems like standard input is not available on this platform... a thing I would try is using next() and Integer.parseInt() but I'm almost 100% sure this wouldn't help. Since the exception involved is NoSuchElementException, I'm pretty sure it's also useless to use try{} and catch (java.util.NoSuchElementException e) {}... it really seems like standard input is not enabled. Still, give this a try:

import java.util.Scanner;
class Tester {
    public static void main(String[] args) {
        // Implement your code here
        int sequence=1;
        
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the length of sequence: ");      

        // input the lenght of the sequence
        int n = 0;
        do
        {
            try
            {
               n = sc.nextInt();
            }
            catch (java.util.NoSuchElementException e)
            { // intentionally left blank
            }
        } while (sc.hasNextLine());
        while(n!=0)
        {
           System.out.print(sequence);
           System.out.print(" ");
           sequence*=2;
           n-=1;
        }
        sc.close();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM