Skip to content

Commit

Permalink
почти работает
Browse files Browse the repository at this point in the history
  • Loading branch information
Relect committed Jun 5, 2020
1 parent 7c8d9b0 commit cc190cb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/main/java/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public static void main(String[] args) {
if (!json.startsWith("{\"") ||
!json.endsWith("\"}") ||
(json.indexOf(':') == -1) ||
(json.substring(1,json.lastIndexOf('}')).indexOf("{") != -1) ||
(json.substring(1,json.lastIndexOf('}')).indexOf("}") != -1) ||
(json == null)) throw new InputMismatchException();
String json1 = json.substring(1);

Expand All @@ -20,7 +22,7 @@ public static void main(String[] args) {
System.out.println(str.value(json1));

} catch (InputMismatchException e) {
System.out.println("JSON должен начинаться с \"{\", заканчиваться \"}\" и иметь \":\". ");
System.out.println("JSON должен начинаться с {\", заканчиваться \"} и иметь :. ");
}

}
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@ public class Service {
// get key
public static ArrayList<String> key (String json){
ArrayList<String> result = new ArrayList<String>();
try {

while (!json.startsWith("}")) {
try {
if (json.indexOf('\"') != 0 ||
json.substring(json.indexOf(':')-1,json.indexOf(':')) != "\"")
{throw new InputMismatchException();
break;}
json.substring(json.indexOf(':')-1,json.indexOf(':')) != "\"")
throw new InputMismatchException();
} catch (InputMismatchException e) {
System.out.println("JSON.key должен начинаться с \" и заканчиваться \":.");
break;
}
result.add(json.substring(1, json.indexOf(':') - 1));
if (json.indexOf(',') != -1) {
json = json.substring(json.indexOf(',') + 1);
} else {
json = json.substring(json.indexOf('}'));
}
}
} catch (InputMismatchException e) {
System.out.println("JSON.key должен начинаться с \" и заканчиваться \":.");
}

return result;
}
// get value
Expand Down

0 comments on commit cc190cb

Please sign in to comment.