Arrays not getting values - android

I have been working on this yahtzee project and have run into a problem. The dice_number array doesn't seem to be getting the randomly generated values. The oneScore TextView always displays "--". I'm posting my code. Thanks in advance for any help given. Also if you need to see anymore of the code let me know.
switch (v.getId()) {
case R.id.rollBtn:
for(i = 0; i < 5; i++){
randnum = random.nextInt(5);
if(i == 0){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 1){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 2){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 3){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 4){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 5){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
}
break;
case R.id.onesBtn:
for (i = 0; i < 5; i++)
{
if (dice_number[i] == 1) {
dice_count[0] += 1;
oneScore.setText(Integer.toString(dice_count[0]));
}
else
oneScore.setText("--");
}

That second for loop re-sets the text for each number. So if you have an array
dice_number = {3, 2, 1, 1, 4}
The TextView will be set to --, then --, then 1 (if dice_count[0] was 0), then 2, then --, so all you will see is the result from dice_number[4], which is the last --.
You need to either construct a string or use multiple TextViews to see the whole array, not just the last element.
Also, the if statements in the first for loop aren't doing anything as far as I can tell.

Not an answer, just a suggested refactoring:
case R.id.rollBtn:
for(i = 0; i < 5; i++){
randnum = random.nextInt(5);
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
break;
in fact that piece of code that says if(i == 5) {..} will never be reached because the value of i is always 0,1,2,3, or 4.

Related

Android Studio count not outputting number correctly

I made a code to show the amount of times a vowel is used. A, E, I, O, U. The code is counting but it is not conunting correctly.
#Override
public void onClick(View v) {
int letter1 = 0;
int letter2 = 0;
String enter1 = str1.getText().toString();
String enter2 = str2.getText().toString();
for (int i = 0; i < str1.length(); i++) {
if (enter1.charAt(i) == 'a' || enter1.charAt(i) == 'e' || enter1.charAt(i) == 'i' || enter1.charAt(i) == 'o' ||
enter1.charAt(i) == 'u')
{
letter1++;
}
for(int j = 0; j < str2.length(); j ++){
if(enter2.charAt(j) == 'a' || enter2.charAt(j) == 'e' || enter2.charAt(j) == 'i' || enter2.charAt(j) == 'o' ||
enter2.charAt(j) == 'u')
{
letter2++;
}
}
display3.setText("The amount of vowels are :"+ letter1 + " & " + letter2);
}
}
});
My output for Hello World is 2 and 5. 2 for Hello is correct but 5 for World is not. What am I missing?
If we reformat the code slightly, the problem is more apparent:
for (int i = 0; i < str1.length(); i++) {
if (enter1.charAt(i) == 'a' || enter1.charAt(i) == 'e' || enter1.charAt(i) == 'i' || enter1.charAt(i) == 'o' ||
enter1.charAt(i) == 'u') {
letter1++;
}
for (int j = 0; j < str2.length(); j++) {
if (enter2.charAt(j) == 'a' || enter2.charAt(j) == 'e' || enter2.charAt(j) == 'i' || enter2.charAt(j) == 'o' ||
enter2.charAt(j) == 'u') {
letter2++;
}
}
display3.setText("The amount of vowels are :" + letter1 + " & " + letter2);
}
As you can see, the second for loop is inside the first one. That means the second number will be N times larger than it should be, where N is the length of the first word. This matches what's happening: there is only 1 vowel in "world" but there are 5 letters in "hello".
The display3.setText(...) part will also be executed N times, but since it overwrites the previous value you won't be able to notice that, and you'll just see the final value.
To fix, just close the first for loop earlier:
for (int i = 0; i < str1.length(); i++) {
if (enter1.charAt(i) == 'a' || enter1.charAt(i) == 'e' || enter1.charAt(i) == 'i' || enter1.charAt(i) == 'o' ||
enter1.charAt(i) == 'u') {
letter1++;
}
}
for (int j = 0; j < str2.length(); j++) {
if (enter2.charAt(j) == 'a' || enter2.charAt(j) == 'e' || enter2.charAt(j) == 'i' || enter2.charAt(j) == 'o' ||
enter2.charAt(j) == 'u') {
letter2++;
}
}
display3.setText("The amount of vowels are :" + letter1 + " & " + letter2);
Something problem with the brackets
#Override
public void onClick(View v) {
for(Loop){
if(condition){
}
}//first loop
for(Loop){
if(condition){
}
}//second loop
//display here
});//end for onClick

