Use the method public static boolean is ConsecutiveFourint

Use the method { public static boolean is ConsecutiveFour(int[][] values) } to test whether two dimensional array has four consecutive numbers of the same value , either horizontally , vertically , or diagonally.(java please)

1-nstead of having your program get input from the keyboard make it read the data from files (num1.txt, num2.txt,num3.txt,num4.txt,num5.txt,num6.txt)

example : num6.txt

0 1 0 3 1 6 1

0 1 6 8 6 0 1

5 5 2 2 8 2 9

6 4 2 2 2 2 1

1 5 6 2 4 0 7

3 4 3 2 4 0 7

num5.txt

0 0 0 0 1 6 1

0 1 6 8 6 1 1

9 6 2 1 1 2 2

6 9 6 1 1 9 2

1 3 9 1 4 0 2

3 3 3 9 4 0 2

2-Pass the name of the file into your program as a command line argument...Look at CmdLine.java for an example of this!

import java.util.*;

import java.io.*;

public class Cmdline {

   public static void main(String[] args)throws FileNotFoundException{

      System.out.println(\"args[0]is \"+args[0]);

   Scanner s=new Scanner(new File(args[0]));

      while(s.hasNext()){

      int x=s.nextInt();

      System.out.println(\"x is \"+x);

      }

      s.close();

Solution

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class Lab5c {

public static void main(String[] args) {
Scanner input = null;
       try {
           input = new Scanner(new File(args[0]));
       } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       int[][] m = new int[100][100];
       int x=-1,y=0;
       while(input.hasNextLine())
       {
           String line=input.nextLine();
           x++;
           y=line.split(\" \").length;
           String[] array=line.split(\" \");
           for (int i = 0; i < array.length; i++) {
               m[x][i]=Integer.valueOf(array[i]);
           }
          
       }
  

System.out.println(isConsecutiveFour(m));

}

public static boolean isConsecutiveFour(int[][] values) {

// checking rows
for (int i = 0; i < values.length; i++) {
int current = values[i][0];
int consecutiveCount = 0; // values[i][0] starts count

for (int j = 0; j < values[i].length; j++) {

if (values[i][j] == current) {
consecutiveCount++;
if (consecutiveCount == 4) return true;
} else {
current = values[i][j];
consecutiveCount = 1;
}
}
}
// check columns
for (int j = 0; j < values[0].length; j++) {
int consecutiveCount = 0; // values[0][j] starts count
int current = values[0][j];

for (int i = 0; i < values.length; i++) {

if (values[i][j] == current) {
consecutiveCount++;
if (consecutiveCount == 4) return true;
} else {
current = values[i][j];
consecutiveCount = 1;
}

}
}

// check topLeft side: going upright
for (int i = values.length - 1; i > 0; i--) {
int y = i;
int x = 0;
int consecutive = 0;
int current = values[y][x];

while (y >= 0) {
System.out.println(\"y = \" + y + \" x = \" + x);
if (values[y][x] == current) {
consecutive++;
if (consecutive == 4) return true;
} else {
consecutive = 1;
current = values[y][x];
}
x++;
y--;
}
}

// check bottom right side: going upright
for (int j = 0; j < values[0].length; j++) {
int y = values.length - 1;
int x = j;
int consecutive = 0;
int current = values[y][x];

while (x < values[0].length && y >= 0) {
System.out.println(\"y = \" + y + \" x = \" + x);
if (values[y][x] == current) {
consecutive++;
if (consecutive == 4) return true;
} else {
consecutive = 1;
current = values[y][x];
}
x++;
y--;
}

}

// check bottom left side going up-left
for (int j = values[0].length - 1; j > 0; j--) {

int x = j;
int y = values.length - 1;
int current = values[y][x];
int consecutiveCount = 0;

while (x >= 0 && y >= 0) {

if (values[y][x] == current) {
consecutiveCount++;
if (consecutiveCount == 4) return true;
} else {
consecutiveCount = 1;
current = values[y][x];
}

x--;
y--;
}
}
// check bottom right side going up-left
for (int row = 1 ; row < values.length; row++) {
int x = values[0].length - 1;
int y = row;
int consecutive = 0;
int current = values[y][x];

while (y >= 0) {
System.out.println(\"y = \" + y + \" x = \" + x);
if (values[y][x] == current) {
consecutive++;
if (consecutive == 4) return true;
} else {
consecutive = 1;
current = values[y][x];
}
x--;
y--;
}

}
return false;
}

}

Use the method { public static boolean is ConsecutiveFour(int[][] values) } to test whether two dimensional array has four consecutive numbers of the same value
Use the method { public static boolean is ConsecutiveFour(int[][] values) } to test whether two dimensional array has four consecutive numbers of the same value
Use the method { public static boolean is ConsecutiveFour(int[][] values) } to test whether two dimensional array has four consecutive numbers of the same value
Use the method { public static boolean is ConsecutiveFour(int[][] values) } to test whether two dimensional array has four consecutive numbers of the same value

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site