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.

Results 1 to 19 of 19

Thread: Logical Problem

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Logical Problem

    Hi,

    The code below compiles without any errors. It's rather a logical problem. The actionPerformed method below loops through a list of Question objects ,While there is an element ahead using the hasNext() method where then if the button 'hitMe' is clicked will set text on a label, which all works fine. But however it loops through the entire list of before I can hit the button a second time. I am trying to list a question every time i click the 'hitMe' button?

    Thanks in advanced

    public void actionPerformed(ActionEvent e)
    {
    questionIterator = questions.listIterator();

    for(Question currQuestion: questions)
    {
    while(questionIterator.hasNext())
    {
    if(e.getSource() == hitMe)
    {
    Question currQ = (Question) questionIterator.next();
    questionLabel.setText(currQ.getName());
    break;
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Logical Problem

    it loops through the entire list of before I can hit the button a second time
    Computers are much faster than humans.

    Can you explain what you want the sequence of events to be?
    You do something,
    the computer does something,
    you do something,
    the computer does something,
    etc

    When posting code, Please wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Logical Problem

    Thanks for the reply. Sorry bad grammar.

    After I hit the JButton 'hitMe', I am trying to print each Question object on the 'questionLabel' JLabel during the for loop. But however it loops through the entire 'questions' List before I can click the 'hitMe' button again, as I want each individual current Question on the label?

    public void actionPerformed(ActionEvent e)
    {
    questionIterator = questions.listIterator();
     
    for(Question currQuestion: questions)
    {
    while(questionIterator.hasNext())
    {
    if(e.getSource() == hitMe)
    {
    Question currQ = (Question) questionIterator.next();
    questionLabel.setText(currQ.getName());
    break;
    }
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Logical Problem

    Please edit the code and format the statements. Nested statements should be indented. Not all statements should start in the first column.

    Can you explain what you want the sequence of events to be?
    After I hit the JButton 'hitMe',
    then what is the simple, single next step the program should do?
    After the program does that, what's next?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Logical Problem

    After I hit the JButton 'hitMe',

    The program should print a Question object within the for loop on the JLabel 'questionLabel',
    Then I should be able to hit the JButton again which then the program will print the next Question object on the JLabel within the for loop,
    until the end of Question List.

    The program instead prints the last Question object on the List on the JLabel after I hit the 'hitMe' button. (It loops till the end of the list, rather waiting for me to hit the JButton 'hitMe' again.

    public void actionPerformed(ActionEvent e)
    {
    	questionIterator = questions.listIterator();
     
    	for(Question currQuestion: questions)
    	{
    		while(questionIterator.hasNext())
    		{
    		        if(e.getSource().equals(hitMe))
    		        {
    			        Question currQ = (Question) questionIterator.next();
    			        questionLabel.setText(currQuestion.getName());
    			        break ;
    			}
    		}
    	}
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Logical Problem

    There needs to be a variable outside the listener that remembers the last text that was put in the label so it can be used to get the next text to put in the label and update the variable for the next call to the listener.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Logical Problem

    Thanks for the reply.

    So I would declare a variable where I instantiate the Listener class, and within in an if clause? Or how would I go about doing so?

    Thanks

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Logical Problem

    Define the variable at the class level outside the listener method so it will preserve its value over calls to the method.
    In the method use the current value of the variable to get the text for the label
    increment the value of the variable for the next call to the listener.

    within in an if clause?
    I'm not sure what you asking about. The only test that would need to be made would be to test the value of the variable that it does not go past the end of the list.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Logical Problem

    OK thanks.

    I understand on preserving the value outside the method, but how could I increment an object (Question in this case) where it is not an integer?

    Iv'e tried to modify the code to apply your context. Am I on the right track?

    Thanks again

    //ListenerClass
    class ListenerClass implements ActionListener
    {
    	//Preserving value for next method call
    	Question temp;
     
    	public void actionPerformed(ActionEvent e)
    	{
    		questionIterator = questions.listIterator();
     
    		if(e.getSource().equals(hitMe))
    		{
    			//iterate though Question objects
    			while(questionIterator.hasNext())
    			{
    				Question currQ = (Question) questionIterator.next();
    				//Set value on JLabel
    				questionLabel.setText(currQ.getName());
     
    				//Assigning current value to temp variable
    				temp = currQ;
    				//Wont compile?
    				temp++;
    				break;
    			}
    		}
    	}
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Logical Problem

    how could I increment an object
    You don't. The variable to save the location in the list could be an int.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Logical Problem

    OK thanks. Hopefully this will be my last question.

    Yea I could increment index value for the next method call.
    Could I use a static variable, or maybe I could declare my Question List static? In the case of holding the data in memory during runtime?

    I still do not have an idea on applying the preserved value to the Question List?

    Thanks

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Logical Problem

    I don't know why the variable needs to be static.
    Where is questions defined? Can the index be defined next to it?
    applying the preserved value to the Question List?
    Does the list have a get(int) method?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Logical Problem

    OK. I have now defined the 'questions' next to 'index' in the ListenerClass.

    I just now have created a getIndex(int index) method in the List class below:

    public int getIndex(int index)
    {
    	int var;
    	return var = listOfQuestions.indexOf(index);
    }

    Would that be correct in determining the index of the current object in the list?

    Thanks

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Logical Problem

    What determines the current object in the list?

    I was assuming that the items in the list were being accessed sequentially. Starting with the first at index 0, then the one at index 1 etc.
    The current object would be related to the value of index and how index is incremented.
    If the code gets the object at index and then increments index, then the current object was located at index-1 (before the increment).


    What is the getIndex() supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Logical Problem

    I'm not to sure, I maybe thought that the index could be returned from the current object's index location, but I realized that wont work.

    See I'm pretty confused in my code.

    I create my LinkedList<Question> questions reference,
    Then I create an instance of the class where my questions are stored: LayoutOfQuestion, which is then passed in to the questions reference calling a specified method,
    Then I create a ListIterator reference which assigns the questions reference using listIterator() method.

    These are defined in my GUI class
    LinkedList<Question> questions = new LinkedList<Question>();
    LayoutOfQuestions layQuestions = new LayoutOfQuestions();
    ListIterator<Question> questionIterator;

    In the GUI constructor(), I assign the list of questions in the LinkedList :

    questions = layQuestions.getMildQuestionList();
    The actionPerformed() method then was previously listed above.

    Thanks

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Logical Problem

    Have you looked at using the List class's get() method with an index? index starts at 0 and increments by 1 to the end of the list.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Logical Problem

    Yes I've used that method many of times even in other various classes, but I still don't know how to apply that to any of my code

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Logical Problem

    Use the get() method to get the next text for the label.
    lbl.setText(someList.get(index++));  // set text and incr index
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Logical Problem

    Ahh That worked perfectly, couldn't get my head around it, thanks alot for your patients.

    Appreciated

Similar Threads

  1. Help with logical operators
    By BiaxialPainter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2012, 12:35 AM
  2. Replies: 1
    Last Post: December 17th, 2011, 02:32 AM
  3. I have a logical error
    By n00b in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2011, 02:52 PM
  4. Logical Operators
    By truebluecougarman in forum Java Theory & Questions
    Replies: 3
    Last Post: January 22nd, 2011, 05:26 PM
  5. While (logical confusing output)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 20th, 2009, 12:17 AM