boolean from array - function in java, input from command line - java

I have a problem, which read N numbers from the command line, and I want to check if all those numbers are the same.
This is my code
class q2 {
public static boolean check(int[] arr) {
for (int i = 0; i < arr.length-1; i++) {
for (int j = 1; j < arr.length; j++) {
if (arr[i]==arr[j]) return false;
}
}
return true;
}
public static void main(String[] args) {
int[] arr = new int[args.length];
boolean result = check(arr);
System.out.println(result);
}
}
but in any case (but no input at all) it return false and I don't know where is the mistake.

If you are passing the values from the command line, you need to add those values into the array that you will pass into the check method:
public static void main(String[] args) {
int[] arr = new int[args.length];
for(int i = 0; i < arr.length; i++){
arr[i] = Integer.parseInt(args[i]);
}
boolean result = check(arr);
System.out.println(result);
}
You can simplify your check method e.g. you do not need a double loop. Just compare the current element with the next one as shown below:
public static boolean check(int[] arr) {
for (int i =0;i<arr.length-1;i++)
if (arr[i] != arr[i + 1])
return true;
return false;
}

Related

Editing and adding the sum of java arrys

I am trying to iterate over an array and add the sum of the array except the number 13 and the number after it.
Example
[1,1,1,1,13,2] = [1,1,1,1,0,0] = 4
this is what I have so far the main things I need to know is how do I check if the array has a number 13 in it and how do I change it to a 0
public static int sum13(int[] nums) {
for(int i=0; i < nums.length; i++) {
if(nums.indexOf(i) == 13) {
}
}
}
public static void main(String[] args) {
//this is the main method
int[] a = {1,2,3,13,4};
sum13(a);
}
}
You can try this , to skip adding all number when you get 13 in your array :
public static int sum13(int[] nums) {
int sum = 0;
for(int i=0; i < nums.length; i++) {
if(nums[i] == 13) {
break;
}
sum += nums[i];
}
return sum;
}
public static void main(String[] args) {
//this is the main method
int[] a = {1,2,3,13,4};
System.out.println(sum13(a));
}
Try this.
public static int sum13(int[] nums) {
return IntStream.of(nums).takeWhile(i -> i != 13).sum();
}

Freeman Chain Code in Java

