Results 1 to 6 of 6

Thread: coding help

  1. #1
    accies76 is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default coding help

    hi, just started using java but got stuck so thought i'd look on google for help! hope this is the right place to post this.

    this is my code

    Java Code:
    public static void main(String[] args) {
    
       Scanner scan = new Scanner(System.in);
    
     
       String testString;
       int len = testString.length();
    
       
       System.out.println("Enter info ");
       testString = scan.next();
       
       System.out.println("The length of the string is: " + len);
    I want the user to enter a word(s) and my program to be able to read that and output how many numbers are in that word. I can do it but entering a word into the program code myself but i cant do it using scanner.

    I'm not sure how to go about it, have tried a few things myself but none work.

    I know that is the problem area.

    String testString;
    int len = testString.length();

    thanks

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    testString isn't initialized yet, therefore it cannot have a length, giving you an error. Switch your "int len ... " to be after you set testString.

  3. #3
    accies76 is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    thanks for the reply.

    i did initialize it as "null" initially but that gave the error "run:
    Exception in thread "main" java.lang.NullPointerException" in regards to "int len...".

    so thats what I don't know, what to initialize teststring as.

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    put testString = scan.next(); before you initiliaze len. len is trying to find the length of testString before it has been given a length.

  5. #5
    accies76 is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    ah ok, thanks.

  6. #6
    biswarup_java is offline Member
    Join Date
    Nov 2007
    Posts
    1
    Rep Power
    0

    Default

    you cannot invoke a method on a null object. thts why it threw tht nullpointerexception when u initialized 'testString' wid null n thn on the next line tried to invoke the 'length();' method in the line :
    int len = testString.length();

Similar Threads

  1. Coding Java on a Mac
    By Menre in forum New To Java
    Replies: 5
    Last Post: 05-06-2008, 05:49 AM
  2. Need Help Coding Project
    By Bascotie in forum New To Java
    Replies: 9
    Last Post: 04-27-2008, 11:31 PM
  3. Help On Coding problem
    By mandrake446 in forum New To Java
    Replies: 3
    Last Post: 12-08-2007, 07:01 AM
  4. Error in my coding
    By one198 in forum New To Java
    Replies: 2
    Last Post: 10-13-2007, 05:07 AM
  5. Help with program coding
    By cachi in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 07:16 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
  •