Hi there ladies and gents. Firstly I am very new to programming and unfortunately finding it hard to grasp the concept and it is not clicking with me as it does with others.
To bwgin with here is my code which works and does what I intend it to do, but the way I am reading it out is what i need help with.
An example of the output i receive could be this :class Main { public static void main(String args[]) { System.out.print( "#Enter line of text : "); String place = BIO.getString(); while ( ! place.equals("END") ) { System.out.printf("spaces in : %s is : %d", place, countCh( place, ' ' ) ); System.out.println(); System.out.print( "#Enter place name : " ); place = BIO.getString(); } } public static int countCh( String s, char c ) { String sLC = s.toLowerCase(); int count = 0; for ( int i = 0; i < sLC.length(); i++ ) { if ( sLC.charAt(i) == c ) count++; } return count; }
"
spaces in : Line 1 test test is : 3
spaces in : Line-2 is : 0
spaces in : Line 3 a b c d e f g is : 8
"
However, i would like to output it in a different format; being like this :
[ 3] spaces in "Line 1 test test"
[ 0] spaces in "Line-2"
[ 8] spaces in "Line 3 a b c d e f g"
EDIT
I just changed the lineTOSystem.out.printf("spaces in : %s is : %d", place, countCh( place, ' ' ) );
The last part is making it read out what is inputted at the end of that line. E.g [ 3] spaces in "line test test 1" Its just how i intergrate that last line; i have no idea why its confusing me because im sure its so simple. As i say below, ill keep playing with it in the meantime.System.out.printf("[%s %d] spaces in ", place, countCh( place, ' ' ) );
I know this is a very simple problem, but every time i change something i mess up my code somehow :<
I hope i have given enough details and thanks in advance to anyone that reads this. I will continue to play with it and if i get the results i want before i get a reply then ill post for this to be closed
Thanks
p.s BIO is just a simple input/output method used at my university which is tuned into our blueJ
p.p.s If anyone could reccomend some extremely good sites with tutorials and Java bits on that help for extreme beginners then please do
Further edit (apologies for all the extra reading, just want to show what i have tried and what i have not)
I just changed it to thisWhich gave me this - java.util.IllegalFormatConversionException: d != java.lang.StringSystem.out.printf("[ %d] spaces in %s ", place, countCh( place, ' ' ) );
at java.util.Formatter$FormatSpecifier.failConversion (Formatter.java:4045)
at java.util.Formatter$FormatSpecifier.printInteger(F ormatter.java:2748)
at java.util.Formatter$FormatSpecifier.print(Formatte r.java:2702)
at java.util.Formatter.format(Formatter.java:2488)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at Main.main(Main.java:17)
No idea what this means >.<