Results 1 to 20 of 36
- 01-20-2010, 03:55 AM #1
Member
- 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;
}
}
- 01-20-2010, 04:05 AM #2
Member
- Join Date
- Jan 2010
- Location
- Wisconsin
- Posts
- 20
- Rep Power
- 0
I think you need numberOfDigits2() with the () after it.
- 01-20-2010, 04:08 AM #3
Member
- 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
- 01-20-2010, 04:11 AM #4
Member
- 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
- 01-20-2010, 04:13 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Your class name, use it in the print() method. It's not legal.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); }
- 01-20-2010, 04:17 AM #6
Member
- Join Date
- Jan 2010
- Posts
- 10
- Rep Power
- 0
- 01-20-2010, 04:20 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
This is the class definition, NumberOfDigits2 is the name of your class. Hope you know that well.Java Code:class 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 " + NumberOfDigits2);
Java Code:System.out.print("The Number Of Digits Is " + 100);
- 01-20-2010, 04:21 AM #8
Member
- Join Date
- Jan 2010
- Location
- Wisconsin
- Posts
- 20
- Rep Power
- 0
n doesn't have a value?That might be a problem to.
- 01-20-2010, 04:24 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, that's the second mistake our OP did in his code. Lets solve it one by one.
- 01-20-2010, 04:28 AM #10
Member
- Join Date
- Jan 2010
- Location
- Wisconsin
- Posts
- 20
- Rep Power
- 0
No, don't change it to numberOfDigits; change the method to a new name like digits() and have the () after bothOk, i changed the method NumberOfDigits2() to NumberOfDigits2;
and now i recieve this error on compile...
- 01-20-2010, 04:29 AM #11
Member
- 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
- 01-20-2010, 04:30 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 01-20-2010, 04:31 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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; } }
- 01-20-2010, 04:32 AM #14
Member
- 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.
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?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; } }
- 01-20-2010, 04:33 AM #15
Member
- Join Date
- Jan 2010
- Location
- Wisconsin
- Posts
- 20
- Rep Power
- 0
What does intend mean?That's why it's good practice to use intend in coding.
- 01-20-2010, 04:33 AM #16
Member
- Join Date
- Jan 2010
- Posts
- 10
- Rep Power
- 0
I see, so now no matter what string is entered it will always print 100
- 01-20-2010, 04:34 AM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 01-20-2010, 04:34 AM #18
Member
- Join Date
- Jan 2010
- Posts
- 10
- Rep Power
- 0
- 01-20-2010, 04:36 AM #19
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 01-20-2010, 04:36 AM #20
Member
- 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: 04-17-2009, 08:34 PM -
how to use Scanner with a number
By cew27 in forum New To JavaReplies: 10Last Post: 04-03-2009, 06:23 PM -
How to count the number of specific object in a Vector collection?
By johnsienk in forum New To JavaReplies: 4Last Post: 03-28-2009, 04:58 PM -
[SOLVED] How to count the number of words in a string
By andy5605 in forum New To JavaReplies: 8Last Post: 02-04-2009, 08:55 PM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks