-2

I like to center the data of this array using Joptionpane i look at string.format and other but nothing helps i want something like all the information in the matrix its alight whit each other and a "|" to separete the information of each column

the information in data doesnt alignt

score [0][0]="Empresa        "
score [0][1]="Guanacaste";
score [0][2]="Alajuela";
score [0][3]="Heredia";
score [0][4]="San Jose";
score [0][5]="Cartago";
score [0][6]="Limon";
score [0][7]="Puntarenas";
score [0][8]="Total";
score [0][9]="Porcentaje";
score [1][0]="SOIN"+"                ";
score [2][0]="AVANTICA"+"      ";
score [3][0]="INNOVASOFT ";
score [4][0]="CRUX"+"        "+"       ";
score [5][0]="NCQ"+"        "+"         ";

this is the array : String [][] score= new String[6][10];

public static void Output() {
  int l = 0;
  String data = "";
  if (score[0][0] != (null)) {
    while (l < score.length) {
      for (int i = 0; i < 10; i++) {
        if (score[l][i] == null) {
          score[l][i] = "0";
        } 
        data += "  | " + score[l][i];

        if (i == 9) {
          data += " |";
        }

      } 
      l++;
      data += "\n";
    }
    JOptionPane.showMessageDialog(null, data);
  } else {
    JOptionPane.showMessageDialog(null, "No se han ingresado datos", "Datos ingresados", JOptionPane.INFORMATION_MESSAGE);
  }
}
Flown
  • 11,480
  • 3
  • 45
  • 62
  • Possible duplicate of [Aligning a String in JOptionPane](https://stackoverflow.com/questions/14797845/aligning-a-string-in-joptionpane) – Naman Sep 06 '17 at 05:18
  • [link](https://stackoverflow.com/questions/14797845/aligning-a-string-in-joptionpane) doesnt work whit my problem – Juanjose Martinez Sep 06 '17 at 05:35
  • What do you mean by "does not work"? – Usagi Miyamoto Sep 06 '17 at 06:01
  • And if you open the [link](https://stackoverflow.com/questions/12702689/joptionpane-displaying-html-problems-in-java?answertab=oldest#tab-top) suggested in that answer – AxelH Sep 06 '17 at 06:03

2 Answers2

0

Try something like this:

public static void output()
{
  final StringBuilder data = new StringBuilder(1024).append("<html><table>");
  final Formatter fmt = new Formatter(data);
  if (score[0][0] != null )
  {
    for (int l = 0; l < score.length; ++l)
    {
      data.append("<tr>");
      for (int i = 0; i < 10; ++i)
      {
        if (score[l][i] == null)
        {
          score[l][i] = "0";
        } 
        fmt.format("<td>%s</td>", score[l][i]);
      } 
      data.append("</tr>";
    }
    JOptionPane.showMessageDialog(null, data.toString());
  }
  else
  {
    JOptionPane.showMessageDialog(null, "No se han ingresado datos", "Datos ingresados",
                                  JOptionPane.INFORMATION_MESSAGE);
  }
}
Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33
  • Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source) at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source) at java.util.Formatter$FormatSpecifier.print(Unknown Source) at java.util.Formatter.format(Unknown Source) at java.util.Formatter.format(Unknown Source) – Juanjose Martinez Sep 06 '17 at 14:38
0

so i find this solution

int l=0;
String [][] matriz= new String [6][10];

StringBuilder result=new StringBuilder();// this will save and output the matirz

if(score[0][0]!=null){

    for(int i =0;i<6;i++){

        for(int j=0;j<10;j++){

            matriz[i][j]=score[i][j];

        }//for columnas

    }//for lineas

}//if encargado de crear matriz copia
if(score[0][0]!=(null)){
    result.append("<html><font face='Arial'>");
    result.append("<table>");
    result.append("<tr>");
    while(l<score.length ){ 

        for(int i=0;i<10;i++){


            if(score[l][i]==null){

                score[l][i]="0";
            }//if revisa que los datos en la matriz no seal null


            //data+="| "+matriz[l][i]+"   ";
            result.append("|");
            result.append("<td align='center'>").append(score[l][i]).append("</td>");// inserting the matrix data into result
                if(i==9){
                    result.append(" |");
                }

            }//for
        l++;
        result.append("</tr>");
        result.append("<tr>");
        //result.append("\n");


            }//while
            JOptionPane.showMessageDialog(null,result,"Datos Ingresados",JOptionPane.INFORMATION_MESSAGE);
            }//if
else{JOptionPane.showMessageDialog(null, "No se han ingresado datos","Datos Ingresados",JOptionPane.INFORMATION_MESSAGE);}