Yes adherents of Java will not beat me, I repent, constantly I affect PHP. There is a function that parses the line in the file.

Something like that:

NAME: blabla1 | NUMBER: 2221 | SURNAME: balbal999

I would like the name, surname to give a string, and number - a number. Correctly, I understand that the only normal way out is

int number = 0; try{ number = Integer.parseInt(my_super_parsing_function("NUMBER",input_string)); } catch(NumberFormatException e){ //пишем грозное сообщение о сбое в структуре файла } 

Or is there a way to overload it with two different conclusions with the same parameters, just depending on one of them?

  • The method signature in Java contains both parameters and a return value, so it will not work to overload. It is necessary to return the structure / class - Barmaley


2 answers 2

It is best to return the structure:

 class Parameter { public String Name; public int Number; public String Surname; } Parameter parseLine(string line) { Parameter p = new Parameter(); String[] parts = line.split("|"); if (parts.Length != 3) throw SomeSternFormatException(); p.Name = GetValue(parts[0], "NAME"); p.Number = Integer.parseInt(GetValue(parts[1], "NUMBER")); p.Surname = GetValue(parts[2], "SURNAME"); return p; } String getValue(String input, String expectedKey) { String[] parts = input.split(":"); if (parts.Length != 2) throw SomeSternFormatException(); String key = parts[0].Trim(); if (!expectedKey.equals(key)) throw SomeSternFormatException(); return parts[1].Trim(); } 

(This code comes from a strictly defined order and number of parts.)


For a case of arbitrary order, I would do something like this:

 Parameter parseLine(string line) { Parameter p = new Parameter(); String[] parts = line.split("|"); HashMap<String, String> argmap = new HashMap<String, String>(); foreach (String part : parts) { String[] keyvalue = getKeyValue(part); String key = keyvalue[0]; if (argmap.containsKey(key)) throw SomeSternFormatException(); argmap.put(key, value); } if (argmap.size() != 3) throw SomeSternFormatException(); p.Name = argmap["NAME"]; p.Number = Integer.parseInt(argmap["NUMBER"]); p.Surname = argmap["SURNAME"]; return p; } String[] getKeyValue(String input) { String[] parts = input.split(":"); if (parts.Length != 2) throw SomeSternFormatException(); parts[0] = parts[0].Trim(); parts[1] = parts[1].Trim(); return parts; } 

Exceptions let, by the way, the caller catches.

  • @VladD, so I also try to receive this structure. So I need to understand how to use enum to stuff a string in the Switch, what I need to know which field to fill in. - knes
  • @knes: added the answer. - VladD
  • @knes: or can your fields be random? - VladD
  • May or may not go. I mean, it all depends on me. In my solution through indexOf and substring together with the expectedKey.length (), it just pulled from anywhere. So it's not a problem, I'll figure it out further. Thank. - knes
  • @knes: please! Added option with arbitrary order. - VladD

There are a couple of good options.

  • You know what type will be output? Well, ask for the right one. Yes, you will need two functions - GetAsString and GetAsInt .
  • Return an object that has various fields that identify the type of result.
  • The first option either breeds repetitions of the code (the parsing goes the same way), or it implements the example I have given inside GetAsInt ... The second option is not very clear how to do it. public class retclass {public boolean is_int; public int result_int; public String result_string; some kind of it is awkward. Or did I misunderstand you? - knes
  • If you need to repeat, then you have a problem. Make one method that returns as a string, and a method that returns an integer simply calls the first method and converts it to a number. Otherwise, this repetition will appear in each place of the first method call. the second option is well implemented on the pros enum result_type {R_INT, R_STRING, R_URL}; struct Result {result_type type; void * data; }; In Java, you can do it if you apply inheritance. One abstract class and a successor to each answer. - KoVadim
  • If you need to repeat, then you have problems // Exactly. Therefore, I clarify. - knes
  • Here they write such crutches, as @alexlz suggests, and then they complain that it consumes dozens of gigs. It is not necessary in one language to drag constructs from another. - KoVadim