After an hour of browsing the web, I decided to ask for help for my IB project (can't paste my whole code or I'll be cheating off myself). I don't have much experience, so please explain whatever syntax your solution may have (please don't say rewrite the whole thing).

Anyway, I decided to write a program that will store user information such as name, number and notes on them. I created a hashmap called accounts and used the following code to add new infomation

Record temp = new Record();
		temp.name =  nm;
		temp.number = nb;
		record.add(temp);
		Object t = (Object)nm;
		accounts.put(t, temp);
/*
Record is a class i created to store the stuff, nm and nb i got from user input. record is an Arraylist that i'm not sure i need later.
*/

To edit something, say the phone number i add this to the above

temp = accounts.get(t);
		temp.number = nb;
		accounts.put(t, temp);

Now is there a way I can check if t (the name) is already in the account hash map? I realized this is a problem after I accidentally typed a non-existent name and the output was weird.

Also, I need some advise on how to write the info to a text file. I was thinking of just adapting this code my teacher gave us and separating the different variables with symbols that acts as tokens.

try
		{
			PrintWriter pw = new PrintWriter( new FileWriter(
				new File( "FileName.txt" )));
			for(int i = 0; i<SIZE; i++)
				pw.println( ""+ array[ i ]);
			pw.close();
			fileB = true;
		}catch(IOException e)
		{
			JOptionPane.showMessageDialog(null,
				"ERROR in fileArray()\n"+e);
		}

My question is how do I erase and edit the text file when I edit information? (eg, what where and what type of search?)I plan to write to the text file before exit and read it when the program start.

Also, what kind of loop should be used if i want to be able to restart a certain process if wrong information is typed? For example, i'll have a conformation message, and if it's no i want to be able to go back to the start of that process. I know java don't have flags so...


Anyway, I'll appreciate any help I can get, thanks.

Recommended Answers

All 8 Replies

Now is there a way I can check if t (the name) is already in the account hash map? I realized this is a problem after I accidentally typed a non-existent name and the output was weird.

Yes, you can use account.containsKey(yourKey) function. Check documentation for the same

Also, what kind of loop should be used if i want to be able to restart a certain process if wrong information is typed? For example, i'll have a conformation message, and if it's no i want to be able to go back to the start of that process. I know java don't have flags so...

You can use a while loop infinitely and maybe write your condition inside the loop(which leads to exit/stop the process) and break the loop when that condition is met or simply give that condition in the while loop.
Since you want to start your process all over again you will need to re initialize process specific values to their initial values. I think only doing this much will be enough

My question is how do I erase and edit the text file when I edit information? (eg, what where and what type of search?)I plan to write to the text file before exit and read it when the program start.

This technique might be bad efficiency wise , but the most simplest way i can think of is read the entire file as a string, use replace function of String class to edit/erase your file contents and write back to your file i.e. overwrite it.

commented: very helpful relplies ty +0

nice, 2 problems fixed, Thanks! I ended up having to wrap all my code in do while loops... Say, can i call a method from itself? If I could then wouldn't the while loops be unnecessary?

nice, 2 problems fixed, Thanks! I ended up having to wrap all my code in do while loops... Say, can i call a method from itself? If I could then wouldn't the while loops be unnecessary?

Yes if you are saying recursion then yes you could do it. But you will have to specify a condition inside your function, and each time your function will be called you will have to check for that condition. If met you will have to stop calling function any further now. But usually, this would be something you would like to avoid because of the function overhead each time function is called plus personally, I think it will be difficult to maintain.

You can search the web to find out trade offs and advantages/disadvantages of recursion over iteration

This technique might be bad efficiency wise , but the most simplest way i can think of is read the entire file as a string, use replace function of String class to edit/erase your file contents and write back to your file i.e. overwrite it.

I'm guessing you mean something like this?

String data = str.replaceAll("John", "relacedName");

Is there a way to specify a section of the long data string? Some data will be repetitive (such as payment status) and i won't want to change all the data in the string (would String tokenizer work?)

But you will have to specify a condition inside your function, and each time your function will be called you will have to check for that condition. If met you will have to stop calling function any further now. But usually, this would be something you would like to avoid because of the function overhead each time function is called plus personally, I think it will be difficult to maintain

Function overhead just mean extra memory right? In that case if I use an if else statement that only call the method when an error occurs, overhead shouldn't be a problem, right?

Is there a way to specify a section of the long data string? Some data will be repetitive (such as payment status) and i won't want to change all the data in the string (would String tokenizer work?)

You will have to take care that only the intended string gets replaced. for ex. You will have something say a name associated with payment status so you will want to edit only that payment status which corresponds to that name

Function overhead just mean extra memory right? In that case if I use an if else statement that only call the method when an error occurs, overhead shouldn't be a problem, right?

Its not just extra memory. Check on the web you will get a lot of resources that would list out to you disadvantages of using recursive functions and cases where using them is suitable

Depending on where you are going next with this, it may be a good idea to stop for a moment and think design. It sounds like you have a class that represents an Account, a Collection that holds multiple Accounts, and a text file that contains the data for all those accounts. Now you are looking at updating the text file, but not utilising much of what you know about its structure.
Why start with two methods. One to write the whole file form the Collection, and one to read the whole file and re-create the Collection of Accounts in memory. You're going to need them both sooner or later anyway.
Now doing an update is very clean - read the file, loop through the Collection updating Accounts as needed, re-write the file.
This also means that if you change the file format later (eg to add more info, or use XMLEncoder to write it all as an XML file with one line of code) you have just 2 methods to change, and you update code won't be affected at all.

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.