• 100% Satisfaction Guarantee

Leela-TheProgrammer, Computer Software Engineer
Category: Programming
Satisfied Customers: 474
Experience:  Post Grad in CS (Gold Medal)
21067471
Leela-TheProgrammer is online now

# Do you know how to how to check if elements in rows

### Customer Question

Hello,

My code below is meant to only print "YES" when it finds a row that contains all ones in java but when it finds a column that contains all ones it still prints "YES"! How can I fix my code below so that it only prints "YES" when it only finds a row all equal to 1?

Many thanks,

Samia

import java.util.*;
public class fixst
{
public static void main(String [] args)
{ int total =0;
int [][] a = {{1,7,2},{7,1,8}}; //2D ARRAY
boolean yes=false;
//TO CHECK IF THERE'S A ROW IN 2D ARRAY THAT HAS ALL 1s
for(int i = 0;i<a.length; i ++) //loops through rows
{
for(int j = 0; j<a[i].length; j ++) //loops through columns
{
int num = a[i][j];

if(num == 1) //everytime number is ***** adds to total
{
total++;
}

if (total==a[0].length) //if total equals to rows length it means all elements in a row are equal to 1

{
yes = true;

}

}

}
if(!yes)
{
System.out.println("NO");
} else {
System.out.println("YES elements in a row are all equa to 1! ");
}

}
}

Submitted: 1 year ago.
Category: Programming
Expert:  Leela-TheProgrammer replied 1 year ago.
Hi, Thanks for using JustAnswer.This is Leela and I will help you with this question today. I will check the code and provide you a solution. Thanks,Leela
Customer: replied 1 year ago.

Thanks :) please as soon as possible as i've got a deadline at 11:45pm tonight.

Expert:  Leela-TheProgrammer replied 1 year ago.
Sure. I am working on it. Will provide you in max 30 minutes.
Customer: replied 1 year ago.

Also if possible and you have time, I have one more question which i'm really struggling with. I can pay you more if you can please write the following program in java and explain to me how it works...

Write a program that prints “YES” if in each row of A, all the elements are distinct and “NO” otherwise. (Hint: First write a function that takes as input a one-dimensional array and checks if all elements in this array are distinct. Then, apply this function to each row of a matrix (treating this row as a one dimensioal array). Print “YES” if and only if the function returns TRUE for each row.

Expert:  Leela-TheProgrammer replied 1 year ago.
Please find code below: import java.util.*;public class fixst { public static void main(String[] args) { int total = 0; int[][] a = { { 1, 1, 2 }, { 3, 1, 1 } }; // 2D ARRAY boolean yes = true; //variable to check if a specific row has all 1's int rowCount_1 = 0; // variable to track the number of rows which has all 1's // TO CHECK IF THERE'S A ROW IN 2D ARRAY THAT HAS ALL 1s for (int i = 0; i < a.length; i++) // loops through rows { yes = true; // by default set it to true for (int j = 0; j < a.length; j++) // loops through columns { int num = a[j]; //if any number other than 1 is found, then set yes to false and stop processing current row if (num != 1) { yes = false; break; } } // When a row is processed check if that row has all 1's, accordingly increment 1's rowcount if (yes) { rowCount_1++; } } // if 1s row count is > 0 that means there is atleast ONE row with all 1s if (rowCount_1 > 0) { System.out.println("YES elements in atleast one row are all equal to 1! "); } else { System.out.println("NO"); } }}Please let me know if you have any queries. I can help you with your other question also, please post a question for ME so that i can answer it for you.
Expert:  Leela-TheProgrammer replied 1 year ago.
I started working on the SECOND PROGRAM also.PLEASE ACCEPT THE PROPOSAL and SHARE YOUR DETAILS mail, skype or Google chat via PRIVATE CONTACT INFO so that I can contact you quickly and explain the program. I will provide a solution in FEW MINUTES, please post a message as soon as you see my message.
Leela-TheProgrammer, Computer Software Engineer
Category: Programming
Satisfied Customers: 474
Experience: Post Grad in CS (Gold Medal)
Expert:  Leela-TheProgrammer replied 1 year ago.
Please provide your email or skype id via PRIVATE CONTACT info securely. I shared my contact details in PRIVATE CONTACT INFO section, please check them and contact me. I started working on the 2nd question and I have a DOUBT.. Should i print YES when atleast 1 ROW has all distinct values OR only when ALL ROWS have distinct values? Thanks,Leela
Customer: replied 1 year ago.

Only when ALL ROWS have distinct values please. Sorry copied the code you sent me into Bluej and I got this error for a[j] which says int[] can not be converted to int! Also I cannot do skype tonight but I can on another day when your free. For the meantime can you please clearly comment on the code.

Many thanks,

Samia

Expert:  Leela-TheProgrammer replied 1 year ago.
Hi Samia,About question 2: OK. I am clear about 2nd question.About question 1: About the error you are seeing in Bluej, In my code there is no a[j], it should be a[j]. Please check it let's quickly check the same via remote session. Please click on the LINK BELOW and we can do a quick remote session.http://www.justanswer.com/remotedesktop/join.aspx?Session=96a22933-00fd-4fe8-ab79-567e50537f60 Thanks,Leela
Expert:  Leela-TheProgrammer replied 1 year ago.
It should be a [j] (remove space between and [j]) there is problem in the chat window it seems.
Expert:  Leela-TheProgrammer replied 1 year ago.
There is a problem in the CHAT WINDOW CODE IS NOT COMING properly, please provide your email address, I will email you the CODE. It should bea [ i ] [ j ]remove all the spaces.
Customer: replied 1 year ago.

I'm afraid i'm still having the same problem with my code. My email is***@******.***

Expert:  Leela-TheProgrammer replied 1 year ago.
There is problem with the CHAT window. Please check your email and acknowledge the receipt of it.
Expert:  Leela-TheProgrammer replied 1 year ago.
Did you get the CODE for the 1st program?
Customer: replied 1 year ago.

Yes I did. Thank you I will shortly provide you with a ratting. It finally fully works now :)

Expert:  Leela-TheProgrammer replied 1 year ago.
Thanks for acknowledging. I am working on the SECOND program Will provide it in 15 minutes.
Leela-TheProgrammer, Computer Software Engineer
Category: Programming
Satisfied Customers: 474
Experience: Post Grad in CS (Gold Medal)
Expert:  Leela-TheProgrammer replied 1 year ago.
Completed the LOGIC, adding comments and sending you the code in 5 minutes.
Expert:  Leela-TheProgrammer replied 1 year ago.
Sent the java code by email, use that code and let me know if there are any queries. Providing same code on chat also, but it may have some problems, so use it from email. import java.util.*;public class RowAllDistinct { public static void main(String[] args) { int total = 0; int[][] a = { { 1, 3, 2 }, { 3, 2, 1 } }; // 2D ARRAY boolean yes = true; // variable to check if a specific row has all 1's int rowCount_distinct = 0; // variable to track the number of rows which has // all different elements // TO CHECK IF THERE'S A ROW IN 2D ARRAY THAT HAS ALL 1s for (int i = 0; i < a.length; i++) // loops through rows { yes = areAllRowElementsSame(a); // When a row is processed check if that row has all disctint elements // accordingly increment distinct rowcount if (yes) { rowCount_distinct++; } } // if 1s row count is > 0 that means there is atleast ONE row with all // 1s if (rowCount_distinct == a.length) { System.out .println("YES elements in ALL rows are distinct! "); } else { System.out.println("NO"); } } public static boolean areAllRowElementsSame(int[] row) { if (row.length == 0) { return true; } else { //set cannot have duplicates Set mySet = new HashSet(); //add each element in array to set for(int elem:row) mySet.add(elem); //if size of set and row length are same means // the row has all different elements //System.out.println("row as list: " + Arrays.asList(row)); //System.out.println("set size:" + mySet.size() + " row length:" + row.length); //if set size is same as array row size that means set has all the elements in array row // which means array row has ALL distinct elements if (mySet.size() == row.length) { return true; }else{ return false; } } }}
Expert:  Leela-TheProgrammer replied 1 year ago.
Please ACKNOWLEDGE the receipt of the CODE and also let me know if you have any queries. If you need my help in the future just post a question starting with "FOR TOUGHGUY ONLY"and I will be notified of the question and I can quickly help you. Incase i am not available to help some other expert will help you, that way you get timely solution. All the best. Thanks,Leela
Expert:  Leela-TheProgrammer replied 1 year ago.
I clearly documented the logic. Please let me know if you have any queries. Submit the assignment soon and I can clarify all your doubts if any later.
Customer: replied 1 year ago.

Thank you it works. I understand everything but this:

//set cannot have duplicates
Set mySet = new HashSet();
//add each element in array to set
for(int elem:row)

I've never seen this!

Many thanks,

Samia

Expert:  Leela-TheProgrammer replied 1 year ago.
Java has a type called set, which cannot have duplicate values, we are using that to find if there are any duplicate values in row. All the best.

### What Customers are Saying:

• My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer
< Previous | Next >
• My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer
• Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C.
• This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex
• Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP
• I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin
• Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther
• Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C.

• ### ATLPROG

#### Satisfied Customers:

7260
MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
< Previous | Next >

### ATLPROG

#### Satisfied Customers:

7260
MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML

### LogicPro

#### Satisfied Customers:

5514
Expert in C, C++, Java, DOT NET, Python, HTML, Javascript, Design.

### lifesaver

#### Satisfied Customers:

936
Several years of intensive programming and application development experience in various platforms.

### ehabtutor

#### Satisfied Customers:

766
Bachelor of computer science, 5+ years experience in software development, software company owner

### Eljon

#### Satisfied Customers:

558
Founder of StockCanvas.com

### Rafael Martins

#### Satisfied Customers:

446
Desktop, Mobile and Web Developer. 7+ years of experience. Creative solutions provider.

### The-PC-Guy

#### Satisfied Customers:

320
Extensive Knowledge in PHP, MYSQL, CSS & Javascript