-3

I would like to replace the first line of the document Vokabeln.txt, where the number of vocabularies is stored, so that when a vocabulary is added, the number is increased by one. Thanks for helping me.

import java.io.*;
public class Vokabeltrainer
{
    private String file;
    private String line;
    private int anzahlVokabeln;
    private boolean status = true;
    Scanner sc = new Scanner(System.in);
    private ArrayList <Vokabel> Vokabelliste;
    public Vokabeltrainer()
    {
        this.file = "";
        this.Vokabelliste = new ArrayList<Vokabel>();
    }

    public void main() throws IOException
    {
        System.out.println("Vokabeln.txt");
        this.file = ("Vokabeln.txt");
        try (BufferedReader br = new BufferedReader(new FileReader(file)))
        {
            line = br.readLine();
            anzahlVokabeln = Integer.parseInt(line);
            for(int i = 0;i < anzahlVokabeln; i++)
            {
                line = br.readLine();
                this.Vokabelliste.add(new Vokabel(line.split("\\s+")[0],line.split("\\s+")[1]));
            }
        }
        while(status==true)
        {
            System.out.println("Was willst du machen \n-Vokabel hinzufügen\n-Vokabel enfernen\n-Vokabeln Abfragen\n-Programm Quit");
            String line = sc.nextLine();
            if(line.equals("one")||line.equals("Add vocabulary"))
            {
                Vokabelhinzufügen();
            }
            else if(line.equals("two")||line.equals("Remove vocabulary"))
            {

            }
            else if(line.equals("three")||line.equals("Vokabeln Abfragen"))
            {

            }
            else if(line.equals("four")||line.equals("Quit"))
            {
                status = false;
                //Maybe Statistics from the User
            }
            else
            {
                System.out.println("This option doesnt exists.");
            }
        }
    }

    public void Vokabelhinzufügen()
    {
        boolean vokabelhinzustatus = true;
        String Vokabel = "";
        while(vokabelhinzustatus==true)
        {
            System.out.println("Please enter the vocabulary now. (Hallo Hello)");
            Vokabel = sc.nextLine();
            try(PrintWriter output = new PrintWriter(new FileWriter("Vokabeln.txt",true))) 
            {
                output.printf("%s\r\n", Vokabel.toLowerCase());
                String after = String.valueOf(anzahlVokabeln+1);
                String before = String.valueOf(anzahlVokabeln);
//At this point the replace has to be. before is the number before the translation was added and after is after the translation was added.
            } 

            catch (Exception e) {}            
            System.out.println("Vocabulary Successfully Added");
            System.out.println("Exit Add Vocabulary?");
            String line = sc.nextLine();
            if(line.equals("yes"))
            {
                break;
            }
        }
    }

    public void Vokabelentfernen()
    {

    }
}
Christopher Schneider
  • 3,745
  • 2
  • 24
  • 38
  • What happens when you run your current code. – matt Dec 15 '21 at 16:35
  • the vocab is added but the next time i read in the file the programm doesnt read in the new vocab – Linus Huck Dec 15 '21 at 16:37
  • What is "the number"? What number? You need to add more details. Like, what does this file look like before and after you run the program? Add your comment as details to the question as well as be more specific about the problem. – Christopher Schneider Dec 15 '21 at 16:38
  • 1
    So the file changes? You only read the file once, so I don't see how the program would read the new vocab. – matt Dec 15 '21 at 16:38
  • The adding is not happening so far, only the storage of the vocabularies – Linus Huck Dec 15 '21 at 16:43
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Dec 22 '21 at 08:58

1 Answers1

0

Not an answer to your question; a design suggestion instead.

It appears that you have a file in which you store things (perhaps Vokablin) and each thing in the file is structured the same, but contains different details.

Do not store the count in the file. Instead, Just store things in the file and read them all.

If you must, add a marker at the end of the file that indicates "end of stuff". For, reasons, you might want to store a count of things in the file. If that is the case, store the count as part of the end-of-file marker.

If you use this technique, your add-to-the-file algorithm becomes this:

  1. Read a line.
  2. Is the line a thing?
  3. if yes, write it,
  4. if no, parse the end-of-file marker, increment the count, and write the end-of-file marker.
DwB
  • 37,124
  • 11
  • 56
  • 82