Results 1 to 10 of 10
  1. #1
    b.m
    b.m is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default error with String toString()

    Hi guys,
    i'm having problem with my method String toString()...
    The problem is that the program prints in the end a "null" value...

    This is my code...
    Java Code:
    import java.util.Scanner;
    
    public class Book {
        private String title;
        private String isbn;
        private String location;
        private String name;
        private String published;
        private static Scanner give=new Scanner(System.in);
        private String condition;
        private Book(String title,String isbn) {
            super();
            this.title=title;
            this.isbn=isbn;
            }
        private void getPublished(){
           System.out.println("When is it publish");
           published=give.nextLine();      
        }
        private void loanBook(String location,String name) {
            this.name=name;
            this.location=location;
        }
        private void returnBook() {
            String book;
            System.out.println("How was the condition before the loan?");
            book=give.nextLine();
            if(book.equals("new")){
            condition="used";}
            else if(book.equals("used")){
                condition="old";
            }
        }
        private String getISBN(){
            return isbn;
        }
        private void setISBN(String string) {
            this.isbn=string;
            }
        @Override
        public boolean equals(Object obj) {
            Book book = (Book)obj;
            if (this.isbn.equals(book.getISBN())){
                return true;}
            return false;
        }
    
        @Override
        public int hashCode() {
            int hash = 3;
            hash = 89 * hash + (this.isbn != null ? this.isbn.hashCode() : 0);
            return hash;
        }
        @Override
        public String toString(){
             return "Title <"+title + ">, written by " + name + " ,ISBN "+isbn+ ",\npublished "+published+" is in condition "+condition +" (and can be found "+location+")";
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    	Book myBook = new Book("Sensor-Actuator Supported Implicit Interaction in Driver Assistance Systems","3 8348 0963 6");
    	System.out.println(myBook.toString());
    	myBook.loanBook("Bookshelf","Andreas Riener");
    	System.out.println(myBook.toString());
    	myBook.returnBook();
            myBook.getPublished();
    	System.out.println(myBook.toString());
    	// Test if books are equal
    	Book myOtherBook = new Book("Sensor-Actuator Supported Implicit Interaction in Driver Assistance Systems","3 8348 0963 6");
    	System.out.println(myBook.equals(myOtherBook));
    	// erroneous ISBN number
    	myBook.setISBN("1-567-67878-1");
    	System.out.println(myBook.getISBN());
    	// correct ISBN number
    	myBook.setISBN("1-567-67878-5");
    	System.out.println(myBook.getISBN());
            }
    
    }
    And this is the output
    Java Code:
    Title <Sensor-Actuator Supported Implicit Interaction in Driver Assistance Systems>, written by null ,ISBN 3 8348 0963 6,
    published null is in condition null (and can be found null)
    Title <Sensor-Actuator Supported Implicit Interaction in Driver Assistance Systems>, written by Andreas Riener ,ISBN 3 8348 0963 6,
    published null is in condition null (and can be found Bookshelf)
    Give condition
    new
    When is it publish
    2010
    Title <Sensor-Actuator Supported Implicit Interaction in Driver Assistance Systems>, written by Andreas Riener ,ISBN 3 8348 0963 6,
    published 2010 is in condition used (and can be found Bookshelf)
    true
    1-567-67878-1
    1-567-67878-5
    Can somebody tell me why and help me figure it out....It's just that i don't want that "null" value to show out in the output...:confused:

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Well, that means "name" is null. What did you expect? Now, what you can do, is to check all the values before adding them to the "output" and adding something else instead, when they're null, or you could make sure that they all have values. Your choice, but "null" in the output String means the string in that position was null.

  3. #3
    b.m
    b.m is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    yes, but i don't want to print that null value....

    is there anyway i can print one after one the sentences without that 'null' value
    for exp.

    The first sentence just:
    Java Code:
    Title <Sensor-Actuator Supported Implicit Interaction in Driver Assistance Systems>,ISBN 3 8348 0963 6
    2nd:
    Java Code:
    Title <Sensor-Actuator Supported Implicit Interaction in Driver Assistance Systems>,written by Andreas Riener ,ISBN 3 8348 0963 6,(and can be found Bookshelf)
    and the last :
    Java Code:
    Title <Sensor-Actuator Supported Implicit Interaction in Driver Assistance Systems>, written by Andreas Riener ,ISBN 3 8348 0963 6,
    published 2010 is in condition used (and can be found Bookshelf)

  4. #4
    SmilingKey is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default

    If you do that I think you'll avoid name null value

    public String toString(){
    if (name==null){
    name="";
    }

    return "Title <"+title + ">, written by " + name + " ,ISBN "+isbn+ ",\npublished "+published+" is in condition "+condition +" (and can be found "+location+")";
    }

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by SmilingKey View Post
    If you do that I think you'll avoid name null value

    public String toString(){
    if (name==null){
    name="";
    }

    return "Title <"+title + ">, written by " + name + " ,ISBN "+isbn+ ",\npublished "+published+" is in condition "+condition +" (and can be found "+location+")";
    }
    While creating a "hole" in the output (ie. written by ....nobody?)

    @OP, like I said check the values. No one ever said the toString should be a one-liner. Use a StringBuilder (or even just String concatenation) and check each of the values before adding it (and its prefix) to the result String.

    i.e.
    Java Code:
    public String toString() {
      String result = "";
      if (str1 != null) {
        result += "some prefix " + str1 + " ";
      }
      if (str2 != null) {
        result += "some other prefix " + str2;
      }
      return result;
    }
    I had already told you to check the values, BTW. No offense intended but you need to develop more than a bit more initiative and creativity if you wish to be developer.

  6. #6
    b.m
    b.m is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    thank u people for u'r effort on answering so fast....:)

    And yes i know that i need to develop more my creativity, but i'm in the beginning and i'm working hard on it.....thnx for the tips,,,,,and no offense taken...actually i like to hear others opinions....;)

  7. #7
    b.m
    b.m is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    hey i tried to do this with the String concatenation method but i think i'm missing sth...

    here is my code for the String toString()
    Java Code:
     public String toString(){
            String result = "";
            String str1="Title <"+title + ">,ISBN "+isbn+"";
            String str2="Title <"+title + ">, written by " + name + " ,ISBN "+isbn+ ",can be found "+location+"";
            String str3="Title <"+title + ">, written by " + name + " ,ISBN "+isbn+ ",\npublished "+published+" is in condition "+condition +" (and can be found "+location+")";
            if(str1!=null){
                result+=str1;
            }
           if(str2 != null)
            {
              result+=str2;
          }
          if(str3 !=null)
            {
              result+=str3;
          }
            
            return result;
       }
    but still in the output, even that i modified the code, now i receive this

    Java Code:
    run:
    Title <Sensor-Actuator Supported Implicit Interaction in Driver Assistance Systems>,ISBN 3 8348 0963 6Title <Sensor-Actuator Supported Implicit Interaction in Driver Assistance Systems>, written by null ,ISBN 3 8348 0963 6,can be found nullTitle <Sensor-Actuator Supported Implicit Interaction in Driver Assistance Systems>, written by null ,ISBN 3 8348 0963 6,
    published null is in condition null (and can be found null)
    Title <Sensor-Actuator Supported Implicit Interaction in Driver Assistance Systems>,ISBN 3 8348 0963 6Title <Sensor-Actuator Supported Implicit Interaction in Driver Assistance Systems>, written by Andreas Riener ,ISBN 3 8348 0963 6,can be found BookshelfTitle <Sensor-Actuator Supported Implicit Interaction in Driver Assistance Systems>, written by Andreas Riener ,ISBN 3 8348 0963 6,
    published null is in condition null (and can be found Bookshelf)
    How was the condition before the loan?
    new
    When is it publish
    2010
    Title <Sensor-Actuator Supported Implicit Interaction in Driver Assistance Systems>,ISBN 3 8348 0963 6Title <Sensor-Actuator Supported Implicit Interaction in Driver Assistance Systems>, written by Andreas Riener ,ISBN 3 8348 0963 6,can be found BookshelfTitle <Sensor-Actuator Supported Implicit Interaction in Driver Assistance Systems>, written by Andreas Riener ,ISBN 3 8348 0963 6,
    published 2010 is in condition used (and can be found Bookshelf)
    true
    1-567-67878-1
    1-567-67878-5
    The problem now is that it prints itself more then once....:confused:

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Okay, again lets say you want to produce the String
    Java Code:
    Mary had a little lamb.
    where the values "had" and "little" are supplied by variables named "action" and "attribute"
    you would do
    Java Code:
    public class Dittle {
        String action;
        String attribute;
    
        public Dittle() {
        }
    
        public Dittle(String action) {
            this.action = action;
        }
    
        public Dittle(String attribute) {
            this.attrribute = attribute;
        }
    
        public Dittle(String action, String attribute) {
            this.action = action;
            this.attrribute = attribute;
        }
    
        public String toString() {
            String result = "";
            if (action != null) {
                result += "Mary " + action + " ";
            }
            if (attribute != null) {
                result += "a " + attribute + " lamb.";
            }
            return result;
        }
    }
    Then if "action" is null it would produce "a little lamb."
    and if "attribute" were null it would produce "Mary had "
    and if both were null it would produce an empty String (since none of the objects attributes would be set, in that case, that would be a plausible result).

    Now, do you see how yours differs from that?
    Last edited by masijade; 12-15-2010 at 07:53 AM. Reason: made a complete class to better illustrate

  9. #9
    b.m
    b.m is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Thanks again, but i already finished with that program...
    i still am a bit confused about this StringBuilder but i'll figure it out....;)

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    With StringBuilder you simply use the append method in every place where you currently use "+" or "+=" during String concatenation. The difference is that append is a single atomic action, the "+" or "+=" in String Concatenation goes as follows:

    -- create a StringBuffer (and this is synched making it slower, but safer, than StringBuilder) using the String's current content
    -- append the next value to the StringBuffer
    -- extract a new String object from the StringBuffer

    So, as you can see, a StringBuffer (essentially the same as StringBuilder, but synched) is used anyway, but it needs to created, appended to, and a String extracted from it for every String concatenation operation, whereas when you use StringBuilder yourself it is only a series of appends, and a single String extraction (at the end using toString()) without any synching (which is not necessary in such an atomic method call as this using a local StringBuilder). In end effect much faster, relatively, but, overall, with some exceptions, negligible unless used literally millions of times in a short run time.

Similar Threads

  1. Replies: 10
    Last Post: 12-07-2010, 12:29 PM
  2. toString() method error?
    By blueduiker in forum New To Java
    Replies: 6
    Last Post: 02-07-2010, 03:19 AM
  3. String error
    By Jlin787 in forum New To Java
    Replies: 1
    Last Post: 01-25-2009, 06:14 PM
  4. Replies: 2
    Last Post: 09-18-2008, 07:30 AM
  5. Error: convert from String to long
    By bbq in forum New To Java
    Replies: 1
    Last Post: 06-29-2007, 07:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •