Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

string output

Posted on 2004-08-16
13
Medium Priority
?
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11810311
You just need to do

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

Expert Comment

by:CEHJ
ID: 11810323
You can also do

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

Author Comment

by:annie613
ID: 11810372
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 11810411
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
ID: 11810421
i.e.

String reversed = topDown(result));

// Now the code i posted
0
 

Author Comment

by:annie613
ID: 11810437
do i put that in my output class or will that array just be in the main class
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11810452
May as well keep it in the main class
0
 

Author Comment

by:annie613
ID: 11810537
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
ID: 11810588
nevermind...i just put the code in the wrong place..
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11810591
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
ID: 11810719
:-)
0
 

Author Comment

by:annie613
ID: 11810748
thanks for the help and insight :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11810763
No problem ;-)
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

636 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