Results 1 to 20 of 36
 01202010, 03:55 AM #1Member
 Join Date
 Jan 2010
 Posts
 10
 Rep Power
 0
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;
}
}
 01202010, 04:05 AM #2Member
 Join Date
 Jan 2010
 Location
 Wisconsin
 Posts
 20
 Rep Power
 0
I think you need numberOfDigits2() with the () after it.
 01202010, 04:08 AM #3Member
 Join Date
 Jan 2010
 Posts
 10
 Rep Power
 0
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
 01202010, 04:11 AM #4Member
 Join Date
 Jan 2010
 Posts
 10
 Rep Power
 0
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
 01202010, 04:13 AM #5
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 19
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); }
 01202010, 04:17 AM #6Member
 Join Date
 Jan 2010
 Posts
 10
 Rep Power
 0
 01202010, 04:20 AM #7
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 19
Java Code:class NumberOfDigits2
Java Code:System.out.print("The Number Of Digits Is " + NumberOfDigits2);
Java Code:System.out.print("The Number Of Digits Is " + 100);
 01202010, 04:21 AM #8Member
 Join Date
 Jan 2010
 Location
 Wisconsin
 Posts
 20
 Rep Power
 0
n doesn't have a value?That might be a problem to.
 01202010, 04:24 AM #9
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 19
Yes, that's the second mistake our OP did in his code. Lets solve it one by one.
 01202010, 04:28 AM #10Member
 Join Date
 Jan 2010
 Location
 Wisconsin
 Posts
 20
 Rep Power
 0
Ok, i changed the method NumberOfDigits2() to NumberOfDigits2;
and now i recieve this error on compile...
 01202010, 04:29 AM #11Member
 Join Date
 Jan 2010
 Posts
 10
 Rep Power
 0
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
 01202010, 04:30 AM #12
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 19
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.
 01202010, 04:31 AM #13
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 19
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; } }
 01202010, 04:32 AM #14Member
 Join Date
 Jan 2010
 Location
 Wisconsin
 Posts
 20
 Rep Power
 0
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; } }
 01202010, 04:33 AM #15Member
 Join Date
 Jan 2010
 Location
 Wisconsin
 Posts
 20
 Rep Power
 0
That's why it's good practice to use intend in coding.
 01202010, 04:33 AM #16Member
 Join Date
 Jan 2010
 Posts
 10
 Rep Power
 0
I see, so now no matter what string is entered it will always print 100
 01202010, 04:34 AM #17
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 19
 01202010, 04:34 AM #18Member
 Join Date
 Jan 2010
 Posts
 10
 Rep Power
 0
 01202010, 04:36 AM #19
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 19
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.
 01202010, 04:36 AM #20Member
 Join Date
 Jan 2010
 Location
 Wisconsin
 Posts
 20
 Rep Power
 0
Similar Threads

Array count number Occurances
By gwithey in forum New To JavaReplies: 2Last Post: 04172009, 08:34 PM 
how to use Scanner with a number
By cew27 in forum New To JavaReplies: 10Last Post: 04032009, 06:23 PM 
How to count the number of specific object in a Vector collection?
By johnsienk in forum New To JavaReplies: 4Last Post: 03282009, 04:58 PM 
[SOLVED] How to count the number of words in a string
By andy5605 in forum New To JavaReplies: 8Last Post: 02042009, 08:55 PM 
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11202007, 04:59 PM
Bookmarks