0

I have a Method that calls a second method, the second method will:

  • Create any missing directories
  • Create a file
  • Decode a 2D String[] to a String (Not working)
  • Write content
  • Write the decoded String to the file with a header (Not working)

First method

public static boolean store(Exception exception, String[][] flags){
    return storePrivate(exception, location, flags);
}

Second Method (Not all code just relevant code)

private static boolean storePrivate(Exception exception, String dir, String[][] flags){
    String flag = "";

    for(int i = 0; i >= flags.length; i++){
        flag = flag + "" +  flags[i][0] + ": " + flags[i][1] + "\n";
    }
    try {
        File directory = new File(dir);
        File file = new File(dir + id + ".txt");
        if (!directory.exists()) {
            directory.mkdirs(); 
        }
        file.createNewFile();
        FileWriter filewriter = new FileWriter(file.getAbsoluteFile());
        BufferedWriter writer = new BufferedWriter(filewriter);


        if(flag != ""){
            writer.write("Flags by Developer: ");
            writer.write(flag);
        }   
        writer.flush();
        writer.close();
        return true;
    } catch (IOException e) {
        return false;
    }
}

Call to the first method

public static void main(String[] args) {
    try {
        test();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        ExceptionAPI.store(e, new String[][]{{"flag1", "Should be part of flag1"}, {"flag2", "this should be flag 2 contence"}});
    }
}

public static void test() throws IOException{
    throw new IOException();
}

I cant find why this won't work. I think it has to do with the second method, particularly

if(flag != ""){
    writer.write("Flags by Developer: ");
    writer.write(flag);
}  

Thanks if anyone can help me.

Curlip

3
  • What is the use of the Exception in the method? Commented Apr 18, 2014 at 7:13
  • 1
    As @ambigram_maker pointed out in a comment to the first answer, your loop condition i >= flags.length is wrong and should instead be i < flags.length. You initialize i to 0, which is not greater than or equal to flags.length when it contains any elements (length will be 1). Commented Apr 18, 2014 at 7:21
  • @ambigram_maker part of the code I left out Commented Apr 18, 2014 at 7:29

1 Answer 1

2

Try this if you want to just convert an array of strings into a single string:

    String[] stringTwoD = //... I think this is a 1D array, and Sting[][] is a 2D, anyway
    String stringOneD = "";
    for (String s : stringTwoD) 
        stringOneD += s;//add the string with the format you want

BTW, your loop condition seems wrong and ,so you may change it to :

for(int i = 0; i < flags.length; i++){
    flag += flags[i][0] + ": " + flags[i][1] + "\n";
}
Sign up to request clarification or add additional context in comments.

12 Comments

Exactly... OP writes ... i >= flags.length; i++)...
You'd better use a StringBuilder.
@ambigram_maker> I just wanted to notify him/her about the change he should make. I'll add that approach too.
OP is generally accepted instead of him/her. (Just sayin')
OP's Array is 2-dimensional though (String[][]). His approach is basically fine except the i >= flags.length as @ambigram_maker pointed out.
|

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.