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++;
}
}
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?
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.
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)
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: http://www.javaprogrammingforums.com...n-feeding.html
Re: i could use some help please!!!
Quote:
Originally Posted by
CSUTD
You wrote your for loop wrong.
instead of this
Code :
for (int count=0; count<=exNum
{
System.out.print(input.charAt(0));
count++;
}
it should be this
Code :
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.
Re: i could use some help please!!!
Quote:
Originally Posted by
CSUTD
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.