Click here to Skip to main content
15,116,354 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello there,ive written this code for my project and the write file is bugging me out.
whenever it writes in the "votes.txt" it writes flawlessly but whenever i add other voter it writes at the aame line and modifies it.
i dont now if that made sense but hope you help <3

What I have tried:

Java
<pre>public class Voters {

	public static HashSet<Voter> voters = new HashSet<>();

	public static void main(String[] args) throws IOException {

		while (true) {
			System.out.println("1. Add New Voter");
			System.out.println("2. List All Voters");
			System.out.println("3. Find a Voter By Name");
			System.out.println("4. Get information about specific voter");
			System.out.println("5. Exit");
			Scanner scanner = new Scanner(System.in);

			int chosen = scanner.nextInt();

			switch (chosen) {
			case 1:
				System.out.print("National ID Number: ");
				int idNum = scanner.nextInt();
				System.out.print("\nName: ");
				String name = scanner.next();
				System.out.print("\nMale Relative: ");
				String maleRelative = scanner.next();
				System.out.print("\nAge: ");
				int age = scanner.nextInt();
				System.out.print("\nAddress: ");
				String address = scanner.next();
				System.out.print("\nProvince: ");
				String province = scanner.next();
				System.out.println("");
				Voter voter = new Voter(idNum, name, maleRelative, age, address, province);
				voters.add(voter);
			    FileWriter fileWriter = new FileWriter("C:\\Users\\Babish\\Desktop\\voters.txt");
				fileWriter.append("\n").append(voter.toString());
				fileWriter.flush();
				fileWriter.close();
				break;
			case 2:
				for (Voter v : voters) {
					System.out.println(v);
				}
				break;
			case 3:
				System.out.print("Name: ");
				Scanner ss = new Scanner(System.in);
				String n = ss.nextLine();
				System.out.println();
				for (Voter v : voters) {
					if (v.name.contains(n)) {
						System.out.println(v);
						break;
					}
				}
				break;
			case 4:
				System.out.print("Id: ");
				Scanner nn = new Scanner(System.in);
				int s = nn.nextInt();
				System.out.println();
				for (Voter v : voters) {
					if (v.nationalIdNumber == s)  {
						System.out.println(v);
											}
				}
				break;
			case 5:
				System.exit(0);
				break;
			default:
			}

		}
	}

	static class Voter {
		public int nationalIdNumber;
		public String name;
		public String maleRelativeName;
		public int age;
		public String address;
		public String province;

		public Voter(int nationalIdNumber, String name, String maleRelativeName, int age, String address,
			String province) {
			this.nationalIdNumber = nationalIdNumber;
			this.name = name;
			this.maleRelativeName = maleRelativeName;
			this.age = age;
			this.address = address;
			this.province = province;
		}

		public String toString() {
			return "Id: " + nationalIdNumber + "  Name: " + name + "  Male Relative: " + maleRelativeName + "  Age: "
					+ age + "  Address: " + address + "  Province: " + province;
		}
	}
}
Posted
Updated 26-Apr-21 2:41am

1 solution

The FileWriter constructor taking just one parameter - the file path - opens a brand new file, overwriting any existing file.

To append to an existing file, use the two parameter version that takes a bool as the second parameter, and pass it "true" to append to an existing file if any exists:
Java
FileWriter fileWriter = new FileWriter("C:\\Users\\Babish\\Desktop\\voters.txt", true);
   
Comments
CPallini 26-Apr-21 7:56am
   
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900