11 Replies - 247 Views - Last Post: 03 May 2013 - 06:06 PM Rate Topic: -----

#1 LakersFan805  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 15-February 13

How do you place every character on a new line?

Posted 29 April 2013 - 07:33 PM

Okay, so i did a program that reverses strings recursively, but how do you place every character on a new line?
lets say I input "Hello", how do you output it like that:
h
e
l
l
o

Thanks in advance.
Here's my code:

import java.util.Scanner;

public class StringReverser 
{
	public static void main (String[] args)
	{
		Scanner keyboard = new Scanner(System.in);
		String text = "";
		
		System.out.println("enter text");
		text = keyboard.nextLine();
		
		System.out.print(reverse(text, text.length()-1));
	}
	
	public static String reverse(String t, int counter)
	{
		if (counter < 0)
		{
			return "";
		}
		else
		{
			return t.charAt(counter) + reverse(t, counter -1);
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: How do you place every character on a new line?

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,119
  • Joined: 19-March 11

Re: How do you place every character on a new line?

Posted 29 April 2013 - 07:37 PM

Take each char c in the string in sequence. Print c, followed by a newline. Repeat.
Was This Post Helpful? 1
  • +
  • -

#3 LakersFan805  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 15-February 13

Re: How do you place every character on a new line?

Posted 29 April 2013 - 07:42 PM

View Postjon.kiparsky, on 29 April 2013 - 07:37 PM, said:

Take each char c in the string in sequence. Print c, followed by a newline. Repeat.

And how would I do that?by using a loop?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: How do you place every character on a new line?

Posted 29 April 2013 - 07:44 PM

System.out.println() automatically add the new line for you
Was This Post Helpful? 0
  • +
  • -

#5 LakersFan805  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 15-February 13

Re: How do you place every character on a new line?

Posted 29 April 2013 - 07:50 PM

View Postpbl, on 29 April 2013 - 07:44 PM, said:

System.out.println() automatically add the new line for you

Yes, but i want a new line for every character e.g.:
h
e
l
l
o

instead of hello.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: How do you place every character on a new line?

Posted 29 April 2013 - 07:55 PM

String str = "hello";
for(int i = 0; i < str.length(); ++i)
   System.out.println(str.charAt(i));



Honestly... a really basic but really basic concept
Was This Post Helpful? 1
  • +
  • -

#7 LakersFan805  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 15-February 13

Re: How do you place every character on a new line?

Posted 29 April 2013 - 08:10 PM

View Postpbl, on 29 April 2013 - 07:55 PM, said:

String str = "hello";
for(int i = 0; i < str.length(); ++i)
   System.out.println(str.charAt(i));



Honestly... a really basic but really basic concept

Yes it is,but the complicated part is combining it into this line:
return t.charAt(counter) + reverse(t, counter -1);


How would you combine it?I'm new to programming with recursion.

This post has been edited by LakersFan805: 29 April 2013 - 08:10 PM

Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,119
  • Joined: 19-March 11

Re: How do you place every character on a new line?

Posted 29 April 2013 - 08:15 PM

So you want to generate a string with newlines? You could try inserting the newline characters manually. :)
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: How do you place every character on a new line?

Posted 29 April 2013 - 08:18 PM

Your method is completly false... and not really recursive if you really want to use the recurvive definiton

it should be

   String orig = "Hello";
   String giro = reverse(orig);
   for(int i = 0; i < giro.length(); ++i)
      System.out.println(giro.charAt(i));


and reverse something like

   String reverse(String str) {
      if(str.length() == 1)
        return str;

      return reverse(str.substring(1)) + str.charAt(0);
   }



Was This Post Helpful? 1
  • +
  • -

#10 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: How do you place every character on a new line?

Posted 29 April 2013 - 10:16 PM

You want to reverse the string, so that hello becomes olleh? You could create a character array from the string and reverse it, or simply:
public String reverse(String s)
{
    String temp = "";
    for(int i = 1; i <= s.length(); i++)
        temp += Character.toString(s.charAt(s.length() - i));
    return temp;
}



Sorry if I've missed something.
Was This Post Helpful? 1
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,652
  • Joined: 20-September 08

Re: How do you place every character on a new line?

Posted 30 April 2013 - 03:31 AM

If you're going to exercise recursion, why don't you do it ... recursively? Say


    public static void reverseToStdOut(String t, int counter) {
        if (counter < 0) {
            return;
        } else {
            System.out.println(t.charAt(counter--));
            reverseToStdOut(t, counter);
        }
    }


This post has been edited by g00se: 30 April 2013 - 03:32 AM
Reason for edit:: Fixed code tags

Was This Post Helpful? 1
  • +
  • -

#12 LakersFan805  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 15-February 13

Re: How do you place every character on a new line?

Posted 03 May 2013 - 06:06 PM

View Postpbl, on 29 April 2013 - 08:18 PM, said:

Your method is completly false... and not really recursive if you really want to use the recurvive definiton

it should be

   String orig = "Hello";
   String giro = reverse(orig);
   for(int i = 0; i < giro.length(); ++i)
      System.out.println(giro.charAt(i));


and reverse something like

   String reverse(String str) {
      if(str.length() == 1)
        return str;

      return reverse(str.substring(1)) + str.charAt(0);
   }



Thanks for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1