jeudi 29 juin 2017

Java putting random symbols in my text file instead of integers

I have a program that is supposed to put a string and random integer into a text file. For some reason my program is putting a random symbol into the file instead of an integer. Anyone know why this is happening?

What the file currently looks like:

Test
賬
Test2
∗

What the file should look like:

Test
36076
Test2
74263

Class that gets the string and random number:

public void newList() {

    boolean newCode = false;

    while (!newCode) {

        System.out.println("NEW LIST");
        System.out.print("List Name: ");

        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        listName = scanner.next();

        System.out.println("The list name has been saved!");
        System.out.println();

        Random generateCode = new Random();
        setGeneratedCode(generateCode.nextInt((99999 - 10000) + 1) + 10000);

        File f = new File(generatedCode + ".txt");
        if (f.exists()) {
            newCode = false;
        }
        if (!f.exists()) {
            newCode = true;
        }
    }

    ListsWriter listsWriterObject = new ListsWriter();
    listsWriterObject.listsWriter(listName, generatedCode);
}

Class that writes the string and number to the file:

public void listsWriter(String listName, int generatedCode) {
    BufferedWriter bw = null;

    File lists = new File("lists.txt");

    try{
        FileWriter fw = new FileWriter(lists, true);
        bw = new BufferedWriter(fw);

        bw.write(listName);
        bw.newLine();
        bw.write(generatedCode);
        bw.newLine();
        bw.close();

    }catch(IOException ex){
            ex.printStackTrace();
    }


    FileCreator fileCreatorObject = new FileCreator();
    fileCreatorObject.fileCreator(generatedCode);
}




Aucun commentaire:

Enregistrer un commentaire