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 9 years ago.
Improve this question
public Integer[] imgs = new Integer[]{
R.drawable.ic_launcher,R.drawable.ic_launcher
};
String s_img = "R.drawable.img_test";
i want
imgs[0] = s_img
imgs[0] == R.drawable.img_test
true !!
What shall I do?
Try this:
s_img = "img_test";//not "R.drawable.img_test" just the name of the image
if(getResources().getIdentifier(s_img, "drawable", getPackageName()) == imgs[0]){
return true;
}else{
return false;
}
You want an array of drawables, right?
Integer[] drawables = {R.drawable.image1, R.drawable.image2, ....};
And you can access values by:
drawables[0] == R.drawable.image1
drawables[1] == R.drawable.image2
And you loop them aswel.
for comparing Object do not use "=="
try
"R.drawable.img_test".equals(Integer.toString(imgs[0]));
Put them in an Enum instead of array.
tell me is that exactly what you need or not ,i think i git what you need .
public Integer[] imgs = new Integer[]{
R.drawable.ic_launcher,R.drawable.ic_launcher
};
String s_img;
s_img = Integer.toString(x);
Related
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 10 months ago.
Improve this question
I have a simple for loop
and I want to change it into a statement using filter
for (int lotto_num: lottos) {
if (lotto_num == 0) {
unknown_count++;
}
for (int win_num : win_nums) {
if (lotto_num == win_num) {
count ++;
}
}
}
is it possible? I dont't really know how to use filter and stream.
In order to use Java's Stram API you should firstly convert your int[] arrays win_nums and lottos to List<Integer>.
It can be done this way:
List<Integer> win_nums_list = IntStream.of(win_nums).boxed().collect(Collectors.toList());
List<Integer> lottos_list= IntStream.of(lottos).boxed().collect(Collectors.toList());
Now we can use Stream API to get the desired result.
int count = win_nums_list.stream().filter(win_num -> lottos_list.contains(win_num)).count();
int unknown_count = lottos_list.stream().filter(lotto -> lotto== 0).count();
with adding imports to the beginning of your code:
import java.util.*;
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 6 years ago.
Improve this question
Basically, I'm creating an app where users choose pass, merit or distinction for 18 different units (school basically). My problem is that I can't figure out how to tell the spinner that if the string in it is "Pass", that string equals the integer 70 (Merit = 80 and distinction = 90). I am using a string array and adapters for the spinners. I am currently trying to do this with an If statement:
if (spinner1.toString().equals("Pass")){}
I just have no idea what I should do to tell the string to equal an int.
To reiterate, I want Pass to = 70, Merit = 80, Distinction = 90.
Any guidance and help on this is much appreciated. :)
hope this helps, it's a little vague...
you mean, you have a few string<->int conversion to do?
public YourConstructor() {
/*Map<String, Integer>*/ theMap = new HashMap<String, Integer>();
theMap.put("Pass", 10);
theMap.put("Merit", 70);
theMap.put("distinction ", 90);
...
}
public int getNumber(String text) {
return theMap.get(text);
// I hope this will be, one day, forgotten. This is BAD.
//for (String s : theMap.keySet())
// if (text.equals(s))
// return theMap.get(s);
// return 0; // default for "item not found", or throw
}
thus final String yourString = ""+getNumber(spinner.getSelectedItem().toString());
Conversion::String to integer is done by:
int integer = Integer.parseInt(text);
integer to String
String theString = ""+integer;
Create a Map like below:
Map<String,int> gradeScoreMap=new HashMap<String,int>();
gradeScoreMap.put("PASS",70);
gradeScoreMap.put("MERIT",70);
gradeScoreMap.put("DISTINCTION",70);
The above map will contains the mapping for your grade with score
create a getter for Map:
public String getgradeScoreMap(String grade) {
return gradeScoreMap.get(grade);
}
the above method will be used to fetch the score based on input grade.
Now use:
int score=-1;
if (spinner1.toString().equals("Pass")){
score =getgradeScoreMap(spinner1.toString());
}
The score will be your equivalent value for "PASS" i.e 70
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 In my code I am having following arrays:-
String[] Category = {Rental, Gifts, Gifts};
float[] Amount = {14.76, 15.0, 20.0};
My problem is I want find out same element from first array as there is "Gifts". And according to that convert second one.
So the output will be as:-
Category = {Rental,Gifts};
Amount = {14.76, 35.0};
Will anybody tell me how to achieve this in java?
You should use Map here: Category value should be mapped to Amount value (I would name them in lowercase according to Java coding style):
Map<String, Float> costs = new HashMap<>();
for (int i = 0; i < category.length; ++i) {
Float initial = costs.get(category[i]);
if (initial == null)
initial = 0f;
costs.put(category[i], initial + amount[i]);
}
After these actions, costs map would contain something like (Rental -> 14.76, Gifts -> 35.0)
To get the arrays back (not sure, why it can be useful), you should just iterate through Map's items and write them into an array:
category = new String[costs.size()];
amount = new Float[costs.size()];
int i = 0;
for (Map.Entry<String, Float> entry: costs.entrySet()) {
category[i] = entry.getKey();
amount[i] = entry.getValue();
++i;
}
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
public Set intersection(Set set){
Set intersect = new Set(this.count + set.count);
for(int i=1; i<count; i++)
{
if(this.items[i] && set.items[i])
intersect.add(i);
return intersect;
}
To find the intersection of two sets you can use the function retainAll.
Set<Integer> s1;
Set<Integer> s2;
s1.retainAll(s2);
After this, s1 will contain the intersection.
You could also use this method for arrays if you don't mind converting them to a set or list first, e.g.:
int a[] = {1,2,3};
Set<Integer> mySet = new HashSet<Integer>(Arrays.asList(a));
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 am trying to run some objects methods in this way:
String[] sequencer = {"seq1", "seq2", "seq3", "seq4", "seq5", "seq6", "seq7", "seq8"};
for(int i; i<9; i++) {
sequencer[i].setBackground(Color.red);
}
seq1, seq2..seq8 are jPanels. Any idea how to do this ? I hope you understand what I want to do.
Can you do this? Or are you looking for something else?
JPanel [] sequencer = new JPanel[]{seq1, seq2, seq3, seq4, seq, seq6, seq7, seq8};
for(int i; i<9; i++) {
sequencer [i].setBackground(Color.red);
}