0

I want to build a copy of code that involves method and array. My intention is to display a date that will be entered by the user.

But I get an error:

array required, but string found

This is part of my code:

public class Reservation{
    public static Scanner input=new Scanner(System.in);
    public static String[] date=new String[5];
    public static int arraylength=5;
    public static int index;
    
    public static void main (String[] args){
      start();
    }

    //this is the method where the date will be entered
    public static void register(){
        for(index=0;index<date.length;index++){
            System.out.print("Enter the date of reservation(DD/MM/YY): ");
            date[index]=input.nextLine();
        }
    }

    // this is the method where date will be display
    public static void display(String date, int index,int arraylength){
        for(index=0;index<arraylength;index++){
            System.out.println("The date ["+index+"]: "+date[index]);
        }
    }
}
1
  • 1
    In your display method, you provide String object and try iterating through it as it was an array? Commented Jan 1, 2016 at 13:52

3 Answers 3

3

You are using date[index] but variable date is defined as String date in the method parameter. It is not an array, to define array type you should use String[] date.

// this is the method where date will be display
public static void display(String[] date, int index)
{
   System.out.println("The date ["+index+"]: "+date[index]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

i see...so what should i do?sorry im completely new in java
if you pass an array as parameter, then change the parameter type, otherwise change the method code.
3

Error is here

 public static void display(String date, int index,int arraylength)
                            ^^^^^^^^^^^

As you have passed an simple string in the function and you are trying to access as date[index]

Also, your local and global variable have same name, hence Local Variable get priority. So it is considering date as simple string variable.

Comments

0

You need to use static field that you defined as method argument which is in conflict, so change it as below:

System.out.println("The date ["+index+"]: "+Reservation.date[index]);
                                            ^^^^^^^^^^^

or change the method definition to something like:

public static void display(String aDate, int index,int arraylength)

1 Comment

i try to put Reservation.date[index]....all clear but i receives a new problem...the code is looping the date question...before this it will ask the next question...e.g How many person etc...this is just part of my code

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.