简体   繁体   中英

How to overwrite file in Java with one changed value

I am trying to make a program that uses JFrames and JPanels to construct a voting program. There is a file called voters.txt that is formatted like so:

1234:Herb Weaselman:false
9876:Marge Magnificent:false
4444:Ingmar Inglenook:false
8888:Hector Heroman:false
5678:Agnes Angular:false

In the beginning of my program, I have to ask the user for their voter ID and if their input does not match one of the IDs from the text file, then the program ends. I have managed to achieve this but now I need to overwrite this voters.txt file by changing the false element of whoever voted to true. For instance, if the user entered an ID of 1234, then I would need to change the word after Herb Weaselman to true instead of false. I am not sure how to do this. I know how to write to a file but am not sure how to change that one specific element. Here is the code I have currently:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;

public class Assig5 extends JFrame
{

    public Assig5()
    {

    }

    public static void main(String[] args) throws IOException
    {
        LoginWindow loginWindow = new LoginWindow();
        loginWindow.fileReader();
        if(!loginWindow.checkResult(loginWindow.result))
        {
            System.out.println("Invalid Voter ID");
            System.exit(0);
        }
        System.out.println("Continue");

    }
}

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;

public class LoginWindow extends JFrame
  {
    static ArrayList<String> list = new ArrayList<String>();
    public String result;
    public LoginWindow()
    {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      result = JOptionPane.showInputDialog(frame, "Enter voter ID:");

    }
    public static void fileReader() throws FileNotFoundException
    {
        Scanner s = new Scanner(new File("voters.txt"));
        while (s.hasNextLine())
        {
          list.add(s.nextLine());
        }
        s.close();
    }
    public static boolean checkResult(String result)
    {
        for(int i = 0; i < list.size(); i++)
        {
          String[] retval = list.get(i).split(":");
          if(retval[0].equals(result))
          {
            return true;
          }
        }
        return false;
    }
  }

Any help would be appreciated. Thanks in advance.

Just change your array index from false to true when there is a match. Then rewrite the file with the data from the array. If there is not match, the file does not change.


Solution

if(retval[0].equals(result)){
    retval[2] = true;
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM