Java Unreachable code - java

Why is my code returning this error? I've made any mistake? I can not see my error in this code. Can anybody help me?
The code is bellow:
public static void minMax(int m[][])
{
int menor = 0;
int maior = 0;
int i = 0;
int j = 0;
int posicao = 0;
for(i = 0; i < 4;i++)
{
for(j = 0; j < 5; j++)
{
if((i == 0) && (j == 0))
{
menor = m[i][j];
posicao = i;
}
else
{
if(m[i][j] < menor)
{
menor = m[i][j];
posicao = i;
}
}
}
}
for (i = posicao;;)
{
for(j = 0; j < 5; j++)
{
if(j == 0)
{
maior = m[i][j];
}
else
{
if(m[i][j] > maior)
{
maior = m[i][j];
}
}
}
}
System.out.println("\n\nThe smallest element of the array: " + menor);
System.out.println("The line of the smallest element: " + posicao);
System.out.printf("MINMAX element %d encountered at the position: [%d][%d]", maior, i, j);
}
The error is pointed by Eclipse in this line:
System.out.println("\n\nThe smallest element of the array: " + menor);

Looks like your for loop defined as for (i = posicao;;) above your prints never finishes, there is no exit case

Related

How to find common numbers or an anomaly in a 2D array and cause it to trigger something else

I have a 2D array (a matrix of 10x10) with values ranging from 0 to -5.
I want a method to be triggered when there is a sequence of a value found within the array.
For example, there is a sequence of two negative 2. I want it to trigger an event/method that will give a bonus score of 4. This should happen only when there are two -2's and not if there is just one -2.
I tried achieving something like that but I cant figure out how to tell the program to only trigger when 'n' number of a value is found within the matrix.
public class Test {
static int board[][] = new int[10][10];
public static void Test() {
int i, j;
board[0][0] = -1;
board[0][1] = -1;
board[1][1] = -2;
board[1][2] = -2;
board[1][3] = -2;
board[1][4] = -2;
for (i = 0; i < board.length; i++) {
System.out.println("");
for (j = 0; j < board.length; j++) {
//board[i][j] = 0;
System.out.print(board[i][j]);
}
}
System.out.println();
}
public static void scanBoard() {
int i, j;
for (i = 0; i < board.length; i++) {
for (j = 0; j < board.length; j++) {
if (board[i][j] == -1) {
System.out.println("Hello");
}
}
}
}
public static void main(String[] args) {
Test(); //prints out whole array
scanBoard(); //scans for
}
}
public class Main {
static final int size = 10;
static int[][] matrix = new int[size][size];
public static void main(String[] args) {
System.out.println("The first matrix.\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == 3 && j > 3) {
matrix[i][j] = -2; //-2
} else {
matrix[i][j] = 1;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
scanBoard();
System.out.println("\nThe second matrix.\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == 9 && j > 5) {
matrix[i][j] = 2; //changed it from -2 to 2
} else {
matrix[i][j] = 1;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
scanBoard();
}
static void scanBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (matrix[i][j] == -2 && (j + 3 < size)) {
if (matrix[i][j + 1] == -2 && matrix[i][j + 2] == -2 && matrix[i][j + 3] == -2) {
System.out.println("\nThere you go, a special effect!".toUpperCase());
}
}
}
}
}
}
I am not sure if this is the result you wished to see according to your request. I hope this helps you. And I did some changes in your code so it will be easier to read (In my opinion lol).
public class Main {
static final int size = 10;
static int[][] matrix = new int[size][size];
public static void main(String[] args) {
System.out.println("The first matrix.\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == 9 && (j == 0 || j == 1)) {
matrix[i][j] = -2; //-2
} else {
matrix[i][j] = 1;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
scanBoard();
System.out.println("\nThe second matrix.\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == 8 && (j == 5 || j == 6)) {
matrix[i][j] = 2; //changed it from -2 to 2
} else {
matrix[i][j] = 1;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
scanBoard();
}
static void scanBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (matrix[i][j] == -2 && (j + 1 < size)) {
if (matrix[i][j + 1] == -2) {
//You can remove the '.toUpperCase()', it's just my personal preference
System.out.println("\nThere you go, a special effect!".toUpperCase());
}
}
}
}
}
}
From what I understood from the problem statement and comments, you want your scanBoard to behave like this:
public static void scanBoard(int value, int frequency) {
int i, j;
if (value <= 0 && value >= -5 && frequency >= 2 && frequency <= 10) {
for (i = 0; i < board.length; i++) {
int rowFrequency = 0;
for (j = 1; j < board.length; j++) {
if (board[i][j] == value && board[i][j - 1] == value) {
rowFrequency++;
} else {
rowFrequency = 0;
}
if (rowFrequency + 1 >= frequency) {
System.out.println("Hello");
}
}
}
}
}
public static void main(String[] args) {
Test(); //prints out whole array
scanBoard(-2, 4); //prints Hello once
scanBoard(-2, 3); //prints Hello twice
scanBoard(-2, 3); //prints Hello thrice
}

