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: i could use some help please!!!

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Location
    Pittsburgh PA
    Posts
    1
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default i could use some help please!!!

    okay so im kind of a newb to the programming world i have some self tought expierence and im now taking my first formal classes java and C++ but i can use some help with java im not asking anyone to do my home work for me i just need a bit of help. so my assignment is to "design and implement an application that reads a string from the user and prints it on char at a time"

    now heres my code i figure i could use a for loop to execute a certain number of times (how many characters the string is).
    The for loop can start with an marker variable of 0 and print the character at that marker's index value. So the first character. Then it adds 1 to marker. Then the for loop will repeat, but it should print the value at the 1 index. an it repeats until it's done x times, x being how many characters are in the string. i just got a little lost on the way...


    package stringBreakDown;

    import java.util.Scanner;
    public class StringBreakDown {


    public static void main(String[] args) {
    String input;
    float stringLength;
    float exNum;
    Scanner scan =new Scanner(System.in);

    System.out.println("please enter your string");
    input=scan.nextLine();

    stringLength=input.length();
    exNum=stringLength;


    for (int count=0; count<=exNum {
    System.out.print(input.charAt(0));
    count++;

    }


    }
    Alone in bad company.

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: i could use some help please!!!

    I'm going to move this to a more appropriate forum. The member introductions forum isn't really the place for technical questions.

    But your problem is with how you're using the charAt function. What does charAt(0) return?
    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
    Member
    Join Date
    Jul 2011
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: i could use some help please!!!

    Does it compile under javac? If not try building your program up a little bit at a time so you will have a better idea of where your program is breaking down. Annoying as it sounds, try writing one statement at a time, and toss in a gratuitous print as a diagnostic to see if you are doing what you think you are doing. One thing that looked suspicious to me was your for(){} loop. Once things are proven to work, feel free to remove the diagnostics.

    Since you are a complete beginner, may I say that you can expect your programs to drive you nuts before you get them to work, but when you fix it you feel really good. Best of luck.

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Red face Re: i could use some help please!!!

    I think your for loop is wrong. I changed something for your for loop, hope it will meet your requirment:

    (spoonfeeding deleted by KevinWorkman)
    Last edited by KevinWorkman; October 5th, 2011 at 07:07 AM. Reason: deleted spoonfeeding

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: i could use some help please!!!

    luck999, please do not post full code solutions like that. For an explanation of the problem with doing so, please read this: The Problem with Spoon-feeding
    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!

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: i could use some help please!!!

    Quote Originally Posted by CSUTD View Post
    You wrote your for loop wrong.

    instead of this

    for (int count=0; count<=exNum 
    {
       System.out.print(input.charAt(0));
       count++;
    }

    it should be this

    for ( int count = 0; count <= exNum; count++ )
    {
       System.out.print(input.charAt(0));
    }
    Wrong. Placing the increment in the loop body may not be stylistically common, but it should work fine. Did you even read the original post?

    Again, guys, please read the link above on the problems with spoon-feeding. I appreciate that you're trying to help, but supplying incorrect information is NOT helpful. Spoon-feeding is NOT helpful.
    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!

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: i could use some help please!!!

    Quote Originally Posted by CSUTD View Post
    Sorry, didn't consider that spoon feeding since OP already knows how to write a for loop. I know it will work that way but he didn't finish it.

    Yes, I read the post(didn't finish it). He is wanting to print out how many characters were in the string. The charAt(0) is wrong because the program doesn't want to return a string; the program wants to return how many characters are in the string...

    ex) If you type in the word "sports" the program should print out "6."
    Again, no, absolutely not, that's not the OP's goal at all. Please please please don't offer "help" if you don't even know what the OP is asking.
    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!