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 5 years ago.
Improve this question
I have a problem in my assigment, I want to invert a word like "Indonesia" to "aisenodnI".
public static void main (String[]args){
balik();
}
public static void balik (String nama){
for ( i>=nama.Length-1 ; i=0 ; i-- ){
balik = balik + nama.Length();
System.out.print(balik);
}
}
You can use StringBuffer or StringBuilder for this task, StringBuilder would be my choice since its more efficient. its not thread safe so multiple threads can call its methods simultaneously.
String reversedString = new StringBuilder(originalString).reverse().toString()
If you prefer not to use API support you can do something like this
static String reverse(String stringIn) {
char[] cArr = stringIn.toCharArray();
for (int i = 0; i < cArr.length/2; ++i){
char c = cArr[i];
cArr[i] = cArr[cArr.length-1-i];
cArr[cArr.length-1-i] = c;
}
return new String(cArr);
}
If only reversing of the string is needed then you can go with the below method.
String name= "India";
String reverseString = new StringBuffer(name).reverse().toString();
System.Out.Println("reversed sstring is "+reverseString );
Related
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
If i have an input smt. like that:
1,10;3,3;4,1. Lets say String input.
How can I split it in ";", so the result could be like this:
[1,10]
[3,3]
[4,1]
Thanks!
I think you want to do something like this.
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input;
//your input is 1,10;3,3;4,1
input = scanner.next();
String[] splitted = input.split(";");
//save the new list or array
ArrayList list = new ArrayList();
for (int i = 0; i < splitted.length; i++) {
System.out.println(splitted[i]);
//for later usage
list.add(splitted[i]);
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
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
public class ReverseString {
public static void main(String[] args) {
String reverse = "";
String obj = new String("BOOKS");
for ( int i = obj.length() - 1 ; i >= 0 ; i-- )
reverse = reverse + obj.charAt(i);
System.err.println("Orignal string is: "+obj);
System.out.println("Reverse string is: "+reverse);
}
}
Import scanner, then create its object and use it to take user input.
import java.util.Scanner;
//Statements of your code
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String obj = sc.nextLine();
//Statements of your code
}
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 5 years ago.
Improve this question
For example I have this string, where email addresses are differently formatted:
"a.b#c.d" <a.b#c.d>|"Michal pichal (michal.pichal#g.d)" <michal.pichal#g.d>|Melisko Pan <melisko.pan#domena.sk>
I need to extract the email addresses in a form of:
a.b#c.d|michal.pichal#g.d|melisko.pan#domena.sk
My idea was to get any char near # from group [0-9][A-Z][a-z]#.- but I do not know how. Please help with some hint.
This regex extracts emails out of your string:
public static void main(String[] args) {
Pattern pattern = Pattern.compile("[\\w.]+#[\\w.]+");
Matcher matcher = pattern.matcher("\"a.b#c.d\" <a.b#c.d>|\"Michal pichal (michal.pichal#g.d)\" <michal.pichal#g.d>|Melisko Pan <melisko.pan#domena.sk>\r\n");
while(matcher.find()){
String group = matcher.group();
System.out.println("group="+group);
}
It prints:
group=a.b#c.d
group=a.b#c.d
group=michal.pichal#g.d
group=michal.pichal#g.d
group=melisko.pan#domena.sk
import java.util.Scanner;
class test{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String res = ""; //This holds the final result
for(int i=0; i<str.length(); ++i){ //Loop through the entire string
if(str.charAt(i)=='<'){
String build = "";
for(int j=i+1; j<str.length(); ++j){
if(str.charAt(j)=='>'){
break; //Break when we hit the '>'
}
build += str.charAt(j); //Add those between < and >
}
res += build + "|"; //Add the pipe at the end
}
continue;
}
System.out.println(res);
}
}
This ought to do it.
Just run simple nested loops. No need for regex.
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
Write a method called multiConcat that takes a String and an integer as parameters. Return a String made up of the string parameter concatenated with itself count time, where count is the integer. for example, if the parameters values are “ hi” and 4, the return value is “hihihihi” Return the original string if the integer parameter is less than 2.
What i have so Far
import java.util.Scanner;
public class Methods_4_16 {
public static String multiConcat(int Print, String Text){
String Msg;
for(int i = 0; i < Print; i ++ ){
}
return(Msg);
}
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
int Prints;
String Texts;
System.out.print("Enter Text:");
Texts = Input.nextLine();
System.out.print("Enter amount you wanted printed:");
Prints = Input.nextInt();
System.out.print(multiConcat(Prints,Texts));
}
}
Just a few hints:
concating strings can be done this way: appendTo += stuffToConcat
repeating an operation n times can be done with a for-loop of this kind:
for(int i = 0 ; i < n ; i++){
//do the stuff you want to repeat here
}
Should be pretty simple to build the solution from these two parts. And just in case you get a NullPointerException: remember to initialize Msg.
Try this:
public static String multiConcat(int print, String text){
StringBuilder msg = new StringBuilder();
for(int i = 0; i < print; i ++ ) {
msg.append(text);
}
return msg.toString();
}
I have used StringBuilder instead of a String. To know the difference, give this a read: String and StringBuilder.
Also, I would guess you are new to Java programming. Give this link a read. It is about Java naming conventions.
Hope this helps!
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
i want to replcae "Demo" from "DemoString" with other substring,but only if "Demo" is present at the starting of string.No replacement will be in case of "StringDemo".
Do you mean?
String text = ...
String text2 = text.replaceAll("^Demo", "NotDemo");
For regular expressions the ^ only matches the start of the string.
check condition if the string starts with the key then replace the first key with the desired key like below:
String string = "demo string string demo";
if (string.startsWith("demo")) {
System.out.println(string.replaceFirst("demo", "xyz"));
}
output:
xyz string string demo
Update:
As we are using replaceFirst() there is no need of adding condition we can directly call the method to replace the string
System.out.println(string.replaceFirst("^demo", "xyz"));
Check out this short example:
public class HelloWorld{
public static void main(String []args){
String s = "DemoString StringDemo DemoString";
String[] str = s.split(" ");
for(String ss : str)
{
int index = ss.indexOf("Demo"); //check if "Demo" is at the start of the string
if(index == 0)
{
ss = ss.replace("Demo","Demo2");
}
System.out.println(ss);
}
}
}
Output:
Demo2String StringDemo Demo2String
Try something like:
String myline = line.replaceAll("^[Dd][eE][mM][Oo]", "");