Results 1 to 5 of 5
  1. #1
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Need help with strings please!!!!

    I need to make an abbreviation using substrings of 3 seperate strings, here is what i have so far and i am recieving a response of null:
    Java Code:
    import java.util.*;
    
       public class NewCar
       {
          String carDescr;
          String carAbbr;
         private double carPrice;
         private double salesTax;
         private double discount;
      
          public NewCar(String year, String manfr, String model)
          {
             String carDescr = year + manfr + model;
    			String carAbbr = year.substring(2, 4) + manfr.substring(0, 1) + model.substring(0, 1);
             carPrice = 0;
             salesTax = 0;
             discount = 0;
          }
      
         public String getcarAbbr()
         {	
            return carAbbr;
          }
       }
    Java Code:
    public class NewCarTester
       {
          public static void main(String[] args)
          {
             NewCar car = new NewCar("2010 ", "Chevrolet", "Cobalt");
             System.out.println("car abbreviation: "+ car.getcarAbbr());
          }
       }
    ----jGRASP exec: java NewCarTester

    car abbreviation: null

    ----jGRASP: operation complete.
    Last edited by ndsmith20; 10-24-2012 at 10:53 PM. Reason: add code tags

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: Need help with strings please!!!!

    Here's why:
    Java Code:
          String carAbbr;
          public NewCar(String year, String manfr, String model)
          {
                String carAbbr = year.substring(2, 4) + manfr.substring(0, 1) + model.substring(0, 1);
          }
    You're actually creating a new String called carAbbr in the constructor, instead of setting the class-wide String to that value. Remove the String part in the constructor, and you should be good.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Need help with strings please!!!!

    You're redeclaring the carAbbr in the constructor instead of assigning a value to the instance variable. Therefor, when the constructor completes, the new carAbbr isn't saved anywhere. Removed the extra String declaration from the constructor and just do the assignment.

  4. #4
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Need help with strings please!!!!

    Thank you, that i worked....I did not realize that would make such a big difference with the result. You guys are the best on this forum.

  5. #5
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: Need help with strings please!!!!

    Glad to be able to help!

Similar Threads

  1. Strings
    By Krazzyjman in forum New To Java
    Replies: 3
    Last Post: 09-07-2012, 08:14 PM
  2. Strings
    By leepikamukharji in forum New To Java
    Replies: 12
    Last Post: 04-18-2011, 01:43 PM
  3. Need help with Strings
    By </3java in forum New To Java
    Replies: 24
    Last Post: 02-17-2011, 05:00 PM
  4. Strings
    By zoala001 in forum Java Applets
    Replies: 9
    Last Post: 01-02-2011, 10:26 AM
  5. It is possible in Strings..?
    By mlibot in forum New To Java
    Replies: 1
    Last Post: 03-12-2010, 05:30 AM

Posting Permissions

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