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;
}
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 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 a "for" loop that is creating a variable each time through the loop. I am attempting to insert the results into an empty array at the index of the "i" in the loop. From the best I can tell it seems I need to create a ArrayList vs. an Array to make this happen.
int varNum = 10;
Array someArr = new Array ();
for (int i = 0; i < 10; i++){
varNum = varNum +i;
someArr[i] = varNum;
}
On the first loop I want the 10 to be inserted in my array at the "0 index", 11 inserted at "1 index", 12 at the "2 Index".
**The important part is that the Array is not a set size, because I do not know how many indexes I will need in the array, so I want to add them as I needed.
If you use an ArrayList you can call add like this:
int varNum = 10;
ArrayList<Integer> someArr = new ArrayList<Integer>();
for (int i = 0; i < 10; i++)
{
varNum = varNum + i;
someArr.add(varNum);
}
This will allow you to dynamically fill the ArrayList dependent upon how many values it needs to hold.
Better use an ArrayList then, and use their .add() method
someArr.add(varNum)
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 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
I have a list of Portfolio objects in java collection. This Portfolio object has a property called scoringRule. I want to calculate how much percentage of Portfolio objects are present in the list for each type of scoringRule. Actually, I have only 2 different types of scoring rules. The scoringRUle property can be either "single" or "double". How can I do this?
Class Portfolio{
private String name;
private String type;
private String scoringRule;
}
List<Portfolio> portfolios = new ArrayList<Portfolio>();
This is your solution.
double single = 0;
double doublee = 0;
for (Portfolio pobj : portfolios) {
if(pobj.scoringRule.equals("single"))
single++;
else
doublee++;
}
System.out.println("single Percentage : " +(single / list.size() )*100);
System.out.println("doublee Percentage : " + (doublee / list.size())*100);