You are on page 1of 5

5/11/13

why wont this run (Beginning Java forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Java Beginning Java

Author

why wont this run


posted 4/9/2002 7:42 AM

tom brownlee Greenhorn Joined: Aug 14, 2001 Posts: 28

hi guys..i have this code to count a character within a given source file..why wont it run/(it doesnt seem to compile either) import java.io.IOException; class Main { public static void main( String args[] ) { try { int count = 0; int ch = System.in.read(); while ( ch == '/') { count++; ch = System.in.read(); } System.out.println( "Total number of characters: " + count ); } catch ( IOException err ) { System.out.println("Unexpected error on input"); } } } any help? also i have this code (below) for calculating the lines in a source code (any random source code) how can i combine both codes (above and below) to produce code that will calculate the amount of logical lines in a source code? ive asked b4, given it ago, but im stuck at this point..can i get the answer??? thanks guys for your time and effort

www.coderanch.com/t/391399/java/java/wont-run

1/5

5/11/13

why wont this run (Beginning Java forum at JavaRanch)

cheers tom
Marilyn de Queiroz Sheriff Joined: Jul 22, 2000 Posts: 9033

posted 4/9/2002 9:46 AM

Originally posted by tom brownlee: sorry guys i forgot to add this code to my question previously:
view plain c opy to c lipboard print ?

10 0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 .

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

i m p o r tj a v a . i o . * ; c l a s sL i n e 2 { p u b l i cs t a t i cv o i dm a i n ( S t r i n ga r g s [ ] ) { i f( a r g s . l e n g t h! =1 ) { S y s t e m . e r r . p r i n t l n ( " m i s s i n gf i l e n a m e " ) ; S y s t e m . e x i t ( 1 ) ; } t r y { F i l e R e a d e rf r=n e wF i l e R e a d e r ( a r g s [ 0 ] ) ; B u f f e r e d R e a d e rb r=n e wB u f f e r e d R e a d e r ( f r ) ; i n tc n t=0 ; w h i l e( b r . r e a d L i n e ( )! =n u l l ) c n t + + ; b r . c l o s e ( ) ; S y s t e m . o u t . p r i n t l n ( c n t ) ; } c a t c h( I O E x c e p t i o ne ) { S y s t e m . e r r . p r i n t l n ( e ) ; } } }

JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt Marilyn de Queiroz Sheriff Joined: Jul 22, 2000 Posts: 9033

posted 4/9/2002 9:48 AM

Originally posted by tom brownlee: just a thought...my previous question didnt ask one important thing.... how can i ask for a source code from the user to enable my code to be used. ive got this code..but how do i use it in cooperation with my other code?
view plain c opy to c lipboard print ?

10

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

i m p o r tj a v a . i o . * ; p u b l i cc l a s sR e a d S t r i n g { p u b l i cs t a t i cv o i dm a i n( S t r i n g [ ]a r g s ) { B u f f e r e d R e a d e rb r=n e wB u f f e r e d R e a d e r ( n e wI n p u t S t r e a m R e a d e r ( S y s t e m . i n ) ) ; 0 7 . S t r i n gu s e r N a m e=n u l l ; 0 8 . t r y 0 9 . { www.coderanch.com/t/391399/java/java/wont-run

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 .

2/5

5/11/13

why wont this run (Beginning Java forum at JavaRanch)

0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 .

{ u s e r N a m e=b r . r e a d L i n e ( ) ; } c a t c h( I O E x c e p t i o ni o e ) { S y s t e m . o u t . p r i n t l n ( " I Oe r r o rt r y i n gt or e a dy o u rn a m e ! " ) ; S y s t e m . e x i t ( 1 ) ; } S y s t e m . o u t . p r i n t l n ( " T h a n k sf o rt h en a m e ," +u s e r N a m e ) ; }

so i really suppose my entire question would be..how do i combine this code here, along with my code from my previous question to enable a user to enter a source code name and for the code to reply with the number of logical lines within that source code. make sense? once again thanks for the time on this guys. i really appreciate it. tom

Marilyn de Queiroz Sheriff Joined: Jul 22, 2000 Posts: 9033

posted 4/9/2002 9:48 AM

Tom, It's easier to answer your questions if you keep your questions regarding the same thing in the same thread rather than three separate threads. I've moved them all here for you this time. Just hit the reply button and add your code or alternatively, you can hit the edit button and add it into the same post. [ April 08, 2002: Message edited by: Marilyn deQueiroz ]

10

Marilyn de Queiroz Sheriff Joined: Jul 22, 2000 Posts: 9033

posted 4/9/2002 9:56 AM

Code #1 (class Main) will compile if you don't split your String between two lines. System.out.println("Unexpected error on input");

10 should be System.out.println("Unexpected error on input"); or System.out.println("Unexpected error on " + "input");


Marilyn de Queiroz Sheriff Joined: Jul 22, 2000 Posts: 9033

posted 4/9/2002 10:07 AM

Perhaps it would be better if you had three methods rather than three classes.

10

tom brownlee Greenhorn

posted 4/10/2002 5:28 AM

www.coderanch.com/t/391399/java/java/wont-run

3/5

5/11/13

Greenhorn Joined: Aug 14, 2001 Posts: 28

why wont this run (Beginning Java forum at JavaRanch)

sorry marilyn i think it must of been the way i wrote the code out...it should be on one line...with that solved it still doesnt compile. any reason why? and when i use methods instead of classes it still doesnt work? any idea why?

Rob Keefer Greenhorn Joined: Feb 18, 2002 Posts: 26

posted 4/11/2002 11:52 PM

Code #1 works for me ... Maybe you can give a bit more detail about the version of the compiler, os, compile error you get, etc. import java.io.IOException; class Main { public static void main( String args[] ) { try { int count = 0; int ch = System.in.read(); while ( ch == '/') { count++; ch = System.in.read(); } System.out.println( "Total number of characters: " + count ); } catch ( IOException err ) { System.out.println("Unexpected error on input"); } } } /Development/Java/tmp>javac Main.java /Development/Java/tmp>java Main /////// Total number of characters: 7

Rob Keefer Greenhorn Joined: Feb 18, 2002 Posts: 26

posted 4/12/2002 12:00 AM

Code #2 works for me also: /Development/Java/tmp>javac Line2.java /Development/Java/tmp>java Line2 Main.java 24 Again, more information would be helpful ...

I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.

subject: why wont this run

www.coderanch.com/t/391399/java/java/wont-run

Similar Threads

4/5

5/11/13

why wont this run (Beginning Java forum at JavaRanch)

Similar Threads Loop & System.in.read weirdness! parseInt( ) System.in.read() - Abstract method? int and char Checked Exceptions - What did I wrong?
All times above are in your local time zone & format.T he current ranch time (not your local time) is May 11, 2013 06:14:37 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/391399/java/java/wont-run

5/5

You might also like