2

How do i print out multiple arrays, with my code below i only get the double array printed out?

import javax.swing.JOptionPane;

public class Matriseoppgave 
{
  public static void main(String[] args)
  {
    JOptionPane.showMessageDialog(null, "Du skal nå skriv inn 3 navn");

    String getnavn1 = JOptionPane.showInputDialog("Skriv inn første navn ");
    String getnavn2 = JOptionPane.showInputDialog("Skriv inn andre navn ");
    String getnavn3 = JOptionPane.showInputDialog("Skriv inn tredje navn ");

    String[] Arraynavn;
    Arraynavn = new String[3];
    Arraynavn[0] = getnavn1;
    Arraynavn[1] = getnavn2;
    Arraynavn[2] = getnavn3;

    JOptionPane.showMessageDialog(null, "Du skal nå skriv inn 3 tlf nummer");

    String gettlf1 = JOptionPane.showInputDialog("Skriv inn første tlf nummer");
    String gettlf2 = JOptionPane.showInputDialog("Skriv inn andre tlf nummer");
    String gettlf3 = JOptionPane.showInputDialog("Skriv inn tredje tlf nummer");

    int[] Arraytlf;
    Arraytlf = new int[3];
    Arraynavn[0] = gettlf1;
    Arraynavn[1] = gettlf2;
    Arraynavn[2] = gettlf3;

    JOptionPane.showMessageDialog(null, "Du skal nå skriv inn 3 desimaltall");

    String getdes1 = JOptionPane.showInputDialog("Skriv inn første desimaltall");
    String getdes2 = JOptionPane.showInputDialog("Skriv inn andre desimaltall");
    String getdes3 = JOptionPane.showInputDialog("Skriv inn tredje desimaltall");

    double[] Arraydes;
    Arraydes = new double[3];
    Arraynavn[0] = getdes1;
    Arraynavn[1] = getdes2;
    Arraynavn[2] = getdes3;

    Matriseoppgave mat = new Matriseoppgave();
    mat.skrivutklasse(Arraynavn,Arraytlf,Arraydes);
  }  

  public void skrivutklasse(String[] Arraynavn, int[] Arraytlf, double[] Arraydes)
  {
    StringBuilder print1 = new StringBuilder (Arraynavn.length);
    StringBuilder print2 = new StringBuilder (Arraytlf.length);
    StringBuilder print3 = new StringBuilder (Arraydes.length);

    for(int i = 0; i < Arraynavn.length; print1.append (Arraynavn[i++] )) print1.append("\n");
    for(int i = 0; i < Arraytlf.length; print2.append (Arraytlf[i++] )) print2.append("\n");
    for(int i = 0; i < Arraydes.length; print3.append (Arraydes[i++] )) print3.append("\n");
    {
      JOptionPane.showMessageDialog(null, "Array: \n" + print1 + print2 + print3);
    }
  }
}
2
  • hvis du skriver paa dansk, der er mange folke at kan ikke forstaar hvad du skriver! It is good practice to do all your coding in English, especially variable names - then you will be in the habit for when you are collaborating internationally. Commented Sep 10, 2009 at 16:36
  • Heheh.. apparently I can read danish. Commented Sep 15, 2009 at 8:19

2 Answers 2

3

you are assigning to Arraynavn where you are intending Arraytlf and Arraydes.

Obviously this replaces anything you already put in that array. Resulting in only the "double array" to be printed (which is in fact your string array).

When you correct the code you'll encounter some casting errors! Look at the Integer, Double and String classes how to resolve those.

Sign up to request clarification or add additional context in comments.

Comments

2

By the way, there's an easier way to print an array in Java:

double[] array = {0.0, 1.0, 2.0}; System.out.println(Arrays.toString(array));

If you have a multidimensional array, e.g.

int[][] array = { {5, 2}, {6, 3} };

you need to use 'deepToString' instead

System.out.println(Arrays.deepToString(array));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.