Computer Science Canada

Help With Loop's

Author:  iBIGTRAIN [ Tue Jul 21, 2009 8:48 pm ]
Post subject:  Help With Loop's

Im doing an online class about computer science and I have an exam tommrow, this assignment will give me extra marks. Ive got a basic idea on what I have to do for the assignment. I got most of the code right but it's just the last line that im haveing some trouble with.

Write a program that asks the user to enter a word. The program will then repeat the word for as many times as it has characters:

Sample Output

Enter a word : Hello

It has a length of 5 characters.

Hello Hello Hello Hello Hello

This is my code but it dosent work and I dont know what els I can put to make it work, ive been doing to for two days and im extremly angery.

// The "IntegersOne" class.
import java.awt.*;
import hsa.Console;

public class IntegersTwo
{
static Console c; // The output console

public static void main (String[] args)
{
c = new Console ();

String word;
int letters;

c.println ("Please enter a word");
word = c.readLine();
letters = word.length();
{
System.out.println(letters+word);
}

Author:  Analysis Mode [ Tue Jul 21, 2009 8:54 pm ]
Post subject:  Re: Help With Loop's

something like this will do: (C++ syntax)

for (i=0;i<word.length();i++)
printf(word);

Author:  iBIGTRAIN [ Tue Jul 21, 2009 8:58 pm ]
Post subject:  RE:Help With Loop\'s

Is that for Ready to Program Java 1.7.1? ive never seen that before.

Author:  FreshPrince [ Tue Jul 21, 2009 9:03 pm ]
Post subject:  Re: Help With Loop's

// The "IntegersOne" class.
import java.awt.*;
import hsa.Console;

public class IntegersTwo
{
static Console c; // The output console

public static void main (String[] args)
{
c = new Console ();

String word;
int letters;

c.println ("Please enter a word");
word = c.readLine ();
letters = word.length ();

for (int i = 1 ; i <= letters ; i++)
{
System.out.println (word);
}
}



}

the for loop just starts at 1 and executes the command in the brackets for as many times as there are letters in the word, that should work fine.

Author:  iBIGTRAIN [ Tue Jul 21, 2009 9:12 pm ]
Post subject:  RE:Help With Loop\'s

Thank you very much that works perfect, such quick responses this website is great

Author:  Nai [ Wed Jul 22, 2009 8:49 pm ]
Post subject:  RE:Help With Loop\'s

I actually had to do that exact same assignment for my online course a little while ago. It really annoyed me too, but only because I didn't know the length method.


: