How to decrease my repeated code [closed] - java

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 8 years ago.
Improve this question
Hi How I write these several code in only one line
if (stimee.getText().toString().equals("0")) {stimevar="00"; }
if (stimee.getText().toString().equals("1")) {stimevar="01"; }
if (stimee.getText().toString().equals("2")) {stimevar="02"; }
if (stimee.getText().toString().equals("3")) {stimevar="03"; }
if (stimee.getText().toString().equals("4")) {stimevar="04"; }
if (stimee.getText().toString().equals("5")) {stimevar="05"; }
if (stimee.getText().toString().equals("6")) {stimevar="06"; }
if (stimee.getText().toString().equals("7")) {stimevar="07"; }
if (stimee.getText().toString().equals("8")) {stimevar="08"; }
if (stimee.getText().toString().equals("9")) {stimevar="09"; }

all you really need is:
stimevar="0"+stimee.getText().toString();
since i dont see an else clause in your question, this must solve your problem.

You can use a Map to store the values and then set the variable.
Map<String, String> map = new HashMap<>();
map.put("0", "00");
map.put("0", "01");
...
map.put("9", "09");
stimevar = map.get(stimee.getText().toString());

String str = stimee.getText().toString();
for (int i = 0; i <= 9; i++)
if (("" + i).equals(str))
stimevar = "0" + i;

// parse input value as integer
int value = Integer.parseInt(stimee.getText().toString());
// check input for values from 0 to 9
for (int i = 0; i < 10; i ++) {
// if found a match
if (value == i) {
// set variable
stimevar = String.format("%02d", i);
// and stop checking
break;
}
}
EDIT: Thanks to #Tom for pointing out parsing the String only once beforehand would be more effective.

Related

Refactoring a for loop into a stream [closed]

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 2 years ago.
Improve this question
How to change this for loop into stream?
public int calculateForSpliterator(String[] matchTab, String spliterator, RulesChecker rulesChecker) {
int finalScore = 0;
for (String element : matchTab) {
String[] splitScroes = element.split(spliterator);
int ourPoints = Integer.parseInt(splitScroes[0]);
int theirPoints = Integer.parseInt(splitScroes[1]);
finalScore += rulesChecker.checkRules(ourPoints, theirPoints);
}
return finalScore;
}
Assuming RulesChecker#checkRules returns an int, then you can use:
public int calculateForSpliterator(String[] matchTab, String spliterator, RulesChecker rulesChecker) {
return Arrays.stream(matchTab)
.map(element -> element.split(spliterator))
.mapToInt(splitScores -> rulesChecker.checkRules(Integer.parseInt(splitScores[0]),
Integer.parseInt(splitScores[1])))
.sum();
}
Notice how this isn't much more readable than your current solution, so I probably would keep the for-loop if I were you.

Add element to a string [closed]

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 use java and I have the following string:
points="335,234 285,320 185,320 135,234 186,147 285,147 335,233 ";
How it is possible to add 2 to each number?...for example:
points="337,236 287,322 187,322 137,236 188,149 287,149 337,235 ";
You can use String#split to get all the separate numbers in an array, then use a for to iterate through them:
String points = "335,234,285,320,185,320,135,234,186,147,285,147,335,233";
String[] indvPoints = points.split(",");
for(int i = 0; i < indvPoints.length; i++) {
indvPoints[i] = String.valueOf(Integer.parseInt(indvPoints[i]) + 2);
}
points = Arrays.toString(indvPoints).replaceAll("[\\[\\] ]", "");
System.out.println(points);
Although I suggest you just use an int array to begin with, it would be much more efficient and less likely to encounter errors:
int[] points = {335,234,285,320,185,320,135,234,186,147,285,147,335,233};
for(int i = 0; i < points.length; i++) {
points[i] += 2;
}
System.out.println(Arrays.toString(points));

How tho check in Java if there is same characters in a String? [closed]

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've got a 6 char long string, and I would like to check, if there is just one from each character. How to?
You could compare the number of distinct characters to the length of the string:
boolean charMoreThanOnce = s.chars().distinct().count() < s.length();
You can do it using a Set. You need unique elements and Set gurantees you containing the unique elements. HashSet is implementation of Set, you can use it to implement this idea.
public boolean ifAllCharsUnique(String input){
char[] arr = input.toCharArray();
int length = arr.length;
Set<Character> checker = new HashSet<>();
for(int i =0 ; i < length; i++){
if(checker.contains(arr[i]){
return false;
}
checker.add(arr[i]);
}
return true;
}

How to substract two strings [closed]

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 8 years ago.
Improve this question
I have started learning Java and have some across some difficulties. I'm trying to subtract two strings.
for example, with these strings;"032"&&"100". I want to be able to subtract each number individually so that the answer would be "032".
I have tried using substring, and parsing the two values to ints, but don't know what to do next. I have also tries using a for loop, to go through each arrays of the strings.
I do not expect for anyone to do this for me, but I would love to get some insight,or to tell me that i'm headed in the right direction
thanks
public static String appliquerCoup( String combinaison, String coup ) {
String nouveauCoup="";
if(combinaison!=null&&coup!=null){
for(int i=0;i>combinaison.length();i++){
int a = Integer.parseInt(combinaison.substring(i, i + 1));
int b = Integer.parseInt(coup.substring(i, i + 1));
nouveauCoup=String.valueOf(a-b);
if(a-b<0){
nouveauCoup=0;
}
}
} // main
return nouveauCoup;
}
If I understand you question correctly. you want to subtract each digit individually.
So (0-1), (3-0), (2-0). The following program does this (yields -132):
public static void main(String[] args) {
String A = "032";
String B = "100";
String str = "";
for(int i = 0; i < A.length(); i++)
{
int a = Integer.parseInt(A.substring(i, i + 1));
int b = Integer.parseInt(B.substring(i, i + 1));
int c = a - b;
str += String.valueOf(c < 0 ? 0 : c);
}
System.out.println(str);
}
Essentially, extract the i-th character of each string, convert them to integers, then do the subtraction. Convert the result back to a string and append it to the result string.

Comparing multiple strings in Java [closed]

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 8 years ago.
Improve this question
I have 5 distinct strings called, say, string1 through string5.
I want to write a simple if statement that runs if any two of the five strings contain the same string. How would I do that?
Thanks in advance!
Comparison is a binary operation, therefore you can always compare only two objects at a time. I would suggest using a cycle and comparing each string to the remaining ones.
public boolean multipleStringEquals(String[] strings) {
for (int i = 0; i < strings.length; i++) {
for (int j = i + 1; j < strings.length; j++) {
if (strings[i].equals(strings[j])) {
return true;
}
}
}
return false;
}

Categories