Print a message only once after iterating 2d array

import java.util.Scanner;
public class Note {
public static void main(String[] args) {
String etudiants[][] = new String[1][4];
Scanner saisie = new Scanner(System.in);
for (int i = 0; i < 1; i++) {
System.out.print("\n\nEtudiant BTI00" + (i + 1) + "\n\n");
for (int j = 0; j < 4; j++) {
if (j == 0) {
System.out.print("\n\tCode de l'etudiant : ");
} else if (j == 1) {
System.out.print("\n\tNom etudiant : ");
} else if (j == 2) {
System.out.print("\n\tNote Maths : ");
} else if (j == 3) {
System.out.print("\n\tNote Francais : ");
} else {
System.out.print("\n\tChamps inexistant!");
}
etudiants[i][j] = saisie.nextLine();
}
}
System.out.print("\n\tEtudiants Enregistres : \n\n");
// System.out.print("\tCode\tNom\t\tMaths\tFrancais\n\n");
for (int i = 0; i < 1; i++) {
for (int j = 0; j < 4; j++) {
System.out.print("\t" + etudiants[i][j] + " ");
}
}
System.out.println();
System.out.print("\n\tEntrez code etudiant : ");
String recherche = saisie.nextLine();
boolean trouve = false;
for (int i = 0; i < 1; i++) {
for (int j = 0; j < 4; j++) {
if (recherche.equals(etudiants[i][0])) {
trouve = true;
System.out.print("\n\tCode etudiant correct!");
String math = etudiants[i][2];
String francais = etudiants[i][3];
Double m = new Double(math);
double mathConv = m.doubleValue();
Double f = new Double(francais);
double francaisConv = f.doubleValue();
double moyenne = (mathConv + francaisConv) / 2;
System.out.print("\n\tMoyenne de l'etudiant : " + moyenne);
System.out.print("\n\tEtudiant : " + etudiants[i][j]);
if (moyenne <= 40) {
System.out.print("\n\tEchec!");
} else if (moyenne > 40 && moyenne < 70) {
System.out.print("\n\tReprise!");
} else {
System.out.print("\n\tSucces!");
}
} if (!trouve) {
System.out.print("\n\tCode etudiant incorrect!");
}
}
}
}
}
I need to display only one message after entering the code etudiant but istead it displays the message 4 times. The loop should only iterate through the first column of each line and compares it to what the user entered.
The indexing variable (j) of the last inner for-loop (for (int j = 0; j < 4; j++) {) is never used inside the loop, so you actually do not need that loop at all. General styling issues of your sample aside, you should rewrite the last loop like this:
for (int i = 0; i < 1; i++) {
// throw away this
//for (int j = 0; j < 4; j++) {
if (recherche.equals(etudiants[i][0])) {
trouve = true;
// ... rest of the code like you currently have it
// You probably do not need this line too,
// because you have almost the same in your second loop
//System.out.print("\n\tEtudiant : " + etudiants[i][j]);
}
}

Printing message only once after iterating through the first column of a 2d array

import java.util.Scanner;
public class Grade{
public static void main(String[] args) {
String students[][] = new String[2][4];
Scanner input = new Scanner(System.in);
for (int i = 0; i < 2; i++) {
System.out.print("\n\nStudent 00" + (i + 1) + "\n\n");
for (int j = 0; j < 4; j++) {
if (j == 0) {
System.out.print("\n\tStudent Code : ");
} else if (j == 1) {
System.out.print("\n\tName : ");
} else if (j == 2) {
System.out.print("\n\tMaths Grade : ");
} else if (j == 3) {
System.out.print("\n\tFrench Grade : ");
} else {
System.out.print("\n\tNonexistent field!\n");
}
students[i][j] = input.nextLine();
}
}
System.out.print("\n\tRegistered Students : \n\n");
System.out.print("\tCODE\tFULL NAME\tMATHS\tFRENCH\n\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
System.out.print("\t" + students[i][j] + " ");
}
System.out.println();
}
//Ask for student code.
System.out.print("\n\tStudent Code : ");
String search= input.nextLine();
boolean found = false;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
// found= true;
if (search.equals(students[i][0])) {
found = true;
System.out.print("\n\tStudent Code Found!\n");
String math = students[i][2];
String french = students[i][3];
Double m = new Double(math);
double mathConv = m.doubleValue();
Double f = new Double(french);
double frenchConv = f.doubleValue();
double average = (mathConv + frenchConv) / 2;
System.out.print("\n\tMoyenne de l'etudiant : " + average + "\n");
if (average <= 40) {
System.out.print("\n\tFailure!\n");
} else if (average > 40 && average < 70) {
System.out.print("\n\tReprisal!\n");
} else {
System.out.print("\n\tSuccess!\n");
}
}
else if (!search.equals(students[i][0])) {
found = false;
System.out.print("\n\tCode incorrect!\n");
}
}
}
}
}
I need to display only one message after entering the code etudiant but istead it displays the message 4 times. The loop should only iterate through the first column of each line and compares it to what the user entered.
When you iterate for the search you don't need the second loop to iterate over the attributes, because you don't use it
System.out.print("\n\tStudent Code : ");
String search = input.nextLine();
boolean found = false;
for (int i = 0; i < 2; i++) {
// for (int j = 0; j < 4; j++) { //you don't need this loop
Change this:
for (int j = 0; j < 4; j++)
to this:
for (int j = 0; j < 1; j++)
as we want to run that loop only once.
Alternatively, you can remove the j loop altogether.
it's recommended you use the length argument for those arrays, that way if you ever change the size, the loop won't break, in this case you can just get the first element of the array, and then iterate through that. So intead of
for (int i = 0; i < 2; i++) {
System.out.print("\n\nStudent 00" + (i + 1) + "\n\n");
for (int j = 0; j < 4; j++) {
if (j == 0) {
System.out.print("\n\tStudent Code : ");
} else if (j == 1) {
System.out.print("\n\tName : ");
} else if (j == 2) {
System.out.print("\n\tMaths Grade : ");
} else if (j == 3) {
System.out.print("\n\tFrench Grade : ");
} else {
System.out.print("\n\tNonexistent field!\n");
}
students[i][j] = input.nextLine();
}
}
you can say
System.out.print("\n\nStudent 001" + "\n\n");
for (int j = 0; j < students[0].length; j++) {
if (j == 0) {
System.out.print("\n\tStudent Code : ");
} else if (j == 1) {
System.out.print("\n\tName : ");
} else if (j == 2) {
System.out.print("\n\tMaths Grade : ");
} else if (j == 3) {
System.out.print("\n\tFrench Grade : ");
} else {
System.out.print("\n\tNonexistent field!\n");
}
students[0][j] = input.nextLine();
}

java - Exercise, do not work properly on the subject arrays

Check if the matrix is folded from.
The test should make:
image
The code always returns false and it is unclear to me why. What's wrong with the code?
the code:
public class test1 {
public static void main(String[] args) {
int[][] mat = { { 1, 7, 9 }, { 2, 9, 7 }, { 9, 2, 1 } };
boolean flag = true;
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat.length; j++) {
System.out.print("[" + mat[i][j] + "]");
}
System.out.println();
}
for (int i = mat.length - 1; i > -1; i--) {
for (int j = mat.length - 1; j > -1; j--) {
if (i == j) {
j--;
}
if (mat[i][j] != mat[j][i]) {
flag = false;
System.out.println("mat[i][j]" + mat[i][j] + " " + i + " "
+ j);
j = -1;
i = -1;
}
}
}
if (flag == false) {
System.out.println("Not first folded matrix");
} else {
System.out.println("First folded matrix");
}
}
}
thank you
You can use this function:
public static boolean isFolded(int[][] mat){
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat.length; j++) {
if (i == j) {
continue;
}
if (mat[i][j] != mat[mat.length - 1 - j][mat.length - 1 - i]) {
System.out.println("mat[i][j] " + mat[i][j] + " i:" + i + " j:"
+ j);
return false;
}
}
}
return true;
}
Call it in you main like:
flag = isFolded(mat);

Exception in thread "main" java.util.NoSuchElementException on ideone

This is my first code with java .when I tested it on Ideone. It showed:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Hw2_p4.main(Main.java:22)
I searched for answers but I didn't get the problem cause or how to fix it.
the code runs on eclipse normally
Here is the code
import java.util.Scanner;
class Hw2_p4 {
static void swap(String[] A, int a, int b) {
String temp = A[a];
A[a] = A[b];
A[b] = temp;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
int i, j, k, l, counter = 0;
String[] name = new String[16];
String[][] notalong = new String[120][2];
String[] temp = new String[120];
boolean[][] A = new boolean[120][2];
for (i = 0; i < n; i++) {
name[i] = sc.next();
}
for (i = 0; i < m; i++) {
for (j = 0; j < 2; j++) {
notalong[i][j] = sc.next();
}
}
int flag = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < 2; j++) {
flag = 0;
for (k = i + 1; k < m; k++) {
for (l = 0; l < 2; l++) {
if (notalong[i][j].compareToIgnoreCase(notalong[k][l]) == 0 && A[i][j] == false && A[k][l] == false) {
A[k][l] = true;
flag = 1;
}
}
}
if (flag == 1) {
A[i][j] = true;
counter++;
} else if (flag == 0 && A[i][0] == false && A[i][1] == false) {
A[i][j] = true;
counter++;
}
}
}
System.out.println(n - counter);
int x = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < 2; j++) {
if (A[i][j] == false) {
temp[x++] = notalong[i][j];
A[i][j] = true;
for (k = i + 1; k < m; k++) {
for (l = 0; l < 2; l++) {
if (notalong[i][j].compareToIgnoreCase(notalong[k][l]) == 0 && A[k][l] == false) {
A[k][l] = true;
}
}
}
}
}
}
//compare not along with names
int found = 1;
for (i = 0; i < n; i++) {
found = 0;
for (j = 0; j < m; j++) {
for (k = 0; k < 2; k++) {
if (name[i].compareToIgnoreCase(notalong[j][k]) == 0) {
found = 1;
}
}
}
if (found == 0) {
temp[x++] = name[i];
}
}
//sorting lexicographically
boolean swapp = true;
for (i = 0; i < x && swapp; i++) {
swapp = false;
for (j = 0; j < x - i - 1; j++) {
if (temp[j].compareToIgnoreCase(temp[j + 1]) > 0) {
swap(temp, j, j + 1);
swapp = true;
}
}
}
for (i = 0; i < x; i++) {
System.out.println(temp[i]);
}
}
}
Ideone is not interactive. You have to click on Specify input and enter all of your input in there before you run the application. What you are seeing is an exception because System.in has an "end of file" status.
You ought to protect sc.next() with cs.hasNext():
while (!sc.hasNext()) {
Thread.sleep(100);
}
int n = sc.nextInt();
However since you read the input in many location throughout your code try using Scanner.nextLine() preferably printing to the console, what is the input you are expecting next. nextLine() will read all the input until the user presses ENTER, which is a nicer 'user journey' than simply constantly reading keyboard and passing it to System.in.
System.out.print("Please enter your favourite number : "); //NB print and space is last
String input = sc.nextLine();
int favNumber = Double.parseDouble(input);
the way that Ideone gives input to your code is different from the way your IDE(or you)gives input.
so you should probably figure out how does Ideone gives input and then fix your code to that way of inputs.

Categories

Resources