sorting price in ascending order

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 683 Views - Last Post: 03 June 2019 - 10:17 AM Rate Topic: -----

#16 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,842
  • Joined: 25-December 13

Re: sorting price in ascending order

Posted 01 June 2019 - 08:20 AM

Quote

the ouput is "Not available"

Try debugging the code by adding print statements that shows what the code is doing as it executes.
Print the value being searched for and the values of each item as it is retrieved from the list and compared to the searched for item.
Was This Post Helpful? 0
  • +
  • -

#17 arexxa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-May 19

Re: sorting price in ascending order

Posted 01 June 2019 - 08:29 AM

temp = (bedMattress)bedMatt.getFirst();
        String srcB = JOptionPane.showInputDialog("Search for bed: ");
        System.out.println("\nSearching for bed selected....");
        if(temp.getBed().equalsIgnoreCase(srcB)){
            bedMatt.bedDetail(srcB);
        }
        else{
            System.out.println("NOT AVAILABLE....");
        }



before this i do looping for searching, but there is not work out..i disable searching and searching method for mattress i do as comment. and it work.
when i try to do same coding but for mattress it does't work.
Was This Post Helpful? 0
  • +
  • -

#18 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,842
  • Joined: 25-December 13

Re: sorting price in ascending order

Posted 01 June 2019 - 09:12 AM

The posted code only looks at one item. What if the searched for item is not first?
What does the code do if the searched for item is the first in the list?

What is the value returned by getBed() method? Add a print statement that shows its value.
Also print out the value in srcB so you can see what the computer is comparing in the statement on line 4
Was This Post Helpful? 0
  • +
  • -

#19 arexxa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-May 19

Re: sorting price in ascending order

Posted 01 June 2019 - 11:20 PM

i try to do this coding in looping to check whether the linkedlist is not null, but it has an error where NullPointerException at the line getNext() in test class and the line of currNode = currNode.next in LinkedList class.
so i changed the code to not do loop.just simply called the method.

temp = (bedMattress)bedMatt.getFirst();
String srcB = JOptionPane.showInputDialog("Search for bed: ");
while( temp != null){
      temp.bedDetail(srcB)
}
temp = (bedMattress)bedMatt.getNext()



the getBed() method will return the type of bed and i print the statement in the linkedlist. is this what you mean by print out the value?

public void bedDetail(String search){
        currNode = firstNode;
        while(currNode!= null){
            if(currNode.data.getBed().equalsIgnoreCase(search)){
                System.out.println("Type of Bed: " + currNode.data.getBed());
                System.out.println("Bed Price:RM " + currNode.data.calcBed());
            }
            currNode = currNode.next;
        }
    }


Was This Post Helpful? 0
  • +
  • -

#20 arexxa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-May 19

Re: sorting price in ascending order

Posted 02 June 2019 - 12:05 AM

sorry this the code that have error
Object obj = bedMatt.getFirst();
        String srcB = JOptionPane.showInputDialog("Search for bed: ");
        while( obj != null){
            bedMatt.bedDetail(srcB);
            obj = (bedMattress)bedMatt.getNext();
        }


Was This Post Helpful? 0
  • +
  • -

#21 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3638
  • View blog
  • Posts: 16,706
  • Joined: 20-September 08

Re: sorting price in ascending order

Posted 02 June 2019 - 02:07 AM

Quote

        while( obj != null){
            bedMatt.bedDetail(srcB);
            obj = (bedMattress)bedMatt.getNext();
        }


Why do you have nested list iteration? bedDetail (not a good method name) is already iterating the list
Was This Post Helpful? 0
  • +
  • -

#22 arexxa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-May 19

Re: sorting price in ascending order

Posted 02 June 2019 - 02:51 AM

ohhh so i just don't need to loop in test class right? okay..but that if i want to search for bed..after that i do the same coding to search mattress but it not work out. the code exactly like the bedDetail. why it is not work out?
        temp = (bedMattress)bedMatt.getFirst();
        String srcB = JOptionPane.showInputDialog("Search for bed: ");
        System.out.println("\nSearching for bed selected....");
        if(temp.getBed().equalsIgnoreCase(srcB)){
            bedMatt.bedDetail(srcB);
        }
        else{
            System.out.println("NOT AVAILABLE....");
        }


Was This Post Helpful? 0
  • +
  • -

#23 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3638
  • View blog
  • Posts: 16,706
  • Joined: 20-September 08

Re: sorting price in ascending order

Posted 02 June 2019 - 03:37 AM

I'd need to see details of the class contained by the list to comment
Was This Post Helpful? 0
  • +
  • -

#24 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,842
  • Joined: 25-December 13

Re: sorting price in ascending order

Posted 02 June 2019 - 04:05 AM

The print statements are not showing the values of the variables so you can know what the computer sees when the code is executed.
Line 3 should show the value of srcB
There should be a print statement on line 4 that shows the value returned by the getBed() method.
Was This Post Helpful? 0
  • +
  • -

#25 arexxa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-May 19

Re: sorting price in ascending order

Posted 02 June 2019 - 07:27 AM

View Postg00se, on 02 June 2019 - 03:37 AM, said:

I'd need to see details of the class contained by the list to comment


which class that you want to see? is it linkedList or test class?

View PostNormR, on 02 June 2019 - 04:05 AM, said:

The print statements are not showing the values of the variables so you can know what the computer sees when the code is executed.
Line 3 should show the value of srcB
There should be a print statement on line 4 that shows the value returned by the getBed() method.

i dont understand what you mean? on line 4? is it the line where the if statement?
Was This Post Helpful? 0
  • +
  • -

#26 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,842
  • Joined: 25-December 13

Re: sorting price in ascending order

Posted 02 June 2019 - 07:54 AM

Change line 3 to print the value of srcB.
Add a print statement after line 3 that shows the value returned by the getBed() method.
Then you will know what values are being compared in the statement on line 4.
Was This Post Helpful? 0
  • +
  • -

#27 arexxa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-May 19

Re: sorting price in ascending order

Posted 02 June 2019 - 08:27 AM

        temp = (bedMattress)bedMatt.getFirst();
        String srcM = JOptionPane.showInputDialog("Search for mattress: ");
        System.out.println("\nSearching for mattress selected....");
        if(temp.getMattress().equalsIgnoreCase(srcM)){
            bedMatt.mattDetail(srcM);
        }
        else{
            System.out.println("NOT AVAILABLE....");
        }


by using this code?
Was This Post Helpful? 0
  • +
  • -

#28 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,842
  • Joined: 25-December 13

Re: sorting price in ascending order

Posted 02 June 2019 - 08:39 AM

That code needs to be changed to print out values for debugging so you can see what the computer sees when it executes the if statement on line 4.
Where does the code print the contents of srcM?
Where does the code print the value returned by getMattress()?
Also what is the value in temp? Print it so you can see.
Was This Post Helpful? 0
  • +
  • -

#29 arexxa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-May 19

Re: sorting price in ascending order

Posted 02 June 2019 - 08:44 AM

so i need to display first the content in temp then do searching method?
the code print the contents of srcM is in the mattDetail() in the linkedList.
Was This Post Helpful? 0
  • +
  • -

#30 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,842
  • Joined: 25-December 13

Re: sorting price in ascending order

Posted 02 June 2019 - 08:48 AM

I thought there was a problem because the if statement on line 4 was not true when it was supposed to be true. To see why the if statement is not true, print out the values of the two variables whose contents are being compared. That will help you understand why.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3