1

When I attempt to put a string into the console, my loop won't add my string into my array but instead goes straight to the error. What am I missing here?

public static void main(String[]args)
  {
      System.out.println("Trucks running today (min 2):  ");
      Scanner trucks = new Scanner(System.in);

      int userTrucks = trucks.nextInt();
      int [] x = truckNumberArray(userTrucks);
        if(x == null) 
            {
                System.out.print("Error: must be at least two trucks.");
                System.exit(0);
            }
    Scanner size = new Scanner(System.in);
    String [] largeSmall = new String[x.length];
    for(int t = 1; t<x.length;t++)
         {

            System.out.println("Truck is large or small (large max = 100, small max = 10)?  ");
            largeSmall[t] = size.nextLine();
            if (largeSmall.equals("small")) 
                {  
                    System.out.print(" "); 
                }            
            else if (largeSmall.equals("large")) 
                {
                    System.out.print(" ");
                } 
            else 
                {
                        System.out.println("error");
                        System.exit(0);
                }

         }              
   }
}

2 Answers 2

1

Notice this: if (largeSmall.equals(...

Modify: if (largeSmall[t].equals(...

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

1 Comment

It is my pleasure
1

You can't access any array element directly by using the name of that array, the position is also to be specified. Like String [] largeSmall = new String[x.length]; This is an String array and has to be accessed as largeSmall[t].equals("small").

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.