No Such Element Exception while scanning multiple inputs

Ayush Mishra :

I am new to java programming , and i am trying to learn the usage of classes and objects in java programming , while writing the following code i got an exception

java.util.NoSuchElementException

for sample input

5

1 2 3 4 5

here first line contains number of elements (in this case its 5),and next line contains elements.

while taking input inside the for loop in the class Election ,i am getting exception.

I tried searching on stack Overflow, and other resources too,but still can't figure out how to remove this exception.

import java.io.*;
import java.util.Scanner;
public class TestClass {
     public static void main(String[] args) {
        int n;
        Scanner input = new Scanner(System.in);
        n = input.nextInt();
        input.nextLine();
        Election obj = new Election(n);
        obj.getVotes();
    }
}
class Election {
     int n,v1,v2,v3,v4,v5,d;
     public Election(int n) {
          this.n = n;
          v1=v2=v3=v4=v5=d=0;
     }
     public void getVotes() {
       Scanner sc = new Scanner(System.in);
       for(int i = 0 ; i < 1 ; i++) {
         int var = sc.nextInt();
         switch(var) {
             case 1: ++v1; break;
             case 2: ++v2; break;
             case 3: ++v3; break;
             case 4: ++v4; break;
             case 5: ++v5; break;
           default: ++d; break;
         }
       }     
     }
}
Soutzikevich :

Looks like I'm a bit late, but since your accepted answer is more of comment rather than a solution, I'll post this anyway.

Here is a simple deviation of the code you provided, but reaches the desired result!

I'll walk you through this:

public class MyTest {
    public static void main(String[] args) {

        //First of all, we need an instance of an Election-type object, so
        //that we can call its methods and get votes from users.
        Election e = new Election();
        //Now we can easily call the method getVotes(), as defined in Election class.
        //What happens here, is that the program will 'jump' to the getVotes() method
        //and it will execute every line of code in that method. Then it will
        //'return' to where it 'left off' in the main() method. Since getVotes()
        //is of type 'void', it will not return anything. It will just 'jump' back.
        e.getVotes();

        //Now, you can use testResult() method, to see the values of the variables.
        e.testResult();
    }
}

Now, let's take a look at the class Election and how it works.

public class Election {
    private final int VOTES_NUM = 1;
    private int v1,v2,v3,v4,v5,d;

    public Election() {
        v1=v2=v3=v4=v5=d=0;
        //print now, just to show that all variables = 0
        testResult();
    }

    //Simple method that prints value of each variable. We use this for testing
    public void testResult(){
        System.out.println("v1 = "+v1);
        System.out.println("v2 = "+v2);
        System.out.println("v3 = "+v3);
        System.out.println("v4 = "+v4);
        System.out.println("v5 = "+v5);
        System.out.println("d = "+d);
    }

    private int getInput(){
       //First of all, we need a Scanner to take user input. 
       //You do that in your own code too. We simply move it in this method instead.
        Scanner input = new Scanner(System.in);

        //You also need variable to hold the user input. 
        //(Always give meaningful names to all entities)
        int userInput;

        System.out.print("Please enter vote number here: ");

        //the next part has to be in a try-catch block, 
        //to avoid exceptions like InputMismatchException, etc..
        try{
            //Get user input
            userInput = input.nextInt();
        }
        //If user enters letter, or symbol, or something else that isn't an integer,
        //then inform them of the mistake they made and recursively call this method,
        //until they get it right!
        catch (InputMismatchException ime){
            System.out.println("Please enter only a single number");
            return getInput();
        }

        //If all goes well, return the user input
        return userInput;
    }

    public void getVotes() {
        //'VOTES_NUM' is a constant that defines the times the 
        //loop will iterate (like Macros in 'C')
        for(int x=0; x<VOTES_NUM; x++)
            int n = getInput();
        //then let the switch statement increment one of the variables
        switch(userInput) {
            case 1: ++v1; break;
            case 2: ++v2; break;
            case 3: ++v3; break;
            case 4: ++v4; break;
            case 5: ++v5; break;
            default: ++d; break;
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=153179&siteId=1