hey guys, so i just started learning java and had some error in my first array code. Error: Could not find or load main class Being. I use eclipse if that helps, because i read that these problems might be that ecplise cannot find the class, so just making sure it's not from the code.

Any criticism on my code is highly accepted as i want to start Java with a good basic knowledge :)

Thanks in advance.

public class Being 
{
    public static void main(String[] args)
    {
        int rowNum=12;
        int colNum=24;
        String g;
        String [][] map = new int[rowNum][colNum];
        for (int i=0;map.length; i++)
            map[i][map[i].length] = g;
        for (int rowNum=0; rowNum < map.length; rowNum++)
        {
            for (int colNum=0; colNum < map[rowNum].length; colNum++)
                System.out.printf("%d ", map[rowNum][colNum]);
        }
    }
}

Edited 2 Years Ago by DkgMarine

That code contains fatal errors that prevent it from compiling, so there's no possibility of running it.
Eclipse will have flagged the errors, so fix those first.

The only error i get is String cannot be resolved to a type, and i asked many people and they said there is something needed to be done regarding the Java library, could anyone help me out?

DkgMarine: that makes no sense.
first of all, String is a part of basic Java. if that can't be found, your JDK wasn't installed decently, or you didn't set the path of your IDE correctly.

next to that, as James pointed out: there are some serious violations against Java's rules:

String [][] map = new int[rowNum][colNum];

a String and an int are not (even close) the same, meaning that this line will not compile.

either you make it:

String [][] map = new String[rowNum][colNum];
OR
int [][] map = new int[rowNum][colNum];

next:

for (int i=0;map.length; i++)

map.length return an int, with the length of the array, but the second element of a classic for-statement, should lead to a boolean. for instance:

for (int i=0; i < map.length; i++)

following mistake:

for (int rowNum=0; rowNum < map.length; rowNum++)
well, this seems right, but as usual: looks are deceiving.

you already declared rowNum earlier in your code:
int rowNum=12;
which renders the above statement uncompilable.

the same goes for your inner for loop and the colNum declaration.

Yeah,JDK was not installed properly. It didn't allow me to separate right from wrong while learning.

Thanks for your help guys :)

This is the update code for future Java learners...

import java.util.Scanner;
public class Studyone {


    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        int row = 5;
        int col = 5;
        String sentence;
        String sentence2;
        String [][] map = new String[row][col];
        for (int i = 0; i < map.length; i++) 
        {
            for (int j = 0;j < map[i].length; j++) 
            {
                System.out.println("Enter the element");
                sentence2 = input.nextLine();
                map[i][j]=sentence2;
        }
        System.out.println(map[1][3]);
    }

}

No, i was trying just to print for the sake of learning, but now i cannot seem to print it all and when i enter the elements, it does not ask for the first element, it somehow automatically just entered null in the first map[0][0]
any advise?

well, you are always printing the same element, over and over, whether it is filled or not.

move the print statement in your inner for loop, and do it like this:

System.out.println(map[i][j]);

by using the i and j, which are updated by the loops, you'll always print the last element you just added.
of course: you can always create a new loop for this, but it seems a bit redundant.

This article has been dead for over six months. Start a new discussion instead.