Print 1 object from a Linked List

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JoeMac3313
    New Member
    • Jul 2007
    • 16

    Print 1 object from a Linked List

    I am trying to figure out how to print one element of a linked list.

    Code:
    public void printOne() throws IOException
    {
         System.out.println("Part stock number (press 0 to end): ")';
            stockNumber = conIn.nextInt();
            skip conIn.NextLine();
       
          /* I have tried creating a dummy object similiar to removing from a linked list 
             but I cannot get it to print
          */
        // the object takes an int, String, int, and double
        dummy = new Part(stockNumber, " " , 0, 0.0);
        part.toString(dummy);
    
    }
    how do I get an element frome the part list and print just the one element out?
  • sumittyagi
    Recognized Expert New Member
    • Mar 2007
    • 202

    #2
    Originally posted by JoeMac3313
    I am trying to figure out how to print one element of a linked list.

    Code:
    public void printOne() throws IOException
    {
         System.out.println("Part stock number (press 0 to end): ")';
            stockNumber = conIn.nextInt();
            skip conIn.NextLine();
       
          /* I have tried creating a dummy object similiar to removing from a linked list 
             but I cannot get it to print
          */
        // the object takes an int, String, int, and double
        dummy = new Part(stockNumber, " " , 0, 0.0);
        part.toString(dummy);
    
    }
    how do I get an element frome the part list and print just the one element out?
    override the toString function of Object class in your Part class, and print it as you wish.
    eg:-
    [code=java]
    class Part
    {
    private int stockNumber;
    private String desc;
    private int quantity;
    private double value;

    public Part(int stockNumber, String desc, int quantity, double value)
    {
    //copy parameters to instance variables or any other logic you want.
    }

    public String toString()
    {
    StringBuffer tempString = new StringBuffer("[Part:- stockNumber=");
    tempString.appe nd(stockNumber) ;
    tempString.appe nd("; desc=");
    tempString.appe nd(desc);
    .....
    tempString.appe nd("]");
    return tempString.toSt ring();
    }
    }
    [/code]

    Comment

    • JoeMac3313
      New Member
      • Jul 2007
      • 16

      #3
      Will try tomorrow, thanks!

      Comment

      • AZRebelCowgirl73
        New Member
        • Nov 2006
        • 47

        #4
        if you are still having problems let me know, I finished the program! Toodles! ALI :P

        Comment

        Working...