Home > Blockchain >  making a 1d array into a 2d array
making a 1d array into a 2d array

Time:03-29

my homework is to make a 1d array into a 2d array but i just cant seem to make it work.

=- these are the error im getting

Car.java:7: error: not a statement
    String[] [] carList = new String[8][2];  {"Honda","Ford","Lamborghini","Subaru","Saturn","Nissan","BMW","Porsche", "Chevy", "VW", "Kia", "Audi", "Ram"}; 
                                              ^

Car.java:7: error: ';' expected
    String[] [] carList = new String[8][2];  {"Honda","Ford","Lamborghini","Subaru","Saturn","Nissan","BMW","Porsche", "Chevy", "VW", "Kia", "Audi", "Ram"}; 
                                                     ^
2 errors

and this is my code

//Car.java

import java.util.Scanner;

public class Car {
    public static void main(String[] args) {
    String[] [] carList = new String[8][2];  {"Honda","Ford","Lamborghini","Subaru","Saturn","Nissan","BMW","Porsche", "Chevy", "VW", "Kia", "Audi", "Ram"}; 
 
// Declaring array literal
       
        Scanner scanner = new Scanner(System.in);
        String carMake = "";
        

        for (int i = 0; i < 3; i  ) {

            System.out.print("What car do you want to buy?: ");

            carMake = scanner.nextLine();
            
            while (carMake.isEmpty()){

                System.out.print("Input should not be empty, try again: ");

                carMake = scanner.nextLine();
                // starting loopdeloop 
                
            }
             for (int j = 0; j <= carList.length - 1; j  ){
            
            if (carMake.equalsIgnoreCase(carList[j])) {
            System.out.println(j   " "   carMake);
            
           
  // code block to be executed..... for his war crimes
            
            } else {
            System.out.println("404 Car make not found..... look behind you");}
            
          
            
        }
        

            }
            for (int l = 0; l <= carList.length - 1; l  ) {
            System.out.print(carList[l]   " ");}
                   
    }
}

CodePudding user response:

I think you need to first study how to initialize a 2d array and then how to traverse through a 2d array.

you are not initializing properly here fix this

String[] [] carList = new String[8][2];  {"Honda","Ford","Lamborghini","Subaru","Saturn","Nissan","BMW","Porsche", "Chevy", "VW", "Kia", "Audi", "Ram"};
  1. you are traversing a 2d array using a single loop this is wrong

             for (int j = 0; j <= carList.length - 1; j  ){            
                if (carMake.equalsIgnoreCase(carList[j])) {
                 System.out.println(j   " "   carMake);
    }}
    

here are a few examples if its a 1d array then its a linear list so it will be something like

String fruits[] = {"apple","banana","orange"}

but when its a 2d array you have to thing it like a table ex:-

String[][] cars = { {"lamborghini","price 1 million"}, {"skoda","50k"}};

and for iterating if its a 1d array then for loop is enough if its a 2d array then you have to use 2 variables ex:

    for(int i=0;i<n;i  ){
        for(int j=0;j<m;j  ){
         print(cars[i][j])
    }
}


     

CodePudding user response:

To show your 2d array you have to set it like this:

String[] [] carList = new String[8][2]{{"Honda","Ford","Lamborghini","Subaru","Saturn","Nissan","BMW"},{"Porsche", "Chevy", "VW", "Kia", "Audi", "Ram"}}; 

and to loop over this 2d array you have to use two for loops:

for (int i = 0 ; i < 8 ; i  ){
    for (int j = 0 ; j < 2 ; j  ){
     print(carsList[i][j])
    }
}

Or dont use a 2d array if you dont need one (which seems like you don't here) :

String[] carList = new String[16]{"Honda","Ford","Lamborghini","Subaru","Saturn","Nissan","BMW","Porsche", "Chevy", "VW", "Kia", "Audi", "Ram"};

and because this is a 1d array you can loop over it by using one for loop:

for (int j = 0 ; j < carsList.length /*16*/ ; j  ){
 print(carsList[i])
}

CodePudding user response:

You have an extra semicolon before the array initializer:

    String[] [] carList = new String[8][2];  {"Honda","Ford","Lamborghini","Subaru","Saturn","Nissan","BMW","Porsche", "Chevy", "VW", "Kia", "Audi", "Ram"}; 
                                          ^ here

Remove it.

  • Related