Try debugging by print out the values as they are changed to see how they get to be the same.problem...seems like both objects are the same ...even if i read 2 different sets of string in the file.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Try debugging by print out the values as they are changed to see how they get to be the same.problem...seems like both objects are the same ...even if i read 2 different sets of string in the file.
Inside the try {} brackets, the 2 objects are different..but looks like the first of them gets the values from second one outside it. Have no clue why it happens like that
Last edited by piulitza; July 24th, 2011 at 12:47 PM.
I don't understand what you are describing.Inside the try {} brackets, the 2 objects are different..but looks like the first of them gets the values from second one outside it.
Are you saying that two objects are different in one place and the same in another?
Is there code that assigns the value of one to the other?
I think the problem occurs outside the try ..... i would try to use throws to be able avoid using try.
Sorry, without seeing the code I can't make any suggestions.
Nope...still not works .. outside the "for" bubble both objects are equal.
Where is the value of one object assigned to the other?
Here it is where i assign values for the object (i also added some comments this time):
for (int i=0; i<j&&in.hasNext();i++) { intr.setIntrebare(in.next()); // here i assign values for each String of the object intr.setRaspuns1(in.next()); intr.setRaspuns2(in.next()); intr.setRaspuns3(in.next()); intr.setRaspuns4(in.next()); intr.setRaspuns5(in.next()); intr.setRaspunsCorect(in.next()); intrArray[i] = intr; // here assign value for object number i of the array if (i==0) // here i println the value of the String Intrebare of the object nr 0 at the first run of the bubble { System.out.println(intrArray[0].getIntrebare()); } if (i==1) // here i test if both objects from the String are equal and println the values of Intrebare String of both objects { if (intrArray[0].equals(intrArray[1])) { System.out.println("Strings are equal"); } System.out.println(intrArray[0].getIntrebare()); System.out.println(intrArray[1].getIntrebare()); } }
and when i run it i get on the console
Care_este_capitala_Romaniei?
Strings are equal
Care_este_capitala_Bulgariei
Care_este_capitala_Bulgariei
So, it looks like at the first run of the bubble the first object from the first object from the array gets the first values of the first set of Strings from the file, but at the second one both objects get values the second set of Strings. But i don't understand why it happens like that , and i want that at the second run of the String the first object to keep the values of first set of Strings and only second object get the values of the second set of Strings.
You need to add ids to your printouts to show which println printed them:
System.out.println("iA[o].get=" + intrArray[0].getIntrebare());
Print out a line showing what was added at this line:intrArray[i] = intr; // here assign value for object number i of the array
Show the value of i and the contents of the intr variable.
Why do you think the values returned by getIntrebare() should be different for different objects?
Add:
to the intr class. Then you can print call that method by:public String toString() { return here put Strings to show info re the intr object }
System.out.println(intr); // show contents of this intr
I did those changes and now i have
public static void listare(Intrebare intr) // method to list current values of String of the intr object { System.out.println("Current values of intr are:"); System.out.println("intr.getIntrebare() "+intr.getIntrebare()); System.out.println("intr.getRaspuns1() "+intr.getRaspuns1()); System.out.println("intr.getRaspuns2() "+intr.getRaspuns2()); System.out.println("intr.getRaspuns3() "+intr.getRaspuns3()); System.out.println("intr.getRaspuns4() "+intr.getRaspuns4()); System.out.println("intr.getRaspuns5() "+intr.getRaspuns5()); System.out.println("intr.getRaspunsCorect() "+intr.getRaspunsCorect()); } public static Intrebare[] ListaCompletaIntrebari (int j,String url) { Intrebare intr = new Intrebare(); Intrebare[] intrArray = new Intrebare[j]; try { Scanner in = new Scanner (new FileReader(url)); for (int i=0; i<j&&in.hasNext();i++) { intr.setIntrebare(in.next()); // here i assign values for each String of the object intr.setRaspuns1(in.next()); intr.setRaspuns2(in.next()); intr.setRaspuns3(in.next()); intr.setRaspuns4(in.next()); intr.setRaspuns5(in.next()); intr.setRaspunsCorect(in.next()); System.out.println("i= "+i); // here assign value for object number i of the array System.out.println(intr); listare(intr); intrArray[i] = intr; if (i==0) // here i println the value of the String Intrebare of the object nr 0 at the first run of the bubble { System.out.println("intrArray[0].get "+intrArray[0].getIntrebare()); } if (i==1) // here i test if both objects from the String are equal and println the values of Intrebare String of both objects { if (intrArray[0].equals(intrArray[1])) { System.out.println("Strings are equal"); } System.out.println("intrArray[0].get "+intrArray[0].getIntrebare()); System.out.println("intrArray[1].get "+intrArray[1].getIntrebare()); } } } catch(Exception e) { System.out.println(e); } return intrArray; }
And when i run it i get
i= 0
intr = introducereIntrebari.Intrebare@14318bb
Current values of intr are:
intr.getIntrebare() Care_este_capitala_Romaniei?
intr.getRaspuns1() Bucuresti
intr.getRaspuns2() Brasov
intr.getRaspuns3() Timisoara
intr.getRaspuns4() Iasi
intr.getRaspuns5() Cluj
intr.getRaspunsCorect() Bucuresti
intrArray[0].getIntrebare() Care_este_capitala_Romaniei?
i= 1
intr = introducereIntrebari.Intrebare@14318bb
Current values of intr are:
intr.getIntrebare() Care_este_capitala_Bulgariei
intr.getRaspuns1() Sofia
intr.getRaspuns2() Rousse
intr.getRaspuns3() Balcik
intr.getRaspuns4() Craiova
intr.getRaspuns5() Varna
intr.getRaspunsCorect() Sofia
Strings are equal
intrArray[0].getIntrebare() Care_este_capitala_Bulgariei
intrArray[1].getIntrebare() Care_este_capitala_Bulgariei
Of course i do not expect that getIntrebare() value would be same after each run because it gets value of next String. But i want that only the object number i of the array get that value, and the first object should keep the first value of the getIntrebare(). The problem is that first object from the array also gets the values of the Strings changed.
How many different intr objects are created? If you only have one then the contents of the object will be from the last time it was changed. Make sure you have more than one intr object.
Did you add the toString() method to the class so that you can print all the info in the class with the simple statement: System.out.println(intr);
Hooraaay !! finnaly managed it and was so easy to do it .... i have moved the initialization of the intr
and now placed it inside of the for bubble. Now the first object from array keeps its old values and only second one gets the new ones.Intrebare intr = new Intrebare();