简体   繁体   中英

Error with taking inputs all at once , when Using multiple classes and works fine in single class

If I use just one class it works perfect but when I am using multiple classes, I am facing an issue where ,When I am giving the input as whole as a batch (copy paste) it doesn't work (still waits for some more input and does nothing), but when I give each input manually it works perfect.

So, this problem started when I introduced a new class so I guess there is something wrong with the class or inheritance when utilizing with the Scanner class.

Note : this is for my college elab, so I can't use files over here.

codeWithSingleClass -Works perfect

import java.io.*;
import java.lang.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int nooftestCases=scanner.nextInt();

        while(nooftestCases>0) {
            int n,k;
            int[] array = new int[20];
            int sumWithOutRemoval=0 , sumWithRemoval=0;
            n = scanner.nextInt();
            k = scanner.nextInt();
            sumWithOutRemoval = 0;
            for (int i = 0; i < n; i++) {
                array[i] = scanner.nextInt();
                sumWithOutRemoval += array[i];
            }
            if (k == 0) {
                double finalAns = (double) sumWithOutRemoval / n;
                System.out.println(String.format("%.6f", finalAns));
            } else {
                for (int i = 0; i < n; i++) {
                    for (int j = i; j < n; j++) {
                        if (array[i] < array[j]) {
                            int temp = array[i];
                            array[i] = array[j];
                            array[j] = temp;
                        }
                    }
                }
                sumWithRemoval = 0;
                for (int i = 1; i < n - 1; i++) {
                    sumWithRemoval += array[i];
                }
                double finalAns = (double) (sumWithRemoval / (n - (2 * k)));
                System.out.println(String.format("%.6f", finalAns));
            }
            nooftestCases--;
        }
    }
}

codeWithMultipleClasses-hasIssues

import java.io.*;
import java.lang.*;
import java.util.Scanner;

class Sample {

    static int n,k;
    static int[] array = new int[20];
    static int sumWithOutRemoval , sumWithRemoval;

    public void getDetails(){
        Scanner scanner2=new Scanner(System.in);
        n = scanner2.nextInt();
        k = scanner2.nextInt();
        sumWithOutRemoval = 0;
        for (int i = 0; i < n; i++) {
            array[i] = scanner2.nextInt();
            sumWithOutRemoval += array[i];
        }
    }
    public void displayDetails(){
        if (k == 0) {
            double finalAns = (double) sumWithOutRemoval / n;
            System.out.println(String.format("%.6f", finalAns));
        }
        else {
            for (int i = 0; i < n; i++) {
                for (int j = i; j < n; j++) {
                    if (array[i] < array[j]) {
                        int temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }
            sumWithRemoval = 0;
            for (int i = 1; i < n - 1; i++) {
                sumWithRemoval += array[i];
            }
            double finalAns = (double) (sumWithRemoval / (n - (2 * k)));
            System.out.println(String.format("%.6f", finalAns));
        }
    }
}
public class Main extends Sample {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int nooftestCases=scanner.nextInt();
        Sample objname= new Sample();
        while(nooftestCases>0) {
            objname.getDetails();
            objname.displayDetails();
            nooftestCases--;
        }
    }
}

My inputs are

5
5 0
2 9 -10 25 1
5 1
2 9 -10 25 1
5 1
2 9 -10 25 1
5 0
2 9 -10 25 1
5 1
2 9 -10 25 1

expected output

5.400000
4.000000
4.000000
5.400000
4.000000

Your code has two Scanner instances; they're mocking up the input steam. If we change the code to pass the Scanner scanner into the Sample object, and use that, instead of the scanner2 , all is fine.

Ie something like this:

Scanner scanner = new Scanner(System.in);
int nooftestCases = scanner.nextInt();
Sample objname = new Sample(scanner);

And:

class Sample {
    ...

    private final Scanner scanner;

    public Sample(Scanner scanner) {
        this.scanner = scanner;
    }

    public void getDetails() {
        n = scanner.nextInt();
        k = scanner.nextInt();
        sumWithOutRemoval = 0;
        for (int i = 0; i < n; i++) {
            array[i] = scanner.nextInt();
            sumWithOutRemoval += array[i];
        }
    }
...

As an aside, there are more opportunities for improvement on this code. Formatting would be one of them, particularly when posting on Stack Overflow. Something else that specifically hurts my eyes is the static fields (not to mention their unspecified scope). This would be suspect for making algorithmic code like this work well in a broader application. And what are k and v (suggesting more-descriptive names).

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