Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to eliminate vowels from a String, I can eliminate them, but I fail to return them to main(). I got the exact output by using the following code.
String string = "ajeIokluj";
String s = string.replaceAll("[aeiouAEIOU]","");
return s;
It will be great if the required output came by using for loop.
Hope you have written the code similar to below considering your fail to return statement .
public static void main(String[] args) {
String string = "ajeIokluj";
String s = eliminateVowels(string);
System.out.println(s);
}
private static String eliminateVowels(String string) {
String s = string.replaceAll("[aeiouAEIOU]","");
return s;
}
If you did it works perfectly fine and if not use above as reference ;)
Based on your comments since you looking for specifically using for loop (Which is not recommended) please find code below.
public static String removeVowels(final String string){
final String vowels = "AaEeIiOoUu";
final StringBuilder builder = new StringBuilder();
for(final char c : string.toCharArray())
if(vowels.indexOf(c) < 0)
builder.append(c);
return builder.toString();
}
public class main {
public static String removeVowels(String word) {
String ret = "";
String realRet = "";
for (int i = 0; i < word.length(); i++) {
if ("aeiouAEIOU".indexOf(word.charAt(i)) == -1) {
ret += word.charAt(i);
}
}
realRet = realRet + ret.charAt(0) + ret.charAt(1);
return realRet.toLowerCase() ;
}
public static void main(String[] args) {
String pass = removeVowels("Your String");
for(int i=0 ; i < 3; i++) {
pass = pass + (int) (Math.random() * 100) ;
}
System.out.println(pass);
}
}
Try this it may work you!!
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
I am looking for solution using stream api for below requirement. basically we need to write a encryption logic to convert a string
aaabbbbaae --> a3b4a2e1
Solution without stream's api :
public static void main(String[] args) {
String sampleString = "aaabbbaaaaaee";
char charters[] = sampleString.toCharArray();
char currentChar = charters[0];
char prevChar = charters[0];
int count = 0;
String str = "";
for (char c : charters) {
if(c==currentChar) {
count++;
prevChar = c;
}
else {
str = str + prevChar + count;
currentChar = c;
count=1;
}
}
System.out.println(str+currentChar+count);
}
This would be pretty easy to do yourself
String encryptToLettersAndNumbers(String input) {
String product = "";
char last = input.charAt(0);
int streak = 0;
for(int i = 0; i < input.length(); i++) {
if(input.charAt(i) == last) {
streak++;
} else {
product = product + last + streak;
streak = 1;
last = input.charAt(i);
}
}
product = product + last + streak;
return product;
}
Note that this code is untested. Furthermore this will be a very unsafe method of encryption because h1e1l2o1 is pretty easy to guess.
This should get you started-
String sampleString = "aaabbbaaaaaee";
Arrays.asList(sampleString.chars().mapToObj(c->(char)c).toArray(Character[]::new)).stream().forEach(c->{
//You can put your logic here to process
System.out.print(c);
});
This will convert your String to a Character list and stream through it.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am new towards coding, so I've managed to create a code that allows me to reverse words, however, I don't fully understand the for loop construct, because I created this using online resources.
public class word {
public static String rWords(String input) {
String[] split = input.split("");
String output = " ";
for (int i = split.length - 1; i >= 0; i--) {
output += (split[i] + "");
}
return output.trim();
}
}
Say there is already a main class that contains a string value of input, this is another class called word, I understand that making it public static string means it's public and static means it's not declared in instances. It contains one parameter with the input from main class, the output is empty for my for loops results to go into that, however, how does the for loop allow my input to be reversed and return.trim do?
Why don't you use out-of-box approach? E.g. StringBuilder already has a method reverse():
public static String reverseWords(String str) {
return Arrays.stream(str.trim().split("\\s+"))
.map(word -> new StringBuilder(word).reverse().toString())
.collect(Collectors.joining(" "));
}
But you string can do it with old Java:
public static String reverseWords(String str) {
// using StringBuilder for multiple string concatenation
StringBuilder buf = new StringBuilder(str.length());
for (String word : str.trim().split("\\s+")) {
// add space if word is not first one
if (buf.length() > 0)
buf.append(' ');
// add each word from end to beginning
for (int i = word.length() - 1; i >= 0; i--)
buf.append(word.charAt(i));
}
return buf.toString();
}
In case you need swap words in the sentence, principle is the same:
public static String reverseWords(String str) {
StringBuilder buf = new StringBuilder(str.length());
String[] words = str.trim().split("\\s+");
for (int i = words.length - 1; i >= 0; i--) {
if (buf.length() > 0)
buf.append(' ');
buf.append(words[i]);
}
return buf.toString();
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a list of words stored in a list, words.
private String[] words = new String[]{"world", "you"};
I then have a string, helloWorld
private String helloWorld = "Hello world how are you?";
I would like to create a function that will take a string (in this case, helloWorld) and it will look case-insensitively to see if any of the strings in the words list are present. If there is, it will put a * character in between each letter of the matching string.
E.g. the output would be
Hello w*o*r*l*d how are y*o*u? since both world and you are in the list.
Passing "Hello" would simply return back the unmodified string "Hello" because there is nothing in the string that is inside words.
How would I go about doing this? I have tried hardcoding a .replaceAll() call on the string for each word, but then I lose the casing of the string. E.g. "Hello world how are you?" became "hello w*o*r*l*d how are y*o*u?"
This code:
private static String[] words = new String[]{"world", "you"};
private static String helloWorld = "Hello world how are you?";
public static String getHello() {
String s = helloWorld;
for (String word : words) {
int index = s.toLowerCase().indexOf(word.toLowerCase());
if (index >= 0) {
String w = s.substring(index, index + word.length());
StringBuilder sb = new StringBuilder();
sb.append(s.substring(0, index));
for (int i = 0; i < w.length(); i++) {
sb.append(w.charAt(i));
if (i < w.length() - 1)
sb.append("*");
}
sb.append(s.substring(index + w.length()));
s = sb.toString();
}
}
return s;
}
public static void main(String[] args) {
System.out.println(getHello());
}
prints:
Hello w*o*r*l*d how are y*o*u?
String helloWorld = "hello world how are you ";
String[] words = new String[]{"world", "you"};
String newWord = "";
String words1[]= helloWorld.split(" ");
for (int i = 0;i< words.length;i++){
for (int j=0;j<words1.length;j++){
if (words1[j].equals(words[i])){
for (int k = 0 ; k < words1[j].length(); k++){
char character = words1[j].charAt(k);
newWord+=character;
newWord += "*";
}
words1[j] = newWord;
newWord= "";
}
}
}
String str = Arrays.toString(words1);
System.out.println(str);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I wrote a simple program in Java which writes a word backwards. Trying to check if "hello" works. In if-statement I'm checking that string is equal to "olleh". Could anyone see why the if statement won't execute.
public class MyProgram {
public static void main(String[] args) {
String x = "hello";
System.out.println(back(x));
}
public static String back(String str) {
String y = " ";
String temp = " ";
for (int i = str.length() - 1; i >= 0; i--) {
char lets = str.charAt(i);
y += Character.toString(lets);
System.out.println(y);
if (y.equals("olleh")) {
System.out.println("nice");
}
}
return y;
}
}
Try this it will work
public class MyProgram
{
public static void main(String[] args)
{
String x = "hello";
System.out.println(back(x));
}
public static String back(String str )
{
String temp = "";
for (int i = str.length() - 1; i >= 0; i--) {
char lets = str.charAt(i);
temp = temp + lets;
}
if (temp.equals("olleh")) {
System.out.println("nice");
}
return temp;
}
}
If you will initialize y variable to empty string instead of space your if-statement will execute and print "nice". Also you do not need a temp string as you don't use it. You probably want to return you reverted string back (alternatively you can make your method void and remove the return statement).
public static String back(String str) {
String y = "";
for (int i = str.length() - 1; i >= 0; i--) {
char lets = str.charAt(i);
y += Character.toString(lets);
System.out.println(y);
if (y.equals("olleh")) {
System.out.println("nice");
}
}
return y;
}
By the way, it's better to use StringBuilder when you're concatenating strings in a loop.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I only found this but it's not working, i'll be very thankful if someone helps me because i'm new to java and it's getting me mad lol:
public class ConcatenateStrings {
public static String concateLines(String[] s, String separator) {
String result = "";
if (s.length > 0) {
result = s[0]; // start with the first element
for (int i = 1; i < s.length; i++) {
StringBuilder sb = new StringBuilder(result);
sb.append(separator);
sb.append(s[i]);
result = sb.toString();
}
}
return result;
}
}
Don't create a new StringBuilder every time.
public class ConcatenateStrings {
public static String concateLines(String[] s, String separator) {
String result = "";
StringBuilder sb = new StringBuilder();
if (s.length > 0) {
sb.append(s[0]);
for (int i = 1; i < s.length; i++) {
sb.append(separator);
sb.append(s[i]);
}
}
return sb.toString();
}
}
public static void main(String[] args) {
String[] input = {"Test", "input"};
System.out.println(ConcatenateStrings.concateLines(input, ","));
}
Or with user input:
public static void main(String[] args) {
System.out.println(ConcatenateStrings.concateLines(args, ","));
}
As you can see, you can even drop the result variable.