Java - Put else statement in looping for - java

I want to make the handling of which if the data I input is not found. However, when the data is not found, the program will print a message else as much repetition. Where should I put the else statement correctly?
public void Data(String x){
for (int i = 1; i < 6; i++) {
if (x.equalsIgnoreCase(table1[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table1[i][j]);
}
}
else if (x.equalsIgnoreCase(table2[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table2[i][j]);
}
}
else if (x.equalsIgnoreCase(table3[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table3[i][j]);
}
}
else if (x.equalsIgnoreCase(table4[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table4[i][j]);
}
}
else if (x.equalsIgnoreCase(table5[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table5[i][j]);
}
}
else if (x.equalsIgnoreCase(table6[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table6[i][j]);
}
}
else if (x.equalsIgnoreCase(table7[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table7[i][j]);
}
}
else System.out.println("Data not found!");
}
}

Try this:
public void Data(String x){
boolean b = false;
for (int i = 1; i < 6; i++) {
if (x.equalsIgnoreCase(table1[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table1[i][j]);
}
}
else if (x.equalsIgnoreCase(table2[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table2[i][j]);
}
}
else if (x.equalsIgnoreCase(table3[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table3[i][j]);
}
}
else if (x.equalsIgnoreCase(table4[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table4[i][j]);
}
}
else if (x.equalsIgnoreCase(table5[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table5[i][j]);
}
}
else if (x.equalsIgnoreCase(table6[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table6[i][j]);
}
}
else if (x.equalsIgnoreCase(table7[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table7[i][j]);
}
}
else b = true;
}
if (b) System.out.println("Data not found!");
}

If you want to terminate as soon as data is not found, you can change your else statement to this:
else {
System.out.println("Data not found!");
return;
}

Related

Converting Floyd Warshall algorithm pseudocode to java

I'm currently working on implementing the Floyd Warshall algorithm from pseudocode to java. I felt like I had it correct, but anytime I ran it on a graph, I was getting an output of large negative number and I feel that it was probably the way I implemented my algorithm. After looking I realized I may have missed something. I haven't implemented the k-1 part of the pseudocode and I'm not sure how to implement this. Im hoping this will correct the issue.
Here it the code i have written.
public static int[][] floyd(int n, int[][] W, int[][] P, int[][] D) {
D = W;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
P[i][j] = 0;
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if ((D[i][j] == Integer.MAX_VALUE) && (D[i][k] + D[k][j] == Integer.MAX_VALUE)) { // add
continue;
} else if (D[i][j] <= D[i][k] + D[k][j]) {
D[i][j] = D[i][j];
} else {
D[i][j] = D[i][k] + D[k][j];
}
}
return D;
}
And here is the pseudocode im writing based off of.
Floyd-Warshall(W)
n = W.rows
D(0) = W
for (k = 1 to n)
for ( i = 1 to n )
for ( j = 1 to n )
if (dij(k-1) == INF) && (dik(k-1) + dkj(k-1)) == INF)
continue
else if (dij(k-1) ≤ dik(k-1) + dkj(k-1))
dij(k) = dij(k-1) // add
else
dij(k) = dik(k-1) + dkj(k-1) // add
return (D(n))
EDIT
Heres all of my code thats relevant with some corrections
int V = g.nodeList.size();
int W[][] = new int[V][V];
int P[][] = new int[V][V];
// Adding wights to graph
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
W[j][i] = Integer.parseInt(g.edgeList.get((j * V) + i).label);
if (W[j][i] == 0) {
W[i][j] = Integer.MAX_VALUE;
}
}
}
System.out.println("Matrix to find the shortest path of.");
printMatrix(W, V, g);
System.out.println("Shortest Path Matrix.");
printMatrix(floyd(V, W, P), V, g);
System.out.println("Path Matrix");
printPredMatrix(P, V, g);
}
public static int[][] floyd(int n, int[][] W, int[][] P) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
P[i][j] = 0;
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if ((W[i][j] == Integer.MAX_VALUE) && (W[i][k] + W[k][j] == Integer.MAX_VALUE)) { // add
continue;
} else if (W[i][j] <= W[i][k] + W[k][j]) {
W[i][j] = W[i][j];
} else {
W[i][j] = W[i][k] + W[k][j];
}
}
return W;
}
public static int min(int i, int j) {
if (i > j) {
return j;
}
return i;
}
public static void printMatrix(int[][] Matrix, int V, Graph g) {
System.out.print("\n\t");
for (int j = 0; j < V; j++) {
System.out.print(g.nodeList.get(j).name + "\t");
}
System.out.println();
for (int j = 0; j < 35; j++) {
System.out.print("-");
}
System.out.println();
for (int i = 0; i < V; i++) {
System.out.print(g.nodeList.get(i).name + " |\t");
for (int j = 0; j < V; j++) {
if ((Matrix[i][j] == Integer.MAX_VALUE)||(Matrix[i][j] == Integer.MIN_VALUE)) {
System.out.print("~");
} else {
System.out.print(Matrix[i][j]);
}
System.out.print("\t");
}
System.out.println("\n");
}
System.out.println("\n");
}
public static void printPredMatrix(int[][] Matrix, int V, Graph g) {
System.out.print("\n\t");
for (int j = 0; j < V; j++) {
System.out.print(g.nodeList.get(j).name + "\t");
}
System.out.println();
for (int j = 0; j < V * 3; j++) {
System.out.print("-");
}
System.out.println();
for (int i = 0; i < V; i++) {
System.out.print(g.nodeList.get(i).name + " |\t");
for (int j = 0; j < V; j++) {
System.out.print(Matrix[i][j]);
System.out.print("\t");
}
System.out.println("\n");
}
System.out.println("\n");
}
}```

Pyramid of numbers using (for) Java

a pyramid of numbers thats core is increasing by one which would be seen horizontally as 12345.
I figured out this code
but its not as the img I shown up there ^
the code I wrote is:
int rowCount = 1;
for (int i = 6; i > 0; i--)
{
for (int j = 1; j <= i*2; j++)
{
System.out.print(" ");
}
for (int j = 1; j <= rowCount; j++)
{
System.out.print(j+" ");
}
for (int j = rowCount-1; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
rowCount++;
}
}
`

