Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Last active February 28, 2016 18:20
Show Gist options
  • Save Viacheslav77/2b9a3eccd03e5a9de52e to your computer and use it in GitHub Desktop.
Save Viacheslav77/2b9a3eccd03e5a9de52e to your computer and use it in GitHub Desktop.
Parser&Exception Написать метод разбора списка параметров в формате URL: para1=value1&param2=value2&param3=value3. В случае ошибки в формате бросать исключение.
public class InData {
private String [] [] par1;
public InData () {
par1 = new String [10][10];
}
public static class FormatException extends Exception {
public FormatException (String message){
super(message);
}
public String GetMessage(){
return "FormatException" + super.getMessage();
}
}
public void setData(String s) throws FormatException{
String [] par = s.split("&");
for(int i =0; i < par.length; i++){
String [] par2;
par2 = par[i].split("=");
if(par2[0].equals("")||par2[1].equals(" ")){
throw new FormatException ("Ошибка формата строки");
}
par1[i] [i]= par2[0];
par1[i] [i+1] = par2[1];
System.out.println(" " + par1[i][i] + " : " + par1 [i][i+1]);
}
}
public String getData (int i, int j){
return par1[i][j];
}
}
import Exceptions.Main.WidthException;
// Написать метод разбора списка параметров
// в формате URL: para1=value1&param2=value2&param3=value3.
// В случае ошибки в формате бросать исключение.
public class MyClass {
public static void main(String[] argh){
String[] s = {"para1= &param2=value2&param3=value3",
"=value1&param2=value2&param3=value3",
"para1=роро&param2=value2&param3=value3"
};
InData id = new InData();
for (int i=0 ; i<3 ; i++){
System.out.println(i+1 + " переременная: " + s[i]);
try {
id.setData(s[i]);
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment