Solved

string output

Posted on 2004-08-16
13
207 Views
Last Modified: 2010-03-31
quick quesiton. i am playing around with Strings and different methods. i am taking a users keyboard input and displaying the output using different built in functions and i also coded a class that reverses the output. what i am trying to do now is insert somethign like println() to make my output display down??? can anyone suggest where i should put that???

input: hello
output:
h
e
l
l
o
[code]
//in main
  System.out.println("Please input a string ex. WELCOME");
   KBI.pause();
   String result = KBI.getstring();
   System.out.println("output of input as is "+result);
   System.out.println("length of input "+result.length());
   System.out.println("output of input as uppercase "+result.toUpperCase());
   System.out.println("output of input as lowercase "+result.toLowerCase());
   System.out.println("topDown() output "+output.topDown(result));
   System.out.println();

//class
class output
{
   static public String topDown(String s)
      {
        StringBuffer result = new StringBuffer();
        for(int k = s.length()-1; k >=0; k--)
        {
          result.append(s.charAt(k));
        }//end for
        return result.toString();
      }//end topDown
}//end output class

[/code]
0
Comment
Question by:annie613
  • 8
  • 5
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You just need to do

System.out.println(topDown(result));
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You can also do

System.out.println(new StringBuffer(result).reverse());
0
 

Author Comment

by:annie613
Comment Utility
i have that in my code

System.out.println(topDown(result));//your suggestion
System.out.println("topDown() output "+output.topDown(result));//my code

and it prints output as
input: hello
output: olleh

but how do i get it to print

o
l
l
e
h

im missing how to use System.out.print to display myoutput top-down???
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
Comment Utility
Oh you just need to print each character.

char[] chars = reversed.toCharArray();
for(int i = 0;i < chars.length;i++) {
      System.out.println(chars[i]);
}
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
i.e.

String reversed = topDown(result));

// Now the code i posted
0
 

Author Comment

by:annie613
Comment Utility
do i put that in my output class or will that array just be in the main class
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
May as well keep it in the main class
0
 

Author Comment

by:annie613
Comment Utility
i seem to be getting an error on compile
that it is expecting another ;

String reversed = topDown(result));//ERROR ON THIS LINE
   char[] chars = reversed.toCharArray();
   for(int i = 0 ; i < chars.length; i++)
   {
     System.out.println(chars[i]);
   }

   System.out.println("Please input a string ex. WELCOME");
   KBI.pause();
   String result = KBI.getstring();
   System.out.println("output of input as is "+result);
   System.out.println("length of input "+result.length());
   System.out.println("output of input as uppercase "+result.toUpperCase());
   System.out.println("output of input as lowercase "+result.toLowerCase());
   System.out.println("topDown() output "+output.topDown(result));
   System.out.println("different approach for display of input "+new StringBuffer(result).reverse());
}
class output
{
   static public String topDown(String s)
      {
        StringBuffer result = new StringBuffer();
        for(int k = s.length() - 1; k >= 0; k--)
        {
          result.append(s.charAt(k));
        }//end for  
        return result.toString();
      }//end topDown
}//end output class
0
 

Author Comment

by:annie613
Comment Utility
nevermind...i just put the code in the wrong place..
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
That's because you're doing things in the wrong order. The new code should be placed *after* you've got the value of 'result', not before
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0
 

Author Comment

by:annie613
Comment Utility
thanks for the help and insight :)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
No problem ;-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
Increment alphanumeric sequence 6 79
changePi Challenge 15 75
bitbucket vs gitbucket 3 29
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now