string.length()
hi. my program is asking a user to enter a word. say for instance he entered "helloworld" i want helloworld to be printed 10 times. another eg if a user enters bachi it should print bachi 5 times
bachi
bachi
bachi
bachi
bachi
this is my code i can't understand the concept of string.length() my program outputs the number the word and the number of length but i wopuld like to print the string instead of number.
import java.util.Scanner;
class repeat{
publicstaticvoid main(String[] args){
String inputString;
int times;
Scanner scan =new Scanner(System.in);
System.out.println("Enter a word ");
inputString = scan.nextLine();
times = inputString.length();
while( inputString.length() > times);{
System.out.println(times);
}
}
}
i played around alot changed inputString.length() to < and etc etc but still not working what am i doing wrong?/.
sample output
Enter a word
hello
5
[1496 byte] By [
fastmikea] at [2007-11-26 12:29:44]

> hi. my program is asking a user to enter a word. say
> for instance he entered "helloworld" i want
> helloworld to be printed 10 times. another eg if a
> user enters bachi it should print bachi 5 times
> bachi
> bachi
> bachi
> bachi
> bachi
> this is my code i can't understand the concept of
> string.length() my program outputs the number the
> word and the number of length but i wopuld like to
> print the string instead of number.
> > import java.util.Scanner;
> class repeat {
> public static void main(String[] args) {
> String inputString;
> int times;
> Scanner scan = new Scanner(System.in);
> System.out.println("Enter a word ");
> inputString = scan.nextLine();
>times = inputString.length();
> while( inputString.length() > times); {
>
> System.out.println(times);
> }
>
> }
> }
>
> i played around alot changed inputString.length() to
> < and etc etc but still not working what am i doing
> wrong?/.
> sample output
> > Enter a word
> hello
> 5
>
well, first off you're printing out the length of the string, not the string itself. Second, use a for loop not a while loop. Here's an example for loop
for ( int i = 0; i < 5; i++ )
{
System.out.println( "example" );
}
> hi. my program is asking a user to enter a word. say
> for instance he entered "helloworld" i want
> helloworld to be printed 10 times. another eg if a
> user enters bachi it should print bachi 5 times
> bachi
> bachi
> bachi
> bachi
> bachi
> this is my code i can't understand the concept of
> string.length() my program outputs the number the
> word and the number of length but i wopuld like to
> print the string instead of number.
> [co de]
> import java.util.Scanner;
> class repeat {
> public static void main(String[] args) {
> String inputString;
> int times;
> Scanner scan = new Scanner(System.in);
> System.out.println("Enter a word ");
> inputString = scan.nextLine();
>times = inputString.length();
this is fine, store the length of inputstring in a variable (not neccessary, but ok)
> while( inputString.length() > times); {
This loop should never work, since inputString.length() will always equal times, and will never go down (I know, that's not really true, if you do some fancy footwork, but go with me on this). Also, the semi-colon means end of the loop, so if inputString.length(0 was larger than times, this would loop forever. Use a for loop
>
> System.out.println(times);
Your are just printing the value of times here, which is the length of the inputString
> }
>
> }
> }
> [/code]
> i played around alot changed inputString.length() to
> < and etc etc but still not working what am i doing
> wrong?/.
> sample output
> > Enter a word
> hello
> 5
>
public static void main(String[] args) {
String inputString;
int times;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a word ");
inputString = scan.nextLine();
times = inputString.length();
for(int i = 0; i < times; i++)
{
System.out.println(inputString);
}
}
no not with a for loop i have not gone till that chapter i knew i could have done that easily with a for loop but i wanted to do with a while loop.
for(int i=0; i<string.length(); i++) {
System.out.println(inputString.length);
}
but i wanted a while loop. and it depends on the size of the string. like bachi has 5 char so it should print bachi 5 times. madarchod has 9 char so it should print madarchod 9 times.
Message was edited by:
fastmike>
int times = inputString.length();while(times > 0){System.out.println(inputString);times--;}
i am learning java and i am good and i like and i love it but i hate it a little bit also. gosh i tried manipulating different strategies changing this and that and came to know just one simple problem took you 3 min and took me 1 hr and 45 min lol. thanks someone else i appreciate your help. doing some exercise problems and not all the time but very few times i get stuck in this stupid little problems. Thanks
No problem. Don't get frustrated, I've been coding Java for years, and trust me, the easy stuff gets easier all the time. We've all been there, stuck on a problem, and had someone else look at the code and fix it in 2 seconds.
Sometimes, (always), when you are stuck and having a hard time finding a problem, or a solution, it is best just to walk away from the computer for a while, otherwise you'll go nuts. I recommend an ice cold beer for those occasions, it really gets the brain cells turning again.
~Tim
> No problem. Don't get frustrated, I've been coding
> Java for years, and trust me, the easy stuff gets
> easier all the time. We've all been there, stuck on a
> problem, and had someone else look at the code and
> fix it in 2 seconds.
>
> Sometimes, (always), when you are stuck and having a
> hard time finding a problem, or a solution, it is
> best just to walk away from the computer for a while,
> otherwise you'll go nuts. I recommend an ice cold
> beer for those occasions, it really gets the brain
> cells turning again.
> ~Tim
hmmm Thanks for the strategy i was reading alot about it that if you get stuck dont worry debug for 3o or 60 min and if still not solved go outside smoke a cig drink a beer or coke and then come back. your brain should function. i have been learning java since 2 days for like 9 hrs straight. i am graduating next spring and i want to complete learning java in one month. Lets hope for the best. Thanks for the suggestion.
Good luck with that 9 month thing, I've been at it for 3-4 years and don't even pretend to know every(any)thing. Learn the basics, and how to use the documentation, and google, and you'll be fine. But you have to learn the basics, otherwise all the info in the world won't help you.
~Tim
> spring and i want to complete learning java in one
> month.
Define "complete learning Java."
"Java" is huge.
In one month, I could see a dedicated, motivated, intelligent person getting:
* Thorough knowledge of most of the language, except for multithreading, generics, and annotations.
* Thorough knowledge of a handful of key classes in the core API.
* Moderate knowledge of a handful of common packages in the core API.
* A very small smattering of knowledge of one or two other areas (Swing, JSPs, JDBC, etc.).
(You do understand the difference between the language and the API, right? For instance, the Scanner class is not part of the Java language.)
well i forgot to mention that i know c++ and i have not been programming like 2 years since i took all my computer classes in the beginning. Secondly sorry i did not meant to say that i will learn java in one month. i have been studying Java since august. and i would like to complete learning java before i graduate. Java is full of varieties dont know which way to go but my aim is to complete the basic programming in java first and then go to Applet and then swing. I can't understand the Java documentation Api. its too big and HUGE so if i will encounter a problem which i will for sure i will concentrate on it. any more info will be great. This website is really very helpful and perfect.
http://chortle.ccsu.edu/java5/cs151java.html
it open my mind in writing programs in java and i started liking it unless i dont run into problems like this one string.length(0 i wasted 1hr 45 min just to see what was wrong but atlast i thought to post it in the forum and someonelse answered me in 3 min. Now i have an idea about increment and decrement since the author did not mentioned in that chapter.
Message was edited by:
fastmike
You are obviously new to programming so I advise you not to rush things as the most valuable skills come with experience. If you don't take your time now you will surely learn some things in "the hard way" latter. Good luck.
look carefully atwhile . you have a ; at the end of while so it is terminatedand after that u'r printing the times variable whose value is the string length, change the code like
int times=0
while (times<inputString..length())
{
Systyem.out.println(inputString);
}>