Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Input/Output help if possible

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Input/Output help if possible

    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.

    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;
        }
    An example of the output i receive could be this :
    "
    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 line
     System.out.printf("spaces in : %s is : %d",
                                                           place, countCh( place, ' ' ) );
    TO
    System.out.printf("[%s %d] spaces in ",
                                                           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.


    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 this
    System.out.printf("[ %d] spaces in %s ",
                                                           place, countCh( place, ' ' ) );
    Which gave me this - java.util.IllegalFormatConversionException: d != java.lang.String
    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 >.<
    Last edited by rossonomous; December 13th, 2011 at 07:28 AM.

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Input/Output help if possible

    Recommended reading: Formatter (Java Platform SE 6)

    That should help you decipher what's going into printf. If you're still having trouble, I suggest you throw together a really basic SSCCE (possibly only one line in a main method in a class) that demonstrates what you're working with.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Input/Output help if possible

    Apologies but scanning through that I'm not really sure how it helps me. I thought the solution to my situation would be a fairly simple one that I was missing? And with the proper solution I would be able to understand where I was wrong and this would lead to me understanding the code line as a whole. I am not just here to have my work finished for me.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Input/Output help if possible

    Well, your exception is caused because you're passing an int when you've specified that you'd pass a String instead. You're not using the printf function correctly, which makes me think that you don't understand format Strings (heck, half the time I need a reminder). That link explains how to use format Strings. If you read through that link and still have questions, I really suggest narrowing the problem down to an SSCCE. I'm betting that what you're doing wrong will become more obvious to you in the process of creating the SSCCE.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Input/Output help if possible

    Thankyou kevin with a little bit of work and your further reading i have understood where the problem is and how it is a problem. I'm just gonnalayout in my words how i see the problem now and could you just confirm if i am correct?

    So
    %s is for outputting a string
    %d is for outputting an integer
    So when i tried to do this line System.out.printf("[ %d] spaces in %s ",place, countCh( place, ' ' ) );

    %d = place
    %s = countCh(place, ' '
    issue - because place is the first argument it would try to output that first. Because %d is for integers and place is a string, i kept getting an error saying i can't output an integer as a string so it was crashing.

    So i had to change the ordering of the statement so it read it in correctly.
    Which lead me to this line as final - System.out.printf("[ %d] spaces in %s ", countCh( place, ' ' ), place, );

  6. #6
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Input/Output help if possible

    Also one last thing if you could provide a hand. I'm 95% complete with this program now however the last little thing is.

    How can i make the output of
    [ 3] spaces in line 1 test test
    have line 1 test test in quotation marks. When i try to add them to the line where i thought they may go i am asked for closing brackets and cannot compile.
    So it would look like this 100% complete - [ 3] spaces in "Line 1 test test"

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Input/Output help if possible

    That's because String literals are indicated by quotation marks, so when the compiler reaches the second quotation mark, it thinks you want to close the String, and it can't make sense of the rest of it.

    The solution is to escape the quotation marks inside the String. You do this by putting a backslash before the quotation mark you want to ignore:

    String escaped = "This String contains \"escaped\" quotation marks!"
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Input/Output Sequence
    By JoseGuad in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: August 30th, 2011, 07:50 AM
  2. Input output file help
    By peteyfresh12 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 08:44 AM
  3. [SOLVED] T_T [hellllppppp] Input/Output
    By bontet in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: October 29th, 2010, 12:32 PM
  4. Input/Output file help
    By Plural in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 25th, 2010, 09:34 PM
  5. Input/Output file help
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 07:26 PM