lundi 23 mars 2020

Why when cycling through a txt document can I not use .nextInt?

So for school I'm making a project that's going to take in a confirmation number, send out a confirmation number, and generate confirmation numbers. Right now I'm working on the generation of the confirmation number, and it was working, but I wanted to add a check to make sure that the generated confirmation number was not already generated. So I made a txt document, and added one so that it wouldn't just send back an error. So, I'm confused as to where my error is coming from. I am also aware of the issues in my code besides this, and it feels sloppy, but I'm working on it, I made all of these classes separate and now am trying to merge them together in to one program. thank you for your help and for putting up with my grammar.

another method is calling numberGen(); just for reference. I commented on the line I think the error is in because it's one listed in the error code. Method:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;

public class Conformation {
    static boolean check;

    static PrintWriter writer = null;
    static File conNums = new File("/Workspace/IA Crit C/Res/ConNums.txt");
    static File usedNums = new File("/Workspace/IA Crit C/Res/UsedConNums.txt");
    static Scanner in;

    public Conformation() throws IOException {
        int conNum = numberGen();
        check = checkNum(conNum);
        if (check == true) {
            checkUse(conNum);
        }
        addNum(conNum);

    }

    public static int numberGen() {
        Random rand = new Random();
        int max = 999999999;
        int min = 100000000;
        int conNum = 0;

        boolean run = true;

        while(run == true) {
            conNum = rand.nextInt((max - min) + 1) + min;

            check = checkNum(conNum); //if true it means it's already listed

            if (check == false) {
                run = false;
                addNum(conNum);
            }

        }
        return conNum;
    }

    private static void checkUse(int conNum) {
        try {
            in = new Scanner(usedNums);
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }

        while (in.hasNextLine()) {
            if (check == false) {
                int next = in.nextInt();
                if (next == conNum) {
                    check = true;
                }
            }
        }


    }

    private static void addNum(int conNum) {

        if (check == false) {

            try {
                writer = new PrintWriter(conNums);
                writer.println(conNum);
                writer.close();

            } catch (FileNotFoundException e) {
                System.out.println("File Not Found");
            }

        }
    }

    private static boolean checkNum(int conNum) {
        try {
            in = new Scanner(conNums);
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }

        while (in.hasNextLine()) {
            int next = in.nextInt();//this line
            if (next == conNum) {
                check = true;

            }
        }

        return check;
    }

}

Error:

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 Conformation.checkNum(Conformation.java:93)
    at Conformation.numberGen(Conformation.java:37)
    at GUI.<init>(GUI.java:55)
    at GUI$1.run(GUI.java:41)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)



Aucun commentaire:

Enregistrer un commentaire