Home > Software engineering >  Runtime error in Java while testing problem solving. Am i doing the input part wrong on String array
Runtime error in Java while testing problem solving. Am i doing the input part wrong on String array

Time:07-27

enter image description hereVery simple problem but showing Runtime error on the site(beginner btw). Not so sure about the error, but i think the input part is incorrect(getting multiple strings from input). Is there any other ways to get input on String array? if there is, please let me know.

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
    at ExcellentResult.main(ExcellentResult.java:14)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at _$SandboxExecutor.main(_$SandboxExecutor.java:38)`package examples0;
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int salary = scanner.nextInt();
        String[] tabs = new String[n];
        int i;
        for(i=0; i<n; i  ) tabs[i] = scanner.nextLine();
        for(i=0; i<n; i  ){
            switch (tabs[i]) {
                case "Facebook":
                    salary-=150;
                    break;
                case "Instagram":
                    salary-=100;
                    break;
                case "Reddit":
                    salary-=50;
                    break;
            }
        }
        if(salary>0){
            System.out.println(salary);
        }
        else{
            System.out.println("You have lost your salary.");
        }
    }

CodePudding user response:

The code seems to be correct, this was the output that I got after running it:

    3
    1000
    Facebook
    Instagram
    750

Another way to get String inputs is by providing command line arguments when you run it and then access those strings through the "args". Your code will look like:

    public static void main(String[] args) {
    
    int salary = Integer.parseInt(args[0]);
    
    for(int i=1; i<args.length; i  ){
        switch (args[i]) {
            case "Facebook":
                salary-=150;
                break;
            case "Instagram":
                salary-=100;
                break;
            case "Reddit":
                salary-=50;
                break;
        }
    }
    if(salary>0){
        System.out.println(salary);
    }
    else{
        System.out.println("You have lost your salary.");
    }
}

You will have to run it using your terminal like:

    % javac <Name of class>.java
    % java <Name of class> 3000 Facebook Instagram Reddit
    2700
    % java <Name of class> 100 Facebook Instagram Reddit 
    You have lost your salary.

I hope this helps.

  • Related