Okay, hopefully this makes more sense. I have an array hard coded with only 1s and 0s. I am trying to write a function that reads each element to see if it is a 0 or 1. If it is a 1, it will execute another function and then change that 1 to a 0 so that it is not read again. I have it printing simply as a placeholder for the other function I will be implementing later. I'm having trouble getting the findfirst1 function to check every element in the array. I have tried putting the incrementors for i and k in different places within the flow of the code but nothing I have tried gets me the correct output.
public static void main(String[] args)
{
int[][] testarray = {{1,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{1,0,0,0,0,0,0,1}};
findfirst1(testarray);
}
public static void findfirst1(int[][] array1)
{
int value = 0;
for(int i = 0; i < 6;i++)
{
for(int k = 0; k < 7;k++)
{
value = array1[i][k];
if(value == 1)
{
System.out.println(value);
array1[i][k] = 0;
}
else
{
System.out.println(value);
}
}
}
}
Okay, so after starting completely over and writing it all from scratch I figured it out. The array.length was right all along. I had trouble wrapping my head around it because I was so focused on the idea of the "image".
Edit: I just found an error where it wouldn't print the last line for array1, so I just added an extra row of 0s and it worked.
public class ChainCodeClass {
public static void main(String[] args)
{
int[][] array1 = {{0,1,0,0,0,0,0,0},{0,1,0,0,0,1,0,0},{0,1,1,1,0,0,1,0},
{0,0,0,1,1,0,0,1},{0,0,0,0,1,0,0,1},{0,0,0,0,0,1,0,0},
{0,0,0,0,0,0,1,0}**,{0,0,0,0,0,0,0,0}**};
int[][] array2 = {{0,0,0,0,0,0,0,0},{0,1,1,1,1,0,1,0},{0,0,0,0,1,0,1,0},
{0,0,1,1,1,0,1,0},{0,0,1,0,0,0,1,0},{0,0,1,0,0,0,1,0},{0,0,0,0,0,0,1,1},
{0,0,0,0,0,0,0,0}};
System.out.print("First Image");
print(array1);
findfirst1(array1);
print(array1);
System.out.print("Second Image");
print(array2);
outline8(array2);
}
public static void findfirst1(int[][] array)
{
int value = 0;
for(int i = 0; i < array.length; i++)
{
for(int k = 0; k < array.length; k++)
{
value = array[i][k];
if(value == 1)
{
System.out.print(value + " ");
array[i][k] = 0;
}
else
{
System.out.println(value);
}
}
}
}
public static void print(int[][] array)
{
for(int i = 0; i < array.length; i++) // print function for the array using incrementors
{
System.out.print("\n");
for(int k = 0; k < array.length; k++)
{
System.out.print(array[i][k] + " ");
}
}
System.out.println();
}
}

Return all negative elements of a array

Okay, so i need to find all the negative numbers of array and return them.I found the negative number, but how do i return them all? P.S yes i am a beginner.
public static void main(String[] args) {
int [] array = {5,-1,6,3,-20,10,20,-5,2};
System.out.println(findNumber(array));
}
public static int findNumber(int[] sum) {
int num = 0;
for (int i = 0; i < sum.length ; i++) {
if(sum[i] < num) {
num = sum[i];
}
}
return num;
}
Java 8 based solution. You can use stream to filter out numbers greater than or equal to zero
public static int[] findNumber(int[] sum)
{
return Arrays.stream(sum).filter(i -> i < 0).toArray();
}
There are multiple ways of doing this, if you just want to output all of the negative numbers easily you could do this:
public static void main(String[] args) {
int [] array = {5,-1,6,3,-20,10,20,-5,2};
ArrayList<Integer> negativeNumbers = findNumber(sum);
for(Integer negNum : negativeNumbers) {
System.out.println(negNum);
}
}
public static ArrayList<Integer> findNumber(int[] sum) {
ArrayList<Integer> negativeNumbers = new ArrayList<>();
for (int i = 0; i < sum.length ; i++) {
if(sum[i] < 0) {
negativeNumber.add(sum[i]);
}
}
return negativeNumbers;
}
As you told you are beginner, i'm giving code in using arrays only.
Whenever you come across a negative number, just add it to the array and increment it's index number and after checking all the numbers, return the array and print it.
public static void main(String[] args)
{
int [] array = {5,-1,6,3,-20,10,20,-5,2};
int[] neg = findNumber(array);
for(int i = 0 ; i<neg.length; i++)
{
System.out.println(neg[i]);
}
}
public static int[] findNumber(int[] a)
{
int j=0;
int[] n = new int[a.length];
for(int i = 0; i<a.length ; i++)
{
if(a[i] <0)
{
n[j] = a[i];
j++;
}
}
int[] neg = new int[j];
for( int k = 0 ; k < j ; k++)
{
neg[k] = n[k];
}
return neg;
}
I hope it helps.
You can modify your method to iterate through the array of numbers, and add every negative number you encounter, to a List.
public static List<Integers> findNegativeNumbers(int[] num) {
List<Integers> negativeNumbers = new ArrayList<>();
for (int i = 0; i < num.length; i++) {
if(num[i] < 0) {
negativeNumbers.add(num[i]);
}
}
return negativeNumbers;
}
You could then print out the list of negative numbers from this method itself, or return the list with return to be printed in main.
You code is returning the sum of elements, but I understood that you wanted every negative number.
So, I assumed you want something like this:
public static void main(String[] args) {
int [] array = {5,-1,6,3,-20,10,20,-5,2};
Integer [] result = findNumbers( array );
for( int i : result )
{
System.out.println( i );
}
}
public static Integer[] findNumbers(int[] v) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < v.length ; i++) {
if(v[i] < 0) {
list.add(v[i]);
}
}
return list.toArray( new Integer[0] );
}
Is it?
Best regards.
public static int[] findNum(int[] array)
{
int negativeIntCount = 0;
int[] negativeNumbers = new int[array.length];
for(int i = 0; i < array.length; i++)
{
if(array[i] < 0)
{
negativeIntCount++;
negativeNumbers[i] = array[i];
}
}
System.out.println("Total negative numbers in given arrays is " + negativeIntCount);
return negativeNumbers;
}
To display as an array in output :
System.out.println(Arrays.toString(findNum(array)));
To display output as space gaped integers :
for(int x : findNum(array))
{
System.out.print(" " + x)
}

calculate the minimum value for each column in 2D array

