0

My coding have no error but i don't understand why there is error when i run. Below are the complete exception:-

java.util.NoSuchElementException
Initializing population...
   at java.util.StringTokenizer.nextToken(Unknown Source)
   at Input.takeinput(Input.java:44)
   at schedule.main(schedule.java:33)
Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
   at java.util.Random.nextInt(Unknown Source)
   at schedule.createpopulation(schedule.java:244)
   at schedule.main(schedule.java:37)

Where does the exception really happen?

For the first error,here's the coding (Input.java:44)

File file = new File("input.txt");
        Scanner scanner = new Scanner(file);
        while(scanner.hasNextLine()){
            String line = scanner.nextLine();
            //StringTokenizer st = new StringTokenizer(line, ";");
            //input student and sv
            if(line.equals("student")){
                nostud = 0;
                while(!(line=scanner.nextLine()).equals("examiner")){
                    StudData[nostud] = new Student();
                    StringTokenizer st = new StringTokenizer(line, ";");
                    StudData[nostud].setName(st.nextToken());
                    StudData[nostud].setCode(st.nextToken());
                    StudData[nostud].setSvName(st.nextToken());
                    StudData[nostud].setSvCode(st.nextToken());
                    nostud++;
                }
            }

and (schedule.java:33)

input.takeinput();

For the second error.(schedule.java:44)

while(!flag){
    int ex = r.nextInt(noexm);

and (schedule.java:37)

createpopulation();
Noren
  • 11
  • 4

1 Answers1

0

First Exception means that you are calling nextToken more than necessary. There is no "next token" on the string you are tokenizing. For example, for the string a;b;c, you can call nextToken only 3 times, the 4th call would throw an Exception like the one you had: NoSuchElementException Second Exception means that your noexm variable is a negative integer, which is not good. Try to debug and find out the value of noexm and figure out why it is negative.

Hamdi Douss
  • 1,033
  • 1
  • 8
  • 17