1

I am writing an airline program that will allow the user to input names and meal choice for each seating section economy, business, and first. I am trying to save all the names and meals into an array. but I am getting a syntax error.

I get expected message when I implement my flyer array.

I have looked on stack overflow. From what I can tell it should be ok to initialize my array this way.

Thanks for any help.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Flyers 
{    
    public Flyers()
    {

    }

    public List<String> seat = new ArrayList<>();
    int numberOfFlyers;
    int numberOfMeals;
    String name;
    String meal;    

    String[][] flyer;

    public void addEconomyFlyer()
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number of economy seats sold: ");
        numberOfFlyers = in.nextInt();

        flyer = new [numberOfFlyers][numberOfFlyers];
    }  
}
7
  • 3
    What do you think new [numberOfFlyers][numberOfFlyers] does and why do you think so? Commented Jul 26, 2014 at 6:19
  • Why are you making flyer a two-dimensional array? It doesn't make much sense to me. Commented Jul 26, 2014 at 6:21
  • I have looked on stack overflow. From what I can tell it should be ok to initialize my array this way Please link a question/answer you saw that showed your initialization as ok. Commented Jul 26, 2014 at 6:26
  • 1
    I want each [][] to have the same number of items that is equal to the number of people on the plane. Then I will add a nested for loop that will add a name for each of the flyers, and a meal choice. ultimately i need to be able to print out the array Name and their Meal choice. Commented Jul 26, 2014 at 6:27
  • 1
    OK, I think you've misunderstood what a two-dimensional array does. Suppose there are 300 people on the plane. new String[numberOfFlyers][numberOfFlyers] will create 90000 Strings. Do you think you might be better off with two one-dimensional arrays? One for the names and one for the meal choices? Commented Jul 26, 2014 at 6:32

3 Answers 3

2

I want each [][] to have the same number of items that is equal to the number of people on the plane. Then I will add a nested for loop that will add a name for each of the flyers, and a meal choice. ultimately i need to be able to print out the array Name and their Meal choice.

Almost, the issue here is you haven't specified a type for the array.

flyer = new [numberOfFlyers][numberOfFlyers];

should probably be

flyer = new String[numberOfFlyers][numberOfFlyers];

However that doesn't make a lot sense. One solution is to use,

flyer = new String[numberOfFlyers][2];

where 0 is name and 1 is meal. But, really you should probably have a flyer POJO,

flyer = new Flyer[numberOfFlyers];

Where Flyer might look something like,

class Flyer {
  Flyer(String name, String mealType) {
    this.name = name;
    this.mealType = mealType;
  }
  String name;
  String mealType;
  public String toString() {
    return "Name: " + name + ", Meal: " + mealType;
  }
}

Then you can create new Flyer(s) and call toString() in your loop. You might also choose to add getter and setter functions for name and mealType.

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

Comments

0

change :

flyer = new [numberOfFlyers][numberOfFlyers];

to:

flyer = new String[numberOfFlyers][numberOfFlyers];

1 Comment

When creating any array in Java, you need to provide the data type as well with new operator.
0

Scanner class

 Scan.scanner.scan 

You need scan it!!!!

identifier = here

This trickes computer in think identifier there! YES! WORK!

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.