3 Replies - 12982 Views - Last Post: 12 March 2011 - 07:38 PM Rate Topic: -----

#1 AaronHopkins  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 180
  • Joined: 01-March 11

Printing Out String Vertically x Number of Times

Posted 09 March 2011 - 06:58 PM

I am writing a program that has the user input a string. The user then inputs a number, x. Each letter of the string should be displayed vertically x number of times. x = repeats. For example:
-String Writer-
Enter a string
Hello

Enter # of repeats: 
4

H H H H
e e e e
l l l l
l l l l
o o o o


I am having trouble with displaying the string x number of times as when I run it, it only displays once. This is my code so far:
import java.util.Scanner;
public class Assignment13{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("-String Writer-");
        System.out.println("Enter a string");
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();

        System.out.println("Enter # of repeats: ");
        int repeats = in.nextInt();
        int i =0;
        int repeatcounter = 1;
        do{
            	char letter = input.charAt(i);
            	System.out.println(letter);
            	repeatcounter++;
            	i++;
        }while(i < input.length() && repeats >= repeatcounter);

        
        }
    
        
            
    
    
}


Much help will be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Printing Out String Vertically x Number of Times

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Printing Out String Vertically x Number of Times

Posted 09 March 2011 - 07:08 PM

Try two nested for() loops instead of the do...while().

The outside loop runs for the length of the input word. The inside loop runs the repeat count times and outputs the letter plus space indexed by the outer loop's counter. When the inner loop exits print a newline.
Was This Post Helpful? 0
  • +
  • -

#3 AaronHopkins  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 180
  • Joined: 01-March 11

Re: Printing Out String Vertically x Number of Times

Posted 09 March 2011 - 10:06 PM

this is what i have now:
        Scanner in = new Scanner(System.in);
        System.out.println("Enter # of repeats: ");
        int repeats = in.nextInt();
        int i =0;
        int repeatcounter = 0;
        for(i = 0; i < input.length(); i++)
        {
        	char letter = input.charAt(i);
        	for(repeatcounter = 0; repeats >= repeatcounter; repeatcounter++)
        	{
        		System.out.println(+ letter + " ");
        	}
        	System.out.println();
        }

& for some reason it's printing out numbers!?
72 
72 
72 
72 
72 

101 
101 
101 
101 
101 

108 
108 
108 
108 
108 

108 
108 
108 
108 
108 

111 
111 
111 
111 
111 


Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Printing Out String Vertically x Number of Times

Posted 12 March 2011 - 07:38 PM

Change

System.out.println(+ letter + " ");


to

System.out.print(letter + " ");

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1