Print a message only once after iterating 2d array - java

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]);
}
}

Related

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();
}

wanting to cut out hidden println in java

int anzZeilen = 9;
int anzZahlen = 1;
for (int i = 1; i <= 9; i++) {
System.out.print(" ");
for (int j = 1; j < anzZeilen; j++) {
System.out.print(" ");
}
if (anzZahlen % 2 != 0) {
for (int x = 1; x <= anzZahlen; x++) {
System.out.print(" ");
System.out.print(x + " ");
}
}
System.out.println();
anzZahlen++;
anzZeilen--;
}
my problem is that i am creating 9 new println, but i only want to create a new line if the if-statement is true so i dont have that much space inbetween. how do i do that?
my code above
my code with println inside the if block
You could just maintain a single StringBuilder and append to it along the way, printing once only at the end:
int anzZeilen = 9;
int anzZahlen = 1;
StringBuilder msg = new StringBuilder();
for (int i=1; i <= 9; i++) {
msg.append(" ");
for (int j=1; j < anzZeilen; j++) {
msg.append(" ");
}
if (anzZahlen % 2 != 0) {
for (int x = 1; x <= anzZahlen; x++) {
msg.append(" ").append(x).append(" ");
}
}
msg.append("\n");
anzZahlen++;
anzZeilen--;
}
// now print the entire message, once
System.out.println(msg);

Print pyramid of * in Java

I'm wondering if you could help me out. I'm trying to write a nested for loop in java that displays a number pyramid triangle that looks like
___________*#
__________*_*#
_________*___*#
________*_____*#
_______*_______*#
______*_________*#
_____*___________*#
____*_____________*#
___*_______________*#
__*_________________*#
_*___________________*#
***********************#
This is what I have so far:
class Triagle {
public static void printTriagle(int n) {
for (int i = 0; i < n; i++) {
for (int j = n - i; j > 1; j--) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
// printing stars
System.out.print("* ");
}
System.out.println();
}
}
public static void main(String[] args) {
printTriagle(12);//I want to set the triangle to be height of 12
}
}
My result is not equal to the expected output:
___________*#
__________*_*#
_________*_*_*#
________*_*_*_*#
_______*_*_*_*_*#
______*_*_*_*_*_*#
_____*_*_*_*_*_*_*#
____*_*_*_*_*_*_*_*#
___*_*_*_*_*_*_*_*_*#
__*_*_*_*_*_*_*_*_*_*#
_*_*_*_*_*_*_*_*_*_*_*#
*_*_*_*_*_*_*_*_*_*_*_*#
I have updated your code and added comments so that you can understand. Refer to the code below:
public static void printTriagle(int n) {
for (int i = 0; i < n; i++) {
for (int j = n - i; j > 1; j--) {
System.out.print("_");
}
String s = "_";
if (i + 1 >= n) // check if it is the last line
s = "*"; // change to print * instead of _
for (int j = 0; j <= i; j++) {
// printing stars
if (j == i)
System.out.print("*#"); // check if the last print of the line
else if (j == 0)
System.out.print("*" + s); // check if the first print of the line
else
System.out.print(s + s);
}
System.out.println();
}
}
Result:
___________*#
__________*_*#
_________*___*#
________*_____*#
_______*_______*#
______*_________*#
_____*___________*#
____*_____________*#
___*_______________*#
__*_________________*#
_*___________________*#
***********************#
Try this
public static void printTriagle(int n) {
for (int i = 0; i < n; i++) {
for (int j = n - i; j > 1; j--) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
// printing stars
if(i == (n-1)){
System.out.print("**");
}
else{
System.out.print((j == 0 || j == i) ? "* " : " ");
}
}
System.out.println();
}
}
Your issue is here:
for (int j=0; j<=i; j++){
// printing stars
System.out.print("* ");
}
Here, it prints a star for each number between 0 and i, but it only should print a star if it is exactly 0 or i.
Try something like this:
for (int j=0; j<=i; j++){
if ( i == n ) {
System.out.print("* ");
} else {
System.out.print(j == 0 || j == i ? "* " : " ");
}
}
EDIT: You may still have to adapt your code to get the bottom line printed correctly, in case that line has to be all stars
This is what you need to do:
public static void printTriagle(int n) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < 2*n; j++) {
if(i == n-1) {
System.out.print((j != 2*n-1) ? "*" : "#");
}
else {
if(i+j == n-1) {
if(i == 0) {
System.out.print("*#");
break;
}
else {
System.out.print("*");
}
}
else if(j-i == n-1) {
System.out.print("*#"); break;
}
else {
System.out.print("_");
}
}
}
System.out.println();
}

Java Unreachable code

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

I am trying this simple sudoku

I am trying a simple sudoku program. i started by taking the values in a 3D
array and then copied them into a 1D array by using mr.serpardum's method.
i know that there is an error at the point where i am trying to find
duplicate elements,because even if i give same numbers as input the output
says "its a sudoku" but i can't to find it...apparently i can't add any
image coz i dont have enough credits
public class SecondAssignment {
#SuppressWarnings("unused")
public static void main(String[] args) throws IOException {
int i = 0, j = 0, k = 0;
boolean result = false;
int arr1[][];
arr1 = new int[3][3];
int arr2[];
arr2 = new int[9];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the elements in the sudoku block");
//getting elements into array
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
arr1[i][j] = Integer.parseInt(br.readLine());
}
}
//printing it in matrix form
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
System.out.print(arr1[i][j] + "\t");
}
System.out.println(" ");
}
//copying array1 elements into array 2
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
arr2[i * 3 + j] = arr1[i][j];
}
}
//finding duplicate elements
for (i = 0; i < arr2.length; i++) {
for (int m = i + 1; m < arr2.length; m++) {
if (arr2[i] == (arr2[m])) {
System.out.println("Not a sudoku");
//result = true;
} else {
System.out.println("Its a sudoku");
//result = false;
}
}
}
}
}
You can update your code to following
//finding duplicate elements
for( i = 0; i < arr2.length; i++){
for(int m = i+1; m < arr2.length; m++){
if(arr2[i] == (arr2[m])){
result = true;
break;
}
}
}
if(result){
System.out.println("\nNot a sudoku");
}
else{
System.out.println("\nIts a sudoku");
}
You should have used break as soon as the match was found.
This code just checks if duplicate element is present in the array (of size 9) or not.

Categories

Resources