Results 1 to 6 of 6
Thread: coding help
- 11-12-2008, 06:59 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
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
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.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'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
- 11-12-2008, 07:05 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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.
- 11-12-2008, 07:13 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
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.
- 11-12-2008, 07:40 PM #4
put testString = scan.next(); before you initiliaze len. len is trying to find the length of testString before it has been given a length.
- 11-12-2008, 07:52 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
ah ok, thanks.
- 11-12-2008, 08:15 PM #6
Member
- Join Date
- Nov 2007
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
Coding Java on a Mac
By Menre in forum New To JavaReplies: 5Last Post: 05-06-2008, 05:49 AM -
Need Help Coding Project
By Bascotie in forum New To JavaReplies: 9Last Post: 04-27-2008, 11:31 PM -
Help On Coding problem
By mandrake446 in forum New To JavaReplies: 3Last Post: 12-08-2007, 07:01 AM -
Error in my coding
By one198 in forum New To JavaReplies: 2Last Post: 10-13-2007, 05:07 AM -
Help with program coding
By cachi in forum AWT / SwingReplies: 1Last Post: 07-31-2007, 07:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks