Program to take name as input and write it backwards
Can someone please help me with my programming project?
The directions are:
Solution must employ at least one method other than the main() method. Program should be annotated. An explanation of all features of the program.
I'm trying to make a working program that takes your name and writes it backwards. Is that possible?
Can someone please help me write this and give an explanation. Please! I need this ASAP!
Thanks!
Re: Need Help with Programming project
hi,
here is my code. hope it helps.
Code :
import java.io.*;
public class WordInverter {
private static BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args)throws IOException
{
String strInv = null,word;
System.out.print("Enter a word : ");
word=br.readLine();
strInv = invert(word);
System.out.println("The inverted word : " + strInv);
}
//another method
public static String invert (String s) {
String temp = "";
for (int i=s.length()-1; i>=0; i--)
temp += s.charAt (i);
return temp;
}
}
Re: Need Help with Programming project
Hello snake101 and welcome to the Java Programming Forums.
Yes what you are trying to do is possible.
I would use the Scanner class to read in the persons name from the console and then use the StringBuffer reverse() method to reverse the String.
Here is an example. You can comment it yourself.
Code :
import java.util.Scanner;
public class Snake101 {
/**
* JavaProgrammingForums.com
*/
private static String name, nameReversed;
public static void reverse(String n){
nameReversed = (new StringBuffer(n)).reverse().toString();
System.out.println(nameReversed);
}
public static void main(String[] args) {
Snake101 snake = new Snake101();
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name:");
name = sc.next();
snake.reverse(name);
sc.close();
}
}
Re: Need Help with Programming project
Well have you main function prompt for the name and then pass it to a function called reverse that returns the revrse of thee String given to it and then print it out :)
Code :
public class snake{
public static void main(Sting[] args){
System.out.print("Please enter your name: ");
String name = (new Scanner(System.in)).nextLine();
System.out.println("The reverse of your name is: " + this.reverse(name) );
}
private static String reverse(String name){
// You can write the reverse function, or at least have a shot
return reversedName;
}
}
Regards,
Chris
Re: Need Help with Programming project
You are spoilt for choice snake101 b-)
Thank you for your post Truffy. That is a good way to reverse a String, although the StringBuffer reverse() method is much easier IMO.
Re: Need Help with Programming project
Good posts guys, shame I wasn't willing to give an answer straight away :P. Imight have to adapt my style so that my answer is as helpful as other when we post at the same time hehe :P
Chris
Re: Need Help with Programming project
Quote:
Originally Posted by
JavaPF
You are spoilt for choice snake101 b-)
Thank you for your post Truffy. That is a good way to reverse a String, although the StringBuffer reverse() method is much easier IMO.
LOL. i didn't think of using scanner. LOL. i do it on a long way. ;)) Anyway, atleast snake can see different approach in solving it. :D
:-bd
Cheers guys. :)
Re: Need Help with Programming project
Quote:
Originally Posted by
Freaky Chris
Good posts guys, shame I wasn't willing to give an answer straight away :P. Imight have to adapt my style so that my answer is as helpful as other when we post at the same time hehe :P
Chris
Well this was my first post of the morning. I was feeling kind! Nothing like a Java problem & a cup of coffee to wake you up.