-1

I have a very simple question in java

below is my code I am trying to declare a string as given but its showing error in java.

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class Testjava {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

         // one easy string, one that's a little bit harder
        String[] testStrings = {"datefrom":"2018-03-19T23:59:59Z","dateto":"2018-03-22T23:59:59Z", "name" : ["flow"]};

          String encodedString;
          for (String s : testStrings)
          {
        try {
            encodedString = URLEncoder.encode(s, "UTF-8");

          System.out.format("'%s'\n", encodedString);

        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }




    }
}

Can someone tell me how to declare this string value?

Uwe Allner
  • 3,399
  • 9
  • 35
  • 49
TTT
  • 113
  • 2
  • 3
  • 11

1 Answers1

2

Your String array is incorrect. You need to escape the " characters.

Try:

 String[] testStrings = {"datefrom\":\"2018-03-19T23:59:59Z","dateto'\":\"2018-03-22T23:59:59Z", "name\" : [\"flow\"]"};
Cwrwhaf
  • 324
  • 3
  • 5