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.

Page 2 of 2 FirstFirst 12
Results 26 to 37 of 37

Thread: I have a problem with reading 5 strings from an AWT dialog window.

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    problem...seems like both objects are the same ...even if i read 2 different sets of string in the file.
    Try debugging by print out the values as they are changed to see how they get to be the same.

  2. #27
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    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.

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    Inside the try {} brackets, the 2 objects are different..but looks like the first of them gets the values from second one outside it.
    I don't understand what you are describing.
    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?

  4. #29
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    I think the problem occurs outside the try ..... i would try to use throws to be able avoid using try.

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    Sorry, without seeing the code I can't make any suggestions.

  6. #31
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy Re: I have a problem with reading 5 strings from an AWT dialog window.

    Nope...still not works .. outside the "for" bubble both objects are equal.

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    Where is the value of one object assigned to the other?

  8. #33
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    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.

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    You need to add ids to your printouts to show which println printed them:
    System.out.println("iA[o].get=" + intrArray[0].getIntrebare());

    intrArray[i] = intr;                       // here assign value for object number  i of the array
    Print out a line showing what was added at this line:
    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:
    public String toString() {
      return  here put Strings to show info re the intr object
    }
    to the intr class. Then you can print call that method by:
    System.out.println(intr); // show contents of this intr

  10. #35
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    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.

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    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);

  12. #37
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    Hooraaay !! finnaly managed it and was so easy to do it .... i have moved the initialization of the intr
    Intrebare intr = new Intrebare();
    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.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. [SOLVED] Problem in File Reading...
    By Mr.777 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 20th, 2011, 07:24 AM
  2. reading content of the text file, splitting it into strings
    By Dodo in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: January 6th, 2011, 07:57 PM
  3. Reading contents of another window
    By jimmys in forum Java Theory & Questions
    Replies: 8
    Last Post: October 4th, 2010, 11:40 PM
  4. Facing problem with open modeless dialog from modal window
    By Divya in forum Java Theory & Questions
    Replies: 1
    Last Post: May 14th, 2009, 03:18 AM
  5. Problem on reading file in Java program
    By nathanernest in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 13th, 2009, 08:14 AM