0

I need to acess 2D array and write some information there. I want to use for-each loop for this. Here is my code. But when executing it says ;

q13.java:24: error: incompatible types: String[] cannot be converted to int
                                                wi.write(St[a][b] + "\t");
                                                            ^
q13.java:24: error: incompatible types: String[] cannot be converted to int
                                                wi.write(St[a][b] + "\t");

What's wrong in my code?

import java.io.*;
class q13{
    public static void main(String[] args) 
    {
        String St[][]={ {"STUDENT NO", "STII", "SPD", "PS", "DCCNII", "SEI"}, 
                {"DIT/10/C1/0001", "A", "A-", "B+", "A", "A"},
                {"DIT/10/M2/0123", "C-" ,"C" ,"B" ,"B", "B+"},
                {"DIT/10/M1/0054", "D" ,"C-" ,"C" ,"B-", "B"},
                {"DIT/10/M1/0025", "A" ,"A" ,"A-", "A", "A"},
                {"DIT/10/C2/1254", "C" ,"C-" ,"B" ,"B+", "B"}  };

        try{
            BufferedWriter wi = new BufferedWriter(new FileWriter(".\\st_info.txt"));

                for(String[] a:St)
                {
                    for(String[] b:St)
                    {
                        wi.write(St[a][b] + "\t");
                    }

                   wi.write("\r\n");
                   wi.newLine();
                } 

            wi.close();
        }
        catch(IOException ex)
        {
        System.out.print(ex);
        }

    }

}
4
  • 1
    which one is line 24? could you highlight, and possibly add more tags, if applicable (I already added the Java tag; normally, I would have missed your question). Commented Feb 16, 2015 at 15:29
  • Start class names with a capital letter, by Java convention. Commented Feb 16, 2015 at 15:32
  • You are using String as an Integer at St[a][b]. a and b should be integers. Commented Feb 16, 2015 at 15:34
  • @barq and fields (such as St) don't... (to the OP) Its about being able to quickly read and reason about the code. When a person looks at it, if the code follows the conventions, it makes it much easier to read and understand what is going on. Proper capitalization provides us hints about what things are without having to refrence back to definition. Commented Feb 16, 2015 at 15:34

4 Answers 4

4

It doesn't make sense to use a and b as indexes into an array - they are themselves String[] variables - check the declaration.

If you're trying to iterate over every element in the array, I suspect you want:

for(String[] a : St)
{
    for(String b : a)
    {
        wi.write(b + "\t");
    }

   wi.write("\r\n");
   wi.newLine();
} 

(I'd also strongly advise you to follow normal Java naming conversions, and use more meaningful names than "st".)

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

Comments

0

You are accessing each string, when you call for(String[] a:St). This is the reason you are getting the error you have show. A quick method to solve this is to iterate each element manually in the loop and call the loop index like so:

import java.io.*;
class q13{
    public static void main(String[] args) 
    {
        String St[][]={ {"STUDENT NO", "STII", "SPD", "PS", "DCCNII", "SEI"}, 
                {"DIT/10/C1/0001", "A", "A-", "B+", "A", "A"},
                {"DIT/10/M2/0123", "C-" ,"C" ,"B" ,"B", "B+"},
                {"DIT/10/M1/0054", "D" ,"C-" ,"C" ,"B-", "B"},
                {"DIT/10/M1/0025", "A" ,"A" ,"A-", "A", "A"},
                {"DIT/10/C2/1254", "C" ,"C-" ,"B" ,"B+", "B"}  };

        try{
            BufferedWriter wi = new BufferedWriter(new FileWriter(".\\st_info.txt"));
                int i = 0;
                for(String[] a:St)
                {
                    int j = 0;
                    for(String[] b:St)
                    {
                        wi.write(St[i][j] + "\t");
                        j++;
                    }
                   i++;
                   wi.write("\r\n");
                   wi.newLine();
                } 
            wi.close();
        }
        catch(IOException ex)
        {
            System.out.print(ex);
        }
    }
}

Comments

0

Note that St (that should be name st to follow Java Naming Conventions) is an array of arrays of Strings.

Let's take a closer look to at your loop:

for(String[] a : St) {   // a is an array
    for(String[] b : St) {  //b is an array (unrelated to a)
       wi.write(St[a][b] + "\t");  //error, a and b are arrays

Your first loop should loop on the array of arrays, the inner loop will now loop on the array, which includes Strings:

for(String[] a : St) {   // a is an array
    for(String b : a) {  //b is a String
       wi.write(b + "\t");  

Comments

0

It seems the answers here have already mentioned the error in your code. However, it only grazes on the cause of your casting error.

In short, you are attempting to call on an index of an array using a String[] and a String as the indices. For example, your code is equivalent to the following...

String[] array = {"My", "first", "array"};
String stringFromArray =  array["first"];

You are attempting to call on the index of the array using a String (and String[] in your first for-each loop).

In order to obtain the item in an array you must call it using it's index position (starting with position 0 in java). The following would be proper use of calling on the specific index...

//          index [0]    [1]       [2]
String[] array = {"My", "first", "array"}; 
String positionZero = array[0];
String positionOne = array[1];
String positionTwo = array[2];

Your output would be , "My", "first", and "array" respectively.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.