0

I have a string idnum. I add a value to that string using the code below. The function show() displays the value of the string in the console. I wish to have a function that can edit the value of string idnum in the console, without having to exit the console. How do I do that? I am thinking of overwriting the value of the string. Is that possible? How do I overwrite the value of a string in the console?

    public void addstudent(){       
    student p = null;       
    reader1 = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("ID Number: ");
    String idnum = null;
    try {
        idnum = reader1.readLine();
        } 
    catch (IOException e){
                   e.printStackTrace();
                  } 
            p = new student(idnum);         
    studentList.add(p);  
    show();
}

I have this code for the search part

public void searchstudent(){
    System.out.print("search name : "); 
    try{ searchName = reader1.readLine();} catch (IOException e){e.printStackTrace();} 
    searchCount = 0;//for search     
    for(int i = 0; i<count; i++){
        if(studentList.get(i).getLName().toLowerCase().contains(searchName.toLowerCase(‌​))){
            searchCount = i;
            System.out.println(studentList.get(searchCount).getLName());
        }
    }
}
aalku
  • 2,860
  • 2
  • 23
  • 44
murrmurr
  • 115
  • 1
  • 4
  • 10
  • I don't understand the question. What is the compiler from your point of view? Could you explain what you need without the word 'compiler'? – aalku Feb 13 '14 at 14:39
  • Yes...the phrase *in the compiler* doesn't make any sense, unless you mean *at runtime*. If I understand you correctly though, you cannot change the value of a string in a separate method and expect that change to be reflected else where that same string object is referenced (in java). – Paul Richter Feb 13 '14 at 14:39
  • Im sorry, I myself couldn't figure the right question for my question. Can you suggest a better title for this post? I appreciate if you do. – murrmurr Feb 13 '14 at 14:40
  • Im sorry, compiler is not the right word, console is. My bad. – murrmurr Feb 13 '14 at 14:42
  • Well strings are an immutable type the compiler makes different copies of that string everywhere else that it is being used. Java passes by reference i believe. – diaz994 Feb 13 '14 at 14:45
  • 2
    Not it is less confusing but I still don't understand what you want. Please, try to explain how you want to get and not your aproach to the solution. – aalku Feb 13 '14 at 14:47
  • Do you want the console to change the value of idnum and then display it to the console in real time? Print the console -> update value -> erase old value from console -> show new value? – diaz994 Feb 13 '14 at 14:54
  • @diaz994 **[Not pass-by-reference](http://stackoverflow.com/questions/40480/is-java-pass-by-reference)** – Paul Richter Feb 13 '14 at 14:54
  • Then pass by value it is. – diaz994 Feb 13 '14 at 14:54
  • @user270349 my teacher wants as to do this Student Teacher Directory.A program that will save the following information Student: id number, lastname, firstname Teacher: lastname, firstname. The directory program should have the following in the menu. 1 add >> student >> teacher 2 edit >> student >> teacher 3 find by name >> student >> teacher He wants us to do this without a GUI, and I find it so difficult. Im already done with the add and search part for both the student and the teacher. I dont know what to do with the edit. :(( – murrmurr Feb 13 '14 at 15:13
  • @diaz994 my teacher wants as to do this Student Teacher Directory.A program that will save the following information Student: id number, lastname, firstname Teacher: lastname, firstname. The directory program should have the following in the menu. 1 add >> student >> teacher 2 edit >> student >> teacher 3 find by name >> student >> teacher He wants us to do this without a GUI, and I find it so difficult. Im already done with the add and search part for both the student and the teacher. I dont know what to do with the edit. :(( – murrmurr Feb 13 '14 at 15:13
  • @Teeg my teacher wants as to do this Student Teacher Directory.A program that will save the following information Student: id number, lastname, firstname Teacher: lastname, firstname. The directory program should have the following in the menu. 1 add >> student >> teacher 2 edit >> student >> teacher 3 find by name >> student >> teacher He wants us to do this without a GUI, and I find it so difficult. Im already done with the add and search part for both the student and the teacher. I dont know what to do with the edit. :(( – murrmurr Feb 13 '14 at 15:15
  • 1
    @murrmurr - We don't understand what you are saying. Saying it 4 times doesn't help. Perhaps you need to find someone who speaks your language to help you. – Stephen C Feb 13 '14 at 15:31
  • @murrmurr So what part of the "edit" don't you know how to do? – Paul Richter Feb 13 '14 at 15:33
  • @StephenC im sorry. Im new at stackoverflow, I dont know if the people I are notified or not everytime I add a comment without taging their names. sorry. – murrmurr Feb 13 '14 at 15:37
  • @Teeg everything. Im kinda lost on what to do next. Im a newbie in Java programming with a teacher that sucks at teaching. – murrmurr Feb 13 '14 at 15:39
  • @murrmurr k...well you have to understand that saying "everything" is far too broad a question, especially for Stackoverflow. This site is more for specific questions. Your original question was actually good in that way (even though nobody understood it), as it was specific. What you need to do is look at the requirements for this "edit" functionality (what it needs to do), and then figure out what parts you don't know how to do, or don't know their meaning. That should give you lots of coherent phrases to use in google to go the rest of the way. Diaz994's post about setters is also relevant. – Paul Richter Feb 13 '14 at 15:45
  • @murrmurr Also, regardless whether your teacher "sucks at teaching", they are one who gave you the assignment; if something in the written document isn't clear to you, they are probably the best person to ask for clarification. Worst case, talk to some of your classmates. – Paul Richter Feb 13 '14 at 15:46
  • @Teeg I already asked my teacher about the editing part and he just said that "Its up to me, on how my approach." Im currently reading stuffs about the getter and setter method. My first idea on how to edit is first to delete the first value of the string and input a new one using the add function i made. but then, I don't know how to delete a value of a string or if it is possible. The next idea I had is this post, overwriting the value of the string, and still, I dont know how. :( – murrmurr Feb 13 '14 at 15:58
  • 1
    @murrmurr You're thinking about it too much. If `address = "24 Sussex Drive"`, and then later in the program you do `address = "541 Acacia Avenue"`, the old value (the sussex drive string), is gone. The value at `address` no longer points to that old string; it now points to the acacia drive string. You don't need to worry about "deleting" a value, you can simply reassign it. Or said another way, have your variable (address in this case) point to something else. That's exactly what diaz and user270349 are saying as well. – Paul Richter Feb 13 '14 at 16:04
  • @Teeg with getter and setter method, I can change the value of the string in the console, not in the code? – murrmurr Feb 13 '14 at 16:14
  • @murrmurr Forget the console. Think of the console like a blackboard (or whiteboard) at school. Let's say I have a piece of paper that has an address on it, and I ask you to write whatever the paper says on the board. Then I change the value on that paper; does that automatically change what is written on the board? The answer of course is no. I would have to ask you to write the new value on the board again. Its the same with the console. So if you changed some value of your student object, for example, you'd have to write that new value to the console again. – Paul Richter Feb 13 '14 at 16:19
  • 1
    @Teeg Thank you! thank you! thank you! I understand it already. – murrmurr Feb 13 '14 at 16:21
  • Imagine you have a method just like searchstudent but called searchAndEditStudent... What do you want to edit? Every field? – aalku Feb 13 '14 at 17:23
  • @user270349 yes every field – murrmurr Feb 14 '14 at 02:36

2 Answers2

0

You have to find the student and edit it like this:

student.setAddress("Sesame street, 1");

or if you don't have a setter:

student.address = "Sesame street, 1";
aalku
  • 2,860
  • 2
  • 23
  • 44
  • where do I do that? I have this code for the search part public void searchstudent(){ System.out.print("search name : "); try{ searchName = reader1.readLine();} catch (IOException e){e.printStackTrace();} searchCount = 0;//for search for(int i = 0; i – murrmurr Feb 13 '14 at 15:28
  • Please add the code (formatted) to the question, it is very hard to read in a comment. – aalku Feb 13 '14 at 15:58
  • how can i do that? Im sorry, im new here in stackoverflow. I dont know how stuffs work. – murrmurr Feb 13 '14 at 16:00
  • I don't know if you can edit it or you need more reputation, I did it. – aalku Feb 13 '14 at 17:12
0

Make sure that in your class you have a getter and setter method. Once you do you can just call the set method in your student class to update the value.

public String getStudent() {
        return student;
    }


public void setStudent(String student) {
       this.student = student;
    }

You would call it like this.

student.setStudent("Mark Twain");
diaz994
  • 354
  • 3
  • 13
  • Thank you. I will try reading first about the getter and the setter method. Then I'll try to do this. Our teacher gives us this kind of activity without even teaching us stuffs like getter and setter. @@ – murrmurr Feb 13 '14 at 15:31