I need to make a boolean method that reads a original array and a subarray and determines if it contains any positive integers!
The boolean method i have tried NUMEROUS times is the allPositive method!
I cannot get it to return true if they are all positive with text that says:
"All the elements in this array are positive"
and if not say:
"Some elements in this array are negative"
import java.util.*; import java.util.Random;//for Scanner public class Assignment4_3 { public static final int MIN = -10; public static final int MAX = 50; public static final int ROW = 15; public static final int COL = 20; public static void main(String[] args) { //declarations int r, c; boolean pos; char answer; //user input Scanner input = new Scanner(System.in); int[][] a = new int[ROW][COL]; randArray(a, ROW, COL, MIN, MAX); System.out.println("The original table is: "); printArray(a, ROW, COL); originalArray(a, ROW, COL); System.out.println("Do you want to start subarray processing (Y/N)? "); answer = input.next().charAt(0); while(answer == 'y' || answer == 'Y' ){ System.out.println("Enter row size: "); r=input.nextInt(); while( r < 0 || r > ROW){ System.out.println("ERROR! Should be positive and < 15. REENTER: "); r=input.nextInt(); } System.out.println("Enter column size: "); c=input.nextInt(); while(c < 0 || c > COL){ System.out.println("ERROR! Should be positive and < 20. REENTER: "); c=input.nextInt(); } readArray(a, ROW, COL); System.out.println("The subarray is: "); printArray(a, r, c); pos = allPositive(a, r, c); if(pos == true){ System.out.print("Some elements in the original table are positive. \n"); } else{ System.out.print("xxxxxxxxx \n"); } System.out.println("Do you want to continue subarray processing (Y/N)?"); answer = input.next().charAt(0); } } public static void randArray(int[][] matrix, int row, int col, int low, int up) { Random rand = new Random(); for (int r = 0; r < row; r++) for (int c = 0; c < col; c++) matrix[r][c] = rand.nextInt(up - low + 1) + low; } public static void readArray(int[][] matrix, int row, int col) { Scanner input = new Scanner(System.in); for (int r = 0; r < r; r++) for (int c = 0; c < c; c++) matrix[r][c] = input.nextInt(); } public static boolean allPositive(int[][] matrix, int row, int col) { for (int r = 0; r < matrix.length; r++) for (int c = 0; c < matrix[r].length; c++) matrix[r][c] = r + c; if(matrix.length >= 0 && matrix[r].length >= 0){ } return true; } public static void printArray(int[][] matrix, int row, int col) { for (int r = 0; r < row; r++){ for (int c = 0; c < col; c++) System.out.printf("%5d", matrix[r][c]); System.out.println(); } } }
The boolean method i have tried NUMEROUS times is the allPositive method!
I cannot get it to return true if they are all positive with text that says:
"All the elements in this array are positive"
and if not say:
"Some elements in this array are negative"