Summing cells from table

I seem to not be able to solve this java.lang.ArrayIndexOutOfBoundsException: 5
I understand the error, but the table is 5x5 and I think I have everything right for printing it.
public static int tulosta_matriisi(int[][] matriisi) {
int i=0, j=0;
for(i = 0; i <= 4; i++) {
for(j = 0; j <= 4; j++) {
if(j == 4 && i <= 4)
System.out.println(matriisi[i][j]);
else if(i <= 4 && j <= 4)
System.out.print(matriisi[i][j] +"\t");
}
}
return matriisi[i][j];
}
To avoid all this problem you have to use :
for(i = 0; i < matriisi.length; i++) {
for(j = 0; j < matriisi[i].length; j++) {
...
When you get out your loop, the i and j will be incremented so you should not return matriisi[i][j] this make this error java.lang.ArrayIndexOutOfBoundsException: 5, so instead you should to return matriisi[i-1][j-1] so in the end your program should look like this :
public static int tulosta_matriisi(int[][] matriisi) {
int i = 0, j = 0;
for (i = 0; i < matriisi.length; i++) {
for (j = 0; j < matriisi[i].length; j++) {
if (j == matriisi[i].length - 1 && i <= matriisi.length) {
System.out.println(matriisi[i][j]);
} else if (i <= matriisi.length && j <= matriisi[i].length) {
System.out.print(matriisi[i][j] + "\t");
}
}
}
return matriisi[i - 1][j - 1];
}
Good luck

How to execute a print x amount of times?

What I have:
public static void nestedForLoops() {
int k = 5;
for (int i = 1; i <= 5; i++) {
for (int j = 5; j > i; j--) {
System.out.print("*");
}
System.out.println(i);
}
}
Output:
****1
***2
**3
*4
5
Trying to achieve:
****1
***22
**333
*4444
55555
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
if (5 - j >= i) {
System.out.print("*");
} else {
System.out.print(i);
}
}
System.out.println();
}
Here you are:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
if (i <= 5 - j) {
System.out.print("*");
} else {
System.out.print(i);
}
}
System.out.println();
}
for i in range(1,6,1):
print "*" *(5-i),str(i) *i

Array Index out of Bound Exception while running the program to find largest of an array

Here is the code:Getting Array index out of Bound exception
class Max {
public static void main(String args[]) {
int a[][];
Scanner src = new Scanner(System. in );
System.out.println("Enter the no of rows");
int rows = src.nextInt();
a = new int[rows][5];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 5; j++) {
a[i][j] = src.nextInt();
}
}
System.out.println("Array is");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(" " + a[i][j]);
}
System.out.println();
}
int l = a[0][0];
int i;
for (i = 0; i < rows; i++)
l = a[i][0];
for (int j = 0; j < 5; j++)
if (l < a[i][j])
l = a[i][j];
System.out.println("Max" + l);
}
}
on run time it gives the following:
Exception in thread "main" java.lang.Array index out of Bound Exception:3
at Max.main Max.java:33
Can Anyone suggest what is wrong in the code????
You miss a { after the for (i = 0; i < rows; i++) so that the for (int j = 0; j < 5; j++) loop is inside the first one.
Without that, this gives:
for (i = 0; i < rows; i++)
l = a[i][0];
// End of the for i loop, now i = rows.
for (int j = 0; j < 5; j++)
if (l < a[i][j]) // i = rows: bang.
l = a[i][j];
Try this:
public static void main(String args[]) {
int a[][];
Scanner src = new Scanner(System. in );
System.out.println("Enter the no of rows");
int rows = src.nextInt();
a = new int[rows][5];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 5; j++) {
a[i][j] = src.nextInt();
}
}
System.out.println("Array is");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(" " + a[i][j]);
}
System.out.println();
}
int l = a[0][0];
int i;
for (i = 0; i < rows; i++) {
l = a[i][0];
for (int j = 0; j < 5; j++)
if (l < a[i][j])
l = a[i][j];
}
System.out.println("Max" + l);
}

Categories

Resources