i have done a read() and a write()... after running write() the read() won't read the newly wrote data until i compile again. my guess is that the newly written data doesn't get saved until i compile again.. please if anyone knows how to make it saved so the read() can read everything after the write()

public static int read(Member box[])
	{
		Scanner reader = null;
		int count =0; 
		
		try
		{	
			reader = new Scanner(new FileReader("database.txt"));
			
			int id; 
			String type, fn, ln, ph;
			Double fee;
			
			while(reader.hasNextLine())
			{
				id= reader.nextInt();
				type= reader.next();
				fn= reader.next();
				ln= reader.next();
				ph= reader.next();
				fee= reader.nextDouble();
				
				
				box[count]= new Member(id, type, fn, ln, ph, fee);
				count++;
				
			}
	
		}
		
		catch(FileNotFoundException e)
		{
			System.out.println("File not Found!");
		}
		
		catch(IOException e)
		{
			System.out.println("Error reading");
		}
		
		catch(NoSuchElementException e)
		{
			
		}
		
		reader.close();
		return count; 
		
	}
	
	
	////////// to write data to the text file //////////////////
	public static void write(int count)
	{
		count++;
		Scanner keyboard = new Scanner(System.in);
		
		PrintWriter writer = null; 
		try
		{
			writer = new PrintWriter(new FileOutputStream("database.txt", true));
			System.out.println("Please enter for member info:");
			writer.println(count);
			
			System.out.println("What is the member's member type?");
			writer.println(keyboard.next());
			
			System.out.println("First Name:");
			writer.println(keyboard.next());
			
			System.out.println("Last Name");
			writer.println(keyboard.next());
			
			System.out.println("Phone number:");
			writer.println(keyboard.next());
			
			System.out.println("Member Fee:");
			writer.println(keyboard.nextDouble());
			
		}
		
		catch(FileNotFoundException e)
		{
			System.out.println("File not found");
		}
		
		catch(IOException e)
		{
			System.out.println("The exception has been thrown");
		}
		System.out.println("Registration Done!");
		writer.close();
		
	}

Recommended Answers

All 3 Replies

newly wrote data until i compile again

Please get your basic concepts on Java clear, compiling only takes your source code .java file(s) and generates the corresponding .class files for every class. It has nothing to do with anything that might go right or wrong at runtime

you must make sure to close your writer object and create a new reader object, after you save your new file

I was playing with java codes and decided to check your code. Its working fine.

Its quite messy because i gave only few seconds but im sure you will get it

public class TestClass {
	int val=0;
	int count=0;
	Scanner input = new Scanner(System.in);
	public TestClass(){

		do{
			System.out.println("Choose Option (1-2-3) :\n1 - Read\n2 - Write \n3 - Exit");
			val = input.nextInt();
			if(val == 1){
				read();
			}else if(val == 2){
				write();
			}
		}while(val != 3);
	}
	
	public void read(/*Member box[]*/)
	{
		Scanner reader = null;
		int count =0; 
		
		try
		{	
			reader = new Scanner(new FileReader("database.txt"));
			
			int id; 
			String type, fn, ln, ph;
			Double fee;
			
			while(reader.hasNextLine())
			{
				id= reader.nextInt();
				type= reader.next();
				fn= reader.next();
				ln= reader.next();
				ph= reader.next();
				fee= reader.nextDouble();
				
				System.out.println(new Member(id, type, fn, ln, ph, fee));
				
			}
	
		}
		
		catch(FileNotFoundException e)
		{
			System.out.println("File not Found!");
		}
		
		catch(IOException e)
		{
			System.out.println("Error reading");
		}
		
		catch(NoSuchElementException e)
		{
			
		}
		
		reader.close();
		//return count; 
		
	}
	
	
	////////// to write data to the text file //////////////////
	public void write(/*int count*/)
	{
		//count++;
		Scanner keyboard = new Scanner(System.in);
		
		PrintWriter writer = null; 
		try
		{
			writer = new PrintWriter(new FileOutputStream("database.txt", true));
			System.out.println("Please enter for member info:");
			writer.println(5);
			
			System.out.println("What is the member's member type?");
			writer.println(keyboard.next());
			
			System.out.println("First Name:");
			writer.println(keyboard.next());
			
			System.out.println("Last Name");
			writer.println(keyboard.next());
			
			System.out.println("Phone number:");
			writer.println(keyboard.next());
			
			System.out.println("Member Fee:");
			writer.println(keyboard.nextDouble());
			
		}
		
		catch(FileNotFoundException e)
		{
			System.out.println("File not found");
		}
		
		catch(IOException e)
		{
			System.out.println("The exception has been thrown");
		}
		System.out.println("Registration Done!");
		writer.close();
		
	}
	
	public static void main(String[] args) {
		new TestClass();
	}

Best of luck! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.