skip to Main Content

I am currently working on a Java project using Visual Studio as my development environment. However, I have encountered an error in my code that I am struggling to resolve on my own. Despite my efforts to debug and analyze the issue, I haven’t been able to pinpoint the exact cause of the problem.

To provide better context, I will share the specific piece of code and the error message I received below. I believe that with some assistance, I will be able to understand what went wrong and learn how to fix it. Your help would mean a lot to me, and I would greatly appreciate any guidance or advice you can offer regarding this issue.

public class Arrays{
    public static void main (String[] args){
        String[] alfabe = new String[29];
        alfabe [0] ="z";
        alfabe [1] ="a";
        alfabe [2] ="b";
        alfabe [3] ="c";
        alfabe [4] ="ç";
        alfabe [5] ="d";
        alfabe [6] ="e";
        alfabe [7] ="f";
        alfabe [8] ="g";
        alfabe [9] ="ğ";
        alfabe [10] ="h";
        alfabe [11] ="ı";
        alfabe [12] ="i";
        alfabe [13] ="j";
        alfabe [14] ="k";
        alfabe [15] ="l";
        alfabe [16] ="m";
        alfabe [17] ="n";
        alfabe [18] ="o";
        alfabe [19] ="ö";
        alfabe [20] ="p";
        alfabe [21] ="r";
        alfabe [22] ="s"; 
        alfabe [23] ="ş";
        alfabe [24] ="t";
        alfabe [25] ="u";
        alfabe [26] ="ü";
        alfabe [27] ="v";
        alfabe [28] ="y";
    }
}
    "resource": "/c:/Users/doruk/OneDrive/Masaüstü/Java Learning 1/src/Arrays.java",
    "owner": "_generated_diagnostic_collection_name_#3",
    "code": "16777233",
    "severity": 8,
    "message": "Type mismatch: cannot convert from java.lang.String to String",
    "source": "Java",
    "startLineNumber": 10,
    "startColumn": 21,
    "endLineNumber": 10,
    "endColumn": 24

2

Answers


  1. Avoid naming Class like String , use something like MyString.

    Explicitly specify the java.lang.String :

    public class Arrays {
        
            public static void main ( java.lang.String[] args){
                java.lang.String[] alfabe = new java.lang.String[29];
                alfabe [0] ="z";
                alfabe [1] ="a";
                alfabe [2] ="b";
                alfabe [3] ="c";
                alfabe [4] ="ç";
                alfabe [5] ="d";
                alfabe [6] ="e";
                alfabe [7] ="f";
                alfabe [8] ="g";
                alfabe [9] ="ğ";
                alfabe [10] ="h";
                alfabe [11] ="ı";
                alfabe [12] ="i";
                alfabe [13] ="j";
                alfabe [14] ="k";
                alfabe [15] ="l";
                alfabe [16] ="m";
                alfabe [17] ="n";
                alfabe [18] ="o";
                alfabe [19] ="ö";
                alfabe [20] ="p";
                alfabe [21] ="r";
                alfabe [22] ="s"; 
                alfabe [23] ="ş";
                alfabe [24] ="t";
                alfabe [25] ="u";
                alfabe [26] ="ü";
                alfabe [27] ="v";
                alfabe [28] ="y";
            }
        
    }
    
    Login or Signup to reply.
  2. The way you instantiate the array items, requires a lot of work on your part, you can simplify your life by using:

    public class Arrays{
        public static void main (String[] args){
            String[] alfabe = new String[29];
            alfabe [0] ="z";     
             // "97" is a numeric value of "a"...
            for( int i = 1, k = 97; k < 125; i++, k++) {
                  // convert the char to String
                alfabe [i] = Character.toString( k );
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search