multiple background color to recyclerview item's TextView at n intervals in android

I need to make my RecyclerView item's TextView display multiple background color. Let's say i have 7 different color codes that i need to show after every 7 items. Here's my approach towards it. Please help!!
#Override
public void onBindViewHolder(BuyCategoriesViewHolder holder, final int position) {
holder.tv_name.setText(category.get(position).getCategory());
char firstChar=category.get(position).getCategory().charAt(0);
holder.tv_title.setText(String.valueOf(firstChar));
if(position == 0){
holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color1));
}else if(position %7 == 0){
holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color2));
}else if(position %6 == 0){
holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color3));
}else if(position %5 == 0){
holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color4));
}else if(position %4 == 0){
holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color5));
}else if(position %3 == 0){
holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color6));
}else if(position %2 == 0){
holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color7));
}else {
holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color1));
}
//holder.tv_title.setBackgroundResource(R.drawable.shape_circle);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
callItemViewListener.callItemView(position);
}
});
}
You are using the modulus operator in the wrong order. Try it like this:
if (position % 7 == 0) {
holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color1));
} else if (position % 7 == 1) {
holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color2));
} else if (position % 7 == 2) {
holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color3));
} else if (position % 7 == 3) {
holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color4));
} else if (position % 7 == 4) {
holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color5));
} else if (position % 7 == 5) {
holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color6));
} else if (position % 7 == 6) {
holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color7));
}
Or with a switch statement:
int colorRes = 0;
switch(position % 7) {
case 0: colorRes = R.color.list_color1;
break;
case 1: colorRes = R.color.list_color2;
break;
case 2: colorRes = R.color.list_color3;
break;
case 3: colorRes = R.color.list_color4;
break;
case 4: colorRes = R.color.list_color5;
break;
case 5: colorRes = R.color.list_color6;
break;
case 6: colorRes = R.color.list_color7;
break;
}
holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext, colorRes));
Edit
For completeness, incorporate #iClaude 's answer with an example.
This defines an array of color resources and then uses the modulus operator to get the correct index for the array:
// first define colors
private final int[] backgroundColors = {
R.color.list_color1,
R.color.list_color2,
R.color.list_color3,
R.color.list_color4,
R.color.list_color5,
R.color.list_color6,
R.color.list_color7 };
// in onBindViewHolder
int index = position % backgroundColors.length;
int color = ContextCompat.getColor(mContext, backgroundColors[index]);
holder.tv_title.setBackgroundColor(color);
Use This inside of Adapter Class
String[] mColors = {"#3F51B5", "#FF9800", "#009688", "#673AB7", "#999999", "#454545", "#00FF00",
"#FF0000", "#0000FF", "#800000", "#808000", "#00FF00", "#008000", "#00FFFF",
"#000080", "#800080", "#f40059", "#0080b8", "#350040", "#650040", "#750040",
"#45ddc0", "#dea42d", "#b83800", "#dd0244", "#c90000", "#465400",
"#ff004d", "#ff6700", "#5d6eff", "#3955ff", "#0a24ff", "#004380", "#6b2e53",
"#a5c996", "#f94fad", "#ff85bc", "#ff906b", "#b6bc68", "#296139"};
productViewHolder.brandname.setBackgroundColor(Color.parseColor(mColors[i % 40]));
Create an array (0 to 6) with your colors, then use position % 7 to get the offset in the array (the color).
By doing so you also get rid of that ugly if..else construct with many branches.

