Page 1 of 2 12 LastLast
Results 1 to 20 of 36
  1. #1
    wendysbiggy is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default Count number of digits in string using scanner

    can someone please let me know what I need to change and why. I have been looking at it for a little while and cannot figure it out on my own. Thank You.

    import java.util.Scanner;

    class NumberOfDigits2
    {
    public static void main(String args[])
    {
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter a Number!");
    in.nextLine();

    System.out.print("The Number Of Digits Is " + NumberOfDigits2);
    }
    public static int NumberOfDigits2()
    {
    int n;
    if(n<0) n = -n;
    int c = 1;
    while(n > 9)
    {
    c++;
    n = n/10;
    }
    return c;
    }
    }

  2. #2
    justin1980 is offline Member
    Join Date
    Jan 2010
    Location
    Wisconsin
    Posts
    20
    Rep Power
    0

    Default

    I think you need numberOfDigits2() with the () after it.

  3. #3
    wendysbiggy is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default

    My apologies i should have listed what the error was on compile... here it is...

    NumberOfDigits2.java:12: cannot find symbol
    symbol : variable NumberOfDigits2
    location: class NumberOfDigits2
    System.out.print("The Number Of Digits Is " + NumberOfDigits2);
    ^
    1 error

  4. #4
    wendysbiggy is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default

    Ok, i changed the method NumberOfDigits2() to NumberOfDigits2;
    and now i recieve this error on compile...

    NumberOfDigits2.java:24: return outside method
    return c;
    ^
    1 error

    ----jGRASP wedge2: exit code

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Java Code:
    class NumberOfDigits2
    {
    public static void main(String args[])
    {
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter a Number!");
    in.nextLine();
    
    System.out.print("The Number Of Digits Is " + NumberOfDigits2);
    }
    Your class name, use it in the print() method. It's not legal.

  6. #6
    wendysbiggy is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Java Code:
    class NumberOfDigits2
    {
    public static void main(String args[])
    {
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter a Number!");
    in.nextLine();
    
    System.out.print("The Number Of Digits Is " + NumberOfDigits2);
    }
    Your class name, use it in the print() method. It's not legal.
    Sorry, I dont understand what you mean. can you explain please. Thanks

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Java Code:
    class NumberOfDigits2
    This is the class definition, NumberOfDigits2 is the name of your class. Hope you know that well.

    Java Code:
    System.out.print("The Number Of Digits Is " + NumberOfDigits2);
    This is the system class' print() method. You pass the class name to there. It's not a valid argument to print() method. For a min, on that print() method line change as follows and see.

    Java Code:
    System.out.print("The Number Of Digits Is " + 100);

  8. #8
    justin1980 is offline Member
    Join Date
    Jan 2010
    Location
    Wisconsin
    Posts
    20
    Rep Power
    0

    Default

    n doesn't have a value?That might be a problem to.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  10. #10
    justin1980 is offline Member
    Join Date
    Jan 2010
    Location
    Wisconsin
    Posts
    20
    Rep Power
    0

    Default

    Ok, i changed the method NumberOfDigits2() to NumberOfDigits2;
    and now i recieve this error on compile...
    No, don't change it to numberOfDigits; change the method to a new name like digits() and have the () after both

  11. #11
    wendysbiggy is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default

    Ok so i wrote...
    System.out.print("The Number Of Digits Is " + 100);
    }
    /*public static int NumberOfDigits2;
    {
    int n;
    if(n<0) n = -n;
    int c = 1;
    while(n > 9)
    {
    c++;
    n = n/10;
    }
    return c;
    }*/
    }

    and it will print "The Number of Digits is 100"...
    if i uncomment the last method i recieve "return outside method" error

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    No need to change the method name all the time, except if it's constructor. Actual error we have here is the user the class name as a parameter and variable n is not initialize.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by wendysbiggy View Post
    if i uncomment the last method i recieve "return outside method" error
    That's why it's good practice to use intend in coding.

    Java Code:
    import java.util.Scanner;
    
    class NumberOfDigits2 {
    	public static void main(String args[]) {
    		Scanner in = new Scanner(System.in);
    		System.out.print("Please enter a Number!");
    		in.nextLine();
    
    		System.out.print("The Number Of Digits Is " + 100);
    	}
    
    	public static int NumberOfDigits2() {
    		int n = 0;
    		if(n<0) {
     			n = -n;
    		}
    
    		int c = 1;
    		while(n > 9) {
    			c++;
    			n = n/10;
    		}
    		return c;
    	}
    }

  14. #14
    justin1980 is offline Member
    Join Date
    Jan 2010
    Location
    Wisconsin
    Posts
    20
    Rep Power
    0

    Default

    Methods have to have these () after them. and n needs a value so try something like this.
    Java Code:
           
    
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter a Number!");
    in.nextLine();
    
    System.out.print("The Number Of Digits Is " + Digits2());
    
     
    }
      public static int Digits2()
    {
    int n = 77;
    if(n<0) n = -n;
    int c = 1;
    while(n > 9)
    {
    c++;
    n = n/10;
    }
    return c;
    
    }
    }
    That still will not count the letters in a string or whatever you are trying to do, but I think there wont be any errors. What is it exactly you are trying to do?

  15. #15
    justin1980 is offline Member
    Join Date
    Jan 2010
    Location
    Wisconsin
    Posts
    20
    Rep Power
    0

    Default

    That's why it's good practice to use intend in coding.
    What does intend mean?

  16. #16
    wendysbiggy is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default

    I see, so now no matter what string is entered it will always print 100

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by justin1980 View Post
    That still will not count the letters in a string or whatever you are trying to do, but I think there wont be any errors. What is it exactly you are trying to do?
    Actually our OPs code not doing anything yet. He tries to count number of digits in the user specifies number, but not doing it in proper way.

  18. #18
    wendysbiggy is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by justin1980 View Post
    What does intend mean?
    I believe he meant Indent! haha

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by wendysbiggy View Post
    I see, so now no matter what string is entered it will always print 100
    That's the different thing pal. I just ask you to do it point out the error is.

    First you get the user input and seem that you tried to count the number of digit in a different method. So you have to call the relevant method with the correct parameter. That method return value print to the console. That what you've to do basically.

  20. #20
    justin1980 is offline Member
    Join Date
    Jan 2010
    Location
    Wisconsin
    Posts
    20
    Rep Power
    0

    Default

    Originally Posted by justin1980 View Post
    What does intend mean?
    I believe he meant Indent! haha
    Oh duh, I'm sorry!

Page 1 of 2 12 LastLast

Similar Threads

  1. Array count number Occurances
    By gwithey in forum New To Java
    Replies: 2
    Last Post: 04-17-2009, 09:34 PM
  2. how to use Scanner with a number
    By cew27 in forum New To Java
    Replies: 10
    Last Post: 04-03-2009, 07:23 PM
  3. Replies: 4
    Last Post: 03-28-2009, 05:58 PM
  4. Replies: 8
    Last Post: 02-04-2009, 09:55 PM
  5. Replies: 0
    Last Post: 11-20-2007, 05:59 PM

Tags for this Thread

Posting Permissions

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