1

I'm having a really hard time creating this address book of mine, and im also new at this. the thing is I just want to know if it is possible to edit or modify the values inside the array,

String LNAME[]   = new String[SIZE];
String FNAME[]   = new String[SIZE];
String ADDRESS[] = new String[SIZE];
String CONTACT[] = new String[SIZE]; 

if for example i already assigned John Dor in the FNAME array how could i edit that value without going through the whole process again since i would just want to replace r with e so it would be john doe. given address is also there and addresses would contain large strings...

Kara
  • 6,115
  • 16
  • 50
  • 57
user2124478
  • 27
  • 1
  • 6
  • Possible duplicate of http://stackoverflow.com/questions/703548/how-to-change-the-value-of-array-elements – Pradeep Simha Mar 01 '13 at 17:32
  • @PradeepSimha OP wants to change one character in a string. It's more likely to be a duplicate of http://stackoverflow.com/questions/6952363/java-replace-a-character-at-a-specific-index-in-a-string – harpun Mar 01 '13 at 17:37
  • 4 arrays of the same size is asking for an array some some `Object` that holds the information... – Boris the Spider Mar 01 '13 at 17:37

1 Answers1

2

Well, its relatively easy, with normal String arrays you can point using index such as follows, plus assigning new String or using method such as replace ... check this out:

String FNAME[] = new String[1];
FNAME[0] = "John Dor";

System.out.println("FNAME #1: " + FNAME[0]); // prints John Dor

// Using replace to change letter
FNAME[0] = FNAME[0].replace('r', 'e');

System.out.println("FNAME #2: " + FNAME[0]); // prints John Doe

// Replacing with completely new string
FNAME[0] = "John Dor";

System.out.println("FNAME #3: " + FNAME[0]); // prints John Dor

FNAME[0] = "John Doe";

System.out.println("FNAME #4: " + FNAME[0]); // prints John Doe

Problem with String arrays is that you need to know size of them when initializing, I usually prefer ArrayList's instead. Take a look of it from this source: ArrayList example

Mauno Vähä
  • 9,688
  • 3
  • 33
  • 54
  • so heres the thing, theres also this option on the menu where as when you choose "E" (ignorecase()) it promts whether to replace or just edit, and thats where my problem starts when you choose just edit, it should be able to delete the previous char or some char of the string. is that possible? System.out.println("[A] ADD Contact"); System.out.println("[V] VIEW Contact"); System.out.println("[E] EDIT Contact"); System.out.println("[D] DELETE Contact"); System.out.println("[X] Exit Program"); – user2124478 Mar 01 '13 at 18:26
  • oh... cancel that thought. just found this, thanks thought. String are immutable in Java. You can't change them. You need to create a new string with the character replaced. – user2124478 Mar 01 '13 at 18:32