Check textures overlapping

Hey i am writing a game when there are bunch of textures that changes locations, but i want to check that they are not drawing over other textures, i tried to write a code for checking it but its not working well.
Here is the code i tried:
circles.setPosition(new Vector2(r.nextInt(width-height/8*2)+height/8,
r.nextInt(height-height/8*2)+height/8),
i);
circl[i].set((float) (circles.getPosition(i).x+height/16),
(float) (circles.getPosition(i).y+height/16),
height/16);
while(isLaping = true){
System.out.println("in");
for(int y = 0; y < circlesArray.length-1; y++){
if(Intersector.overlaps(circl[y], circl[y+1])){
circles.setPosition(new Vector2(r.nextInt(width-height/8*2)+height/8,
r.nextInt(height-height/8*2)+height/8),
i);
circl[i].set((float) (circles.getPosition(i).x+height/16),
(float) (circles.getPosition(i).y+height/16),
height/16);
}else{
isLaping = false;
}
}
}
How to fix it?
this is confusing,
while(isLaping = true){
you mean it
while(isLaping == true){
or
while(isLaping){
while(isLaping=true); isLaping never is false at the moment evaluate while because asign true every time
I tried again to do so with your tip but the circle are getting crazy and changes places every second
this is the code i have been trying.
for(int i = 0;i<circlesArray.length;i++)
{
if(circlesArray[i]>200)
{
changePosition(i);
}
circlesArray[i]++;
}
private void changePosition(int i)
{
int s = 0;
circles.setPosition(new Vector2(r.nextInt(width-height/12*2)+height/8,r.nextInt(height-height/12*2)+height/12),i);
circl[i].set((float) (circles.getPosition(i).x+height/24),(float) (circles.getPosition(i).y+height/24),height/24);
while (lap == true)
{
circles.setPosition(new Vector2(r.nextInt(width-height/12*2)+height/8,r.nextInt(height-height/12*2)+height/12),i);
circl[i].set((float) (circles.getPosition(i).x+height/24),(float) (circles.getPosition(i).y+height/24),height/24);
for(int y= i+1;y<circlesArray.length;y++)
{
if(circl[i].overlaps(circl[y]))
{
circles.setPosition(new Vector2(r.nextInt(width-height/12*2)+height/8,r.nextInt(height-height/12*2)+height/12),i);
circl[i].set((float) (circles.getPosition(i).x+height/24),(float) (circles.getPosition(i).y+height/24),height/24);
s=0;
}
else
{
s++;
}
}
if(s == circlesArray.length)
lap = false;
}
circlesArray[i] = 0;
x[i] = r.nextInt(circleTexture.length);
}

Hiding textview in a listview based on dynamic position

In my listview I have 3 textviews and I want to hide one textview at some positions.
The positions at which TextView must hide comes at runtime based on some condition.
If I get the indexes I want to hide, in an array and use this logic,then it hides the TextView only for the last index. please help.
int []x={1,2,4};
for(int i=0;i<x.length;i++){
if(position==x[i]){
holder.time.setVisibility(View.GONE);
}
else{
holder.time.setVisibility(View.VISIBLE);
}
}
Here
if(position==x[i]){
holder.time.setVisibility(View.GONE);
}
else{
holder.time.setVisibility(View.VISIBLE);
}
change this to
if(position==x[i]){
holder.time.setVisibility(View.GONE);
break;
}
else{
holder.time.setVisibility(View.VISIBLE);
}
OR
do something like this
int []x={1,2,4};
boolean exists = false;
for(int i=0;i<x.length;i++)
{
if(position==x[i]){
exists = true;
break;
}
}
if (exists)
holder.time.setVisibility(View.GONE);
else
holder.time.setVisibility(View.VISIBLE);
Try using OR condition, instead of for loop.
if (position == 1 || position == 2 || position == 4)
{
holder.time.setVisibility(View.GONE);
}
else
{
holder.time.setVisibility(View.VISIBLE);
}
EDIT --- You can try this way also
int []x={1,2,3,4};
boolean hideMe ;
for(int i = 0; i < x.length; i++)
{
hideMe = false;
if(position == x[i])
{
hideMe = true;
break;
}
}
if (hideMe)
holder.time.setVisibility(View.GONE);
else
holder.time.setVisibility(View.VISIBLE);

Check for Empty EditText fails

I've tried everything from matching strings, to using TextUtils.isEmpty. No matter what I do, b is always true (even when edittext is purposely left blank) which allows the code to proceed to the next steps (this is a Madlib app).
If anybody can see why the code isn't properly checking for blank edittext's and displaying the Please Fill In All Fields" toast when one is blank, it would be very appreciated. Thanks. Sorry for the messy code.
public class Madlibs extends Fragment {
switch (((MainActivity) getActivity()).getStory()) {
case 0:
output.setText(Stories[0]);
title = Titles[0];
actionBar.setTitle(title);
editTextNumber = 12;
addEdit = new BootstrapEditText[editTextNumber];
for (int i = 0; i < addEdit.length; i++) {
addEdit[i] = new BootstrapEditText(getActivity());
l_layout.addView(addEdit[i]);
params.setMargins(0, 20, 0, 20);
addEdit[i].setLayoutParams(params);
addEdit[i].setId(i);
}
addEdit[0].setHint("Name of Sickness");
addEdit[1].setHint("Adjective");
addEdit[2].setHint("Name of Boy");
addEdit[3].setHint("Body Part");
addEdit[4].setHint("Color");
addEdit[5].setHint("Animal");
addEdit[6].setHint("Article of Clothing");
addEdit[7].setHint("Relative");
addEdit[8].setHint("Adjective");
addEdit[9].setHint("Article of Clothing");
addEdit[10].setHint("Body Part");
addEdit[11].setHint("Number");
break;
case 1:
// fragment = new Madlibs();
break;
case 2:
// fragment = new MadlibsSaved();
}
convert = (BootstrapButton) view.findViewById(R.id.convert);
convert.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i;
String s;
for (i = 0; i < addEdit.length; i++) {
s = addEdit[i].getText().toString().trim();
if (s.isEmpty() || s.length() == 0 || s.equals("") || s == null) {
b = false;
}
else
{
b = true;
}
}
if (b = true) {
gather();
postIt();
outputText = output.getText().toString();
} else {
Toast.makeText(getActivity().getApplicationContext(),
"Please Fill In All Fields", Toast.LENGTH_LONG)
.show();
}
}
});
Your problem is here
if (b = true) { // HERE
gather();
postIt();
outputText = output.getText().toString();
}
You are using b = true which sets b to true, and since you are able to do this successfully, the conditional evaluates to true every time. What you want instead is the comparison operator ==
if (b == true) {
gather();
postIt();
outputText = output.getText().toString();
}
or even better, since the variable you are comparing is a boolean, you could just use
if (b) {} //This is "if b is true..."
if (!b) {} //This is "if b is false..."
You can see another one of my answers about this here.
for (i = 0; i < addEdit.length; i++) {
s = addEdit[i].getText().toString().trim();
if (s.isEmpty() || s.length() == 0 || s.equals("") || s == null) {
b = false;
}
else {
b = true;
}
}
For Loop ends here and the following is outside the For Loop :
if (b = true) {
gather();
postIt();
outputText = output.getText().toString();
} else {
Toast.makeText(getActivity().getApplicationContext(),
"Please Fill In All Fields", Toast.LENGTH_LONG)
.show();
}
Problem 1 :
if(b == true) {
}
Problem 2 :
Now, suppose edittext 1 is empty, b is set to true but then it'll straightaway move ahead in the loop to edittext 2 to the last edittext which are not empty hence it'll set b to false again. You'll need to put the check inside the for loop.

Categories

Resources