I have a 2D array , iam trying to calculate the minimum value for each column and put the result in the result array.
the code bellow is calculating the minimum value for each row , how can i get the min value for each column.
import java.util.*;
class Test20 {
public static void main ( String [] args) {
int[][] array = {{6,3,9},
{0,8,2},
{3,7,5}};
Test20 test = new Test20();
System.out.print(Arrays.toString(test.mincol(array)));
}
public static int[] mincol (int[][] n) {
int[] result = new int[n.length];
for (int i = 0; i < n.length; i++) {
int min = n[0][i];
for (int j = 0; j < n[0].length; j++) {
if (n[j][i] < min) {
min = n[j][i];
}
}
result[i] = min;
}
return result;
}
}
Just change the loop the following way:
min = 0;
for(int i=0;i<n.length;i++){
for(int j=0;j<n[0].length;j++){
if(n[j][i]<n[j][min]){
min=j;
}
result[i]=n[min][i];
}
Be aware that you instantiate your result array by the length of the first dimension in your array but later use the n[][] param for looping and access the length of the second dimension in your loop.
If your two dim array is for example 4x5, this will cause ArrayOutOfBoundsExceptions.
You only need to do the same thing but inverting the variables
for(int i=0;i<n.length;i++){
for(int j=0;j<n[0].length;j++){
if(n[j][i]<n[min][j]){
min=i;
}
result[j]=n[min][j];
}
}
If your code is correct just change:
if(n[i][j]<n[i][min]){
min=j;
}
with
if(n[i][j]<n[result[i]][j]){
result[i]=i;
}
finally
for(int i=0;i<n.length;i++) result[i]=n[result[i][j];
you don't need min. But change
int [] result = new int[n.length];
to
int [] result = new int[n[0].length];
How about you transpose your two dimensional array like:
public static int[][] transpose (int[][] original) {
int[][] array = new int[original.length][];
// transpose
if (original.length > 0) {
for (int i = 0; i < original[0].length; i++) {
array[i] = new int[original[i].length];
for (int j = 0; j < original.length; j++) {
array[i][j] = original[j][i];
}
}
}
return array;
}
and then call it as:
System.out.print(Arrays.toString(test.minrow(transpose(array))));
Or, if you want to go without transpose, this is how you can do:
public static int[] mincol (int[][] n) {
int[] result = new int[n.length];
for (int i = 0; i < n.length; i++) {
int min = n[0][i];
for (int j = 0; j < n[0].length; j++) {
if (n[j][i] < min) {
min = n[j][i];
}
}
result[i] = min;
}
return result;
}
Your for loop looks ok. Check the code below I fixed some minor issues.
Based on your code replace Class code with below:
public class Test {
public static void main(String[] args) {
int[][]array={{6,1,9}, {0,1,2}, {3,7,5}};
int[] test;
test = minrow(array);
for(int i=0; i<test.length; i++){
System.out.println(test[i]);
}
}
public static int[] minrow(int[][] n){
int [] result = new int[n.length];
int min;
for(int i=0;i<n.length;i++){
min=0;
for(int j=0;j<n[i].length;j++){
if(n[i][j]<n[i][min]){
min=j;
}
}
result[i]=n[i][min];
}
return result;
}
}

How do I replace duplicate numbers in an integer array?

My question is a very basic one. To give a bit of background, I was given an assignment to write a program that stored 100 unique (i.e no repeats)values into an array and displayed them. I thought I was on the right track earlier, but when I did a test run, all I keep getting is an "out of bounds error." Eclipse tells me it's in the method "checkDouble" but I can't seem to find what's wrong. Can anyone here help me out?
public class exercise2 {
/*#author Paolo
* #param hundredVal array to store values
* #param input random number generator from 1-100
*/
public static int[] hundredVal = new int[100];
public static int n = (int) (Math.random()*100+1);
public static void main(String[] args) {
storeVal();
}
/*
* This method is meant to take 100 random values
* from 1-100 and store them into the array hundredVal
*/
private static void storeVal() {
for (int i = 0; i < hundredVal.length; i++){
hundredVal[i] = (int) (Math.random()*100+1);
if(!checkDouble(hundredVal)){
printVal();
}
}
}
//This method is meant to test if there are any repeated values
public static boolean checkDouble (int[] hundredVal){
for (int i = 0; i < hundredVal.length; i++){
for (int j = 0; i < hundredVal.length; j++){
if (hundredVal[i] == hundredVal [j] && i != j){
return true;
}
}
}
return false;
}
//this just prints out the numbers on the console.
private static void printVal(){
for (int i =0; i < hundredVal.length; i++){
System.out.println(hundredVal[i]);
}
}
}
I don't want anyone to solve the whole assignment for me. I just want to know what is causing the out of bounds error to occur.
class exercise2{
public static int[] hundredVal = new int[100];
public static int n = (int) (Math.random()*100+1);
public static void main(String[] args) {
storeVal();
printVal();
}
private static void storeVal() {
for (int i = 0; i < hundredVal.length; i++){
hundredVal[i] = (int) (Math.random()*100+1);
}
if(!checkDouble(hundredVal)){
printVal();
}
}
public static boolean checkDouble (int[] hundredVal){
for (int i = 0; i < hundredVal.length; i++){
for (int j = 0; j < hundredVal.length; j++){
if (hundredVal[i] == hundredVal [j] && i != j){
return true;
}
}
}
return false;
}
private static void printVal(){
for (int i =0; i < hundredVal.length; i++){
System.out.println(hundredVal[i]);
}
}}
You can go with appropriate java collection - in your case LinkedHashSet should do the job. It's a Set (contains unique values) and it's ordered. It's almost always better to go with collections instead of arrays.

Categories