annie613
asked on
string output
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("topDow n() 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]
input: hello
output:
h
e
l
l
o
[code]
//in main
System.out.println("Please
KBI.pause();
String result = KBI.getstring();
System.out.println("output
System.out.println("length
System.out.println("output
System.out.println("output
System.out.println("topDow
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]
You can also do
System.out.println(new StringBuffer(result).rever se());
System.out.println(new StringBuffer(result).rever
ASKER
i have that in my code
System.out.println(topDown (result)); //your suggestion
System.out.println("topDow n() 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???
System.out.println(topDown
System.out.println("topDow
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???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
i.e.
String reversed = topDown(result));
// Now the code i posted
String reversed = topDown(result));
// Now the code i posted
ASKER
do i put that in my output class or will that array just be in the main class
May as well keep it in the main class
ASKER
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("topDow n() output "+output.topDown(result));
System.out.println("differ ent approach for display of input "+new StringBuffer(result).rever se());
}
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
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
KBI.pause();
String result = KBI.getstring();
System.out.println("output
System.out.println("length
System.out.println("output
System.out.println("output
System.out.println("topDow
System.out.println("differ
}
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
ASKER
nevermind...i just put the code in the wrong place..
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
:-)
ASKER
thanks for the help and insight :)
No problem ;-)
System.out.println(topDown