Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Java question (DL Degree - not school homework!)?
I'm doing a DL degree and we are doing a Java module. I'm completely stuck and need some pointers. Obviously I am also working full time (hence distance learning) so I don't get any lab sessions for help.
I'm pretty sure I need to use an Array in some way but I don't understand how.
This is the question
Write a Java program which takes a sentence from user input, remove all non-letters, reverse it and change case of every letter. For example: Welcome to C4DM -> mdcOTEMOCLEw
This is the code I have so far which does everything except reversing the case:
import java.util.Scanner;
public class StringTest2{ public static void main(String[] args){
Scanner myScanner = new Scanner(System.in);
System.out.print("Please enter a string: ");
String s = myScanner.nextLine();
String nInput = s.replaceAll("\\W", ""); //remove non word characters
String oInput = nInput.replaceAll("\\d", ""); //remove digit characters
String rInput = new StringBuffer(oInput).reverse().toString();
System.out.println("Result: " + rInput);
}
}
One line of code has been shortened, see below. I have added a space after String buffer(oInput) otherwise the word is too long I think. Thanks for your help guys!
String rInput = new StringBuffer(oInput). reverse().toString();
Hi Guys - thanks for the answers. I know how to convert a whole string to uppercase or lowercase, what I am struggling to do is separate out the characters of the string, test them for case and then reverse this (on a char by char basis, as I cannot find a way to reverse the case of string as a string)
2 Answers
- WarrenLv 510 years agoFavorite Answer
All you have to do is call isUpperCase(char a) and then call toUpperCase() or toLowerCase(). Call this in a loop and you are done.
- 10 years ago
Your code is good (I tested it).
Try to restart your IDE and see if it works good.
Also, if you are using Eclipse, try to do right-click on that class file and choose Run As>Desktop application.
Sometimes if you just hit green run button on main toolbar Eclipse doesn't recompile application.
EDIT: I misread word.
You should do this (as stated in answer above):
String rInput = new StringBuffer(oInput). reverse().toString() .toUpperCase();
My apologizes.
Source(s): I had problems with Eclipse like that in past, and thats why I use NetBeans :)