Last active
February 28, 2016 18:20
-
-
Save Viacheslav77/2b9a3eccd03e5a9de52e to your computer and use it in GitHub Desktop.
Parser&Exception Написать метод разбора списка параметров в формате URL: para1=value1¶m2=value2¶m3=value3. В случае ошибки в формате бросать исключение.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Exceptions.Main.WidthException; | |
// Написать метод разбора списка параметров | |
// в формате URL: para1=value1¶m2=value2¶m3=value3. | |
// В случае ошибки в формате бросать исключение. | |
public class MyClass { | |
public static void main(String[] argh){ | |
String[] s = {"para1= ¶m2=value2¶m3=value3", | |
"=value1¶m2=value2¶m3=value3", | |
"para1=роро¶m2=value2¶m3=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