Multiarray inputs Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Multiarray inputs without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am unable to create a loop to add hours to a multi-array. When I use the method Hours there is an error. I suspect my for-loop is not capturing the input.

import javax.swing.JOptionPane;

public class Gain 
{
    // Defining array names.
    String[] name = {"A", “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “L”}; 
    int[][] hours = new int[10][3];

    public final int Hours() 
    {
    boolean canceled = false;
    for (int index = 0; index < name.length; index++) {
        JOptionPane.shoeMessageDialog(“Please enter " + name[index] + “’s hours”);
            for (int x = 0; x <= hours.length; x++)
               for (int y = 0; y <= hours[x].length; y++)
        Integer value = promptForInt(artist[index] + “’s first hour: ”);
        if (value != null) {
          while (value < 0)
        {
            JOptionPane.showMessageDialog(null, "Please positive figures." + "nPlease try again.");
            value = promptForInt(name[index] + “’s first hour: ”);
        }
            pay[x][y] = value;
        } else {
            canceled = true;
            break;
        }
    }

    public static void main(String[] args) // Main program
    {
        for (int x = 0; x <= name.length; x++)
           for (int y = 0; y <= hours.length; y++)
              System.out.println(x, y);
    }

Answer

Try adding in brackets like so:

import javax.swing.JOptionPane;

public class Gain 
{
    // Defining array names.
    String[] name = {"A", “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “L”}; 
    int[][] hours = new int[10][3];

    public final int Hours() 
    {
        boolean canceled = false;
        for (int index = 0; index < name.length; index++)
        {
            JOptionPane.shoeMessageDialog("Please enter " + name[index] + "’s hours");
            for (int x = 0; x <= hours.length; x++)
            {
                for (int y = 0; y <= hours[x].length; y++)
                {
                    Integer value = promptForInt(artist[index] + “’s first hour: ”);
                    if (value != null)
                    {
                        while (value < 0)
                        {
                            JOptionPane.showMessageDialog(null, "Please positive figures." + "nPlease try again.");
                            value = promptForInt(name[index] + “’s first hour: ”);
                        }
                        pay[x][y] = value;
                    } 
                    else
                    {
                        canceled = true;
                        break;
                    }
                }
            }

        }
    }

    public static void main(String[] args) // Main program
    {
        for (int x = 0; x <= name.length; x++)
           for (int y = 0; y <= hours.length; y++)
              System.out.println(x, y);
    }

While it’s fine to avoid brackets in a for loop when you only have one line of code afterwards (like in your main method), you need to use them when you have a whole block, as is the case here in your Hours() method. Personally, I like to use brackets all of the time as it makes the code more readable, but that’s just me. 🙂

We are here to answer your question about Multiarray inputs - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji