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 8 years ago.
Improve this question
Basically, I have to write a method that determines whether all the characters in a string are the same, using only library String methods.
Use matches. Please read up the documentation of Pattern class to understand how to use matches function to check your input. It is quite useless for me to give you a straight answer, since you don't learn much from it.
That method will give you one-liner solution after you have understood regex.
public static void main(String[] args) {
String str = "aaaaa";
int count = str.length() - str.replaceAll(String.valueOf(str.charAt(0)), "").length();
if(count == str.length())
System.out.println("All characters in a given String are same");
}
Try this : This is the simplest and best solution for this kind of problem.
One way to solve this is without looping or recursion is by [ab]using String.replace1.
The actual implementation is left as an exercise.
1 It does not require that your code loops, but something must loop.
Related
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 have a string array
String[] albumnames;
now how to take a string from particular index position with limited number of charachters.
For example,
if, albumnames[position] have value "abcdefghijk"
then i want to take the first 5 characters only.
That is "abcde".
The substring method of String can be used to achieve this. Try
String s = albumnames[position].substring(0,5);
See substring docs
albumnames[position].substring(0,5);
you can see the methods in String class, like substring, indexof
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 have a problem - is there any possibility to recognize in the text the negative number, that has a minus sign on the right?
E.g. I'm thinking about number like that: 1500.0- (instead of -1500.0).
Thank you in advance for any help.
Not only is it possible, it is actually easier to handle a trailing sign. You just convert the number as usual, continuing while you keep getting digits, then if it ends with a minus sign just negate it. Easier than having to remember a leading sign.
Despite the names of the methods provided in the JDK, this is not 'parsing', it is radix conversion.
You could use charAt or a regular Expression, and many more...
String s = "1500.00-";
if (s.charAt(s.length()-2) == '-'){
//minus on the right.
}
First move the minus (if any) from the end to the start:
num = num.replaceAll("(.*)(-)?$", "$2$1");
The good thing here is that if there's no minus sign at the end, or yjr minus us alteady at the start, nothing change it made.
Then parse it as normal, eg:
double d = Double.parseDouble(num);
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 have to create a random cellnumber 07939393914 for automation testing purpose.
Last 079393(5 digits) digits should change randamly.. each time test runs..
Can any one suggest JAVA code for this ? or else Selenium Java code ?
Thanks
Is this a number or a String? I ask as it has a leading zero.
Take you initial number as 7939300000
then add to it Math.round(Math.Random()*10000)
If you want it as a String, take your string as "079393" and use Integer.toString on the result above, then concatentate them
Use RandomStringUtils class.
String randomNumbers = RandomStringUtils.randomNumeric(5);
String phNo = 079393+randomNumbers;
var num=Math.ceil(Math.Random()*100000)
for random first 5 digit numbers
and add
var your_last_5digits; //as string to your last 5 digits
cellNumber='0'+num+your_last_5digits;
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 am new to java need help
the main string i have is
eg.
"String1/string2/string3/string4/all_free.sdx"
"file1/file2/string3/string4/all_free.sdx"
the end result i need is to be able to isolate and get string3
i can indexof but not able to achieve it in few steps need brainy people help as i am new to JAVA
If you know it's the third item that you want to get, one simple approach could be using split method, like this:
String myString = "String1/string2/string3/string4/all_free.sdx";
String string3 = myString.split("/")[2];
The call of split("/") produces an array of strings like this:
{"String1", "string2", "string3", "string4", "all_free.sdx"}
Now you can apply the subscript operator to grab the element that you want.
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
How do i write an if statement in Java that displays Goodbye! if the variable word contains a letter d?
Thanks to everyone.
if (word.contains("d")) {
System.out.println("Goodbye!");
}
Use:
if(word.indexOf("d") >= 0) {
System.out.println("Goodbye!");
}
Look up the Java API docs to see what is available in the String class. There are several options including the indexOf() method that returns -1 if the given character is not in the String and an index of the character if it is found in the String.
int ans = mystring.indexOf(mychar);
You can then use an if statement to check the ans variable.
if (word.contains("d")) System.out.println("Goodbye!");
Well, that was in Java!!
if(word.compareTo("d") == 0)
System.out.println("Goodbye!");
or
if(word.equals("d"))
System.out.println("Goodbye!");
That's assuming of course that word is a String and not a char.