How to parse json object with u0423 code in Android Studio - java

I don't know how to parse this.
{
"code": 1000,
"result_msg": "Successful",
"result": "{\"birthDateAsText\":\"1999-12-3100:00:00.0\",\"birthPlace\":\"\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423,\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\",\"civilId\":\"123\",\"firstname\":\"\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\",\"gender\":\"\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\",\"lastname\":\"\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\",\"nationality\":\"\\u0423\\u0423\\u0423\\u0423\"}"
}
I tried this
if (resultObject.getInt("code") == 1000) {
JSONParse jsonParse = new JSONParse();
JSONObject object = resultObject.getJSONObject("result");
event.onSuccess(jsonParse.convertJson(object));
}
Actually I don't know how to parse and decode jsonobject. Please help me

Try this:
private static String decodeUnicode(String theString) {
char aChar;
int len = theString.length();
StringBuffer outBuffer = new StringBuffer(len);
for (int x = 0; x < len;) {
aChar = theString.charAt(x++);
if (aChar == '\\') {
aChar = theString.charAt(x++);
if (aChar == 'u') {
// Read the xxxx
int value = 0;
for (int i = 0; i < 4; i++) {
aChar = theString.charAt(x++);
switch (aChar) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = (value << 4) + aChar - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = (value << 4) + 10 + aChar - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = (value << 4) + 10 + aChar - 'A';
break;
default:
throw new IllegalArgumentException(
"Malformed \\uxxxx encoding.");
}
}
outBuffer.append((char) value);
} else {
if (aChar == 't')
aChar = '\t';
else if (aChar == 'r')
aChar = '\r';
else if (aChar == 'n')
aChar = '\n';
else if (aChar == 'f')
aChar = '\f';
outBuffer.append(aChar);
}
} else
outBuffer.append(aChar);
}
return outBuffer.toString();
}
Uses:
JSONObject object = resultObject.getJSONObject("result");
System.out.println("Birthday: "+object.getString("birthPlace"))

Decode your response using UTF-8
try {
resp = new String(Base64.decode(response.getBytes("UTF-8"), Base64.NO_WRAP), "UTF-8");
} catch (UnsupportedEncodingException e) {
// Suppress exception (unlikely to happen in prod)
PbLogger.e("Encoding exception", "");
}

Related

Constant Expression Required When Using an Array With a Switch Statement

Hello StackOverflow community, I've just begun to work with arrays and I was wondering how I can make the switch statement work with the values below. It's really irritating that it won't work considering the only error is constant expression required:
import java.lang.Character;
public class Ass11a {
public static char[] vowel = new char[4];
public static char[] consonant = new char[20];
public static char[] number = new char[9];
public static char[] punctuation = new char[10];
public static void main(String[] args) {
EasyReader console = new EasyReader();
System.out.print("Enter a character: ");
char input = console.readChar();
Character.toLowerCase(input);
vowel[0] = 'a';
vowel[1] = 'e';
vowel[2] = 'i';
vowel[3] = 'o';
vowel[4] = 'u';
consonant[0] = 'q';
consonant[1] = 'w';
consonant[2] = 'r';
consonant[3] = 't';
consonant[4] = 'y';
consonant[5] = 'p';
consonant[6] = 's';
consonant[7] = 'd';
consonant[8] = 'f';
consonant[9] = 'g';
consonant[10] = 'h';
consonant[11] = 'j';
consonant[12] = 'k';
consonant[13] = 'l';
consonant[14] = 'z';
consonant[15] = 'x';
consonant[16] = 'c';
consonant[17] = 'v';
consonant[18] = 'b';
consonant[19] = 'n';
consonant[20] = 'm';
number[0] = '1';
number[1] = '2';
number[2] = '3';
number[3] = '4';
number[4] = '5';
number[5] = '6';
number[6] = '7';
number[7] = '8';
number[8] = '9';
number[9] = '0';
punctuation[0] = '.';
punctuation[1] = ',';
punctuation[2] = '?';
punctuation[3] = '!';
punctuation[4] = ';';
punctuation[5] = ':';
punctuation[6] = '"';
punctuation[7] = '\'';
punctuation[8] = '(';
punctuation[9] = ')';
punctuation[10] = '-';
switch(input)
{
case vowel[0]:case vowel[1]:case vowel[2]:case vowel[3]:case vowel[4]:
System.out.println("Vowel");
break;
case consonant[0]:case consonant[1]:case consonant[2]:case consonant[3]:
case consonant[4]:case consonant[5]:case consonant[6]:case consonant[7]:
case consonant[8]:case consonant[9]:case consonant[10]:case consonant[11]:
case consonant[12]:case consonant[13]:case consonant[14]:case consonant[15]:
case consonant[16]:case consonant[17]:case consonant[18]:case consonant[19]:
case consonant[20]:
System.out.println("Consonant");
break;
case number[0]:case number[1]:case number[2]:case number[3]:case number[4]:
case number[5]:case number[6]:case number[7]:case number[8]:case number[9]:
System.out.println("Number");
break;
case punctuation[0]:case punctuation[1]:case punctuation[2]:case punctuation[3]:
case punctuation[4]:case punctuation[5]:case punctuation[6]:case punctuation[7]:
case punctuation[8]:case punctuation[9]:case punctuation[10]:
System.out.println("Punctuation");
break;
default:
System.out.println("Other");
break;
}
}
}
You could use an enum like this
public enum Vowels {
A,E,I,O,U;
static boolean isVowel(char c) {
switch (c) {
case 'A': case 'a':
case 'E': case 'e':
case 'I': case 'i':
case 'O': case 'o':
case 'U': case 'u': return true;
}
return false;
}
static Vowels getVowel(char c) {
switch (c) {
case 'A': case 'a': return A;
case 'E': case 'e': return E;
case 'I': case 'i': return I;
case 'O': case 'o': return O;
case 'U': case 'u': return U;
}
return null;
}
}
and use it like
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
while (console.hasNextLine()) {
String input = console.nextLine();
for (Character ch : input.toCharArray()) {
if (Vowels.isVowel(ch)) {
System.out.println(ch + " is vowel");
}
}
}
}

Changing from an input to a Scanner

I am trying to change this from an input box into a Scanner class, but i am having trouble doing so.
Its a program that takes words and makes them into a phone number here is the code that does so. Any help would be greatly appreciated and if there is something that i can do in return i would gladly do so.
// declare imports
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.*;
public class Telephone {
public static void main(String[] args) {
// ask for the phone number (in letters)
char letter;
String inputMessage = "Please enter the number in Letters " + "or enter '#' to stop the program ";
String inputString = JOptionPane.showInputDialog(inputMessage);
String outputString = "";
String outputMessage = "";
int digit = 0;
int x = 0;
for (int i = 0; i < inputString.length(); i++)
System.out.print(inputString.charAt(x)); {
while (inputString.charAt(x) != '#') {
letter = Character.toUpperCase(inputString.charAt(x));
x++;
// make sure its not a number
if (letter >= 'a' && letter <= 'z') if (letter >= 'A' && letter <= 'Z') {
digit++;
switch (letter) {
case 'A':
case 'B':
case 'C':
outputString += "2";
break;
case 'D':
case 'E':
case 'F':
outputString += "3";
break;
case 'G':
case 'H':
case 'I':
outputString += "4";
break;
case 'J':
case 'K':
case 'L':
outputString += "5";
break;
case 'M':
case 'N':
case 'O':
outputString += "6";
break;
case 'P':
case 'Q':
case 'R':
case 'S':
outputString += "7";
break;
case 'T':
case 'U':
case 'V':
outputString += "8";
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
outputString += "9";
}
if (digit == 7) {
break;
}
if (digit == 3) {
outputString += "-";
}
}
inputMessage = "Enter another set of telephone letters";
}
JOptionPane.showMessageDialog(null, outputString, "Telephone Program", JOptionPane.PLAIN_MESSAGE);
}
Modify the line where you show the JOptionPane as follows,
//String inputString = JOptionPane.showInputDialog(inputMessage);
System.out.println(inputMessage);
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();
so you could do the following,
package test;
import java.util.Scanner;
public class Telephone {
public static void main(String[] args) {
// ask for the phone number (in letters)
char letter;
String inputMessage = "Please enter the number in Letters " + "or enter '#' to stop the program ";
// String inputString = JOptionPane.showInputDialog(inputMessage);
System.out.println(inputMessage);
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();
String outputString = "";
String outputMessage = "";
int digit = 0;
int x = 0;
for (int i = 0; i < inputString.length(); i++) {
System.out.print(inputString.charAt(x));
}
while (inputString != null && inputString.trim().length() > 0 && inputString.charAt(x) != '#') {
letter = Character.toUpperCase(inputString.charAt(x));
x++;
// make sure its not a number
// if (letter >= 'a' && letter <= 'z') {
if (x >= inputString.length()) {
x = 0;
System.out.println("\n" + outputString);
// JOptionPane.showMessageDialog(null, outputString, "Telephone Program", JOptionPane.PLAIN_MESSAGE);
// inputString = JOptionPane.showInputDialog(inputMessage);
System.out.println(inputMessage);
inputString = sc.nextLine();
} else if (letter >= 'A' && letter <= 'Z') {
digit++;
switch (letter) {
case 'A':
case 'B':
case 'C':
outputString += "2";
break;
case 'D':
case 'E':
case 'F':
outputString += "3";
break;
case 'G':
case 'H':
case 'I':
outputString += "4";
break;
case 'J':
case 'K':
case 'L':
outputString += "5";
break;
case 'M':
case 'N':
case 'O':
outputString += "6";
break;
case 'P':
case 'Q':
case 'R':
case 'S':
outputString += "7";
break;
case 'T':
case 'U':
case 'V':
outputString += "8";
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
outputString += "9";
}
if (digit == 7) {
break;
}
if (digit == 3) {
outputString += "-";
}
}
// }
inputMessage = "Enter another set of telephone letters";
}
System.out.println("\n" + outputString);
// JOptionPane.showMessageDialog(null, outputString, "Telephone Program", JOptionPane.PLAIN_MESSAGE);
}
}

Get junk instead of string in toString()

I have a small metod. It's get string with eng characters and return rus characters string (transliteration); But it's something wrong with it. I have no idea what. It's returned not string on Russian, but some junk like "[C#4057db80";
public String getRussianSting(String engString) {
char[] engCharString = engString.toLowerCase().toCharArray();
char[] rusCharString = new char[30];
for (int i = 0; i <= engCharString.length - 1; i++) {
if (engCharString[i] == ' ')
continue;
if (i + 1 <= engCharString.length - 1) {
if (engCharString[i] == 'c' && engCharString[i + 1] == 'h') {
rusCharString[i] = 'ч';
i++;
continue;
} else if (engCharString[i] == 's' && engCharString[i + 1] == 'h') {
rusCharString[i] = 'ш';
i++;
continue;
} else if (engCharString[i] == 't' && engCharString[i + 1] == 'z') {
rusCharString[i] = 'ц';
i++;
continue;
} else if (engCharString[i] == 'y' && engCharString[i + 1] == 'i') {
rusCharString[i] = 'ы';
i++;
} else if (engCharString[i] == 'y' && engCharString[i + 1] == 'e') {
rusCharString[i] = 'э';
i++;
continue;
} else if (engCharString[i] == 'y' && engCharString[i + 1] == 'u') {
rusCharString[i] = 'ю';
i++;
continue;
} else if (engCharString[i] == 'y' && engCharString[i + 1] == 'a') {
rusCharString[i] = 'я';
i++;
continue;
}
}
switch (engCharString[i]) {
case 'a':
rusCharString[i] = 'а';
break;
case 'b':
rusCharString[i] = 'б';
break;
case 'v':
rusCharString[i] = 'в';
break;
case 'g':
rusCharString[i] = 'г';
break;
case 'd':
rusCharString[i] = 'д';
break;
case 'e':
rusCharString[i] = 'е';
break;
case 'j':
rusCharString[i] = 'ж';
break;
case 'z':
rusCharString[i] = 'з';
break;
case 'i':
rusCharString[i] = 'и';
break;
case 'k':
rusCharString[i] = 'к';
break;
case 'l':
rusCharString[i] = 'л';
break;
case 'm':
rusCharString[i] = 'м';
break;
case 'n':
rusCharString[i] = 'н';
break;
case 'o':
rusCharString[i] = 'о';
break;
case 'p':
rusCharString[i] = 'п';
break;
case 'r':
rusCharString[i] = 'р';
break;
case 's':
rusCharString[i] = 'с';
break;
case 't':
rusCharString[i] = 'т';
break;
case 'u':
rusCharString[i] = 'у';
break;
case 'f':
rusCharString[i] = 'ф';
break;
case 'h':
rusCharString[i] = 'х';
break;
case '\'':
rusCharString[i] = 'ь';
break;
default:
break;
}
}
return rusCharString.toString();
}
return rusCharString.toString();
will return the String showing the object representation of the array. That is one major reason char[] is used for sensitive data instead of String.
try:
return new String(rusCharString);
The problem is that when you use the toString of an array, it will use the one that it inherits from Object class.
If you want to get a proper toString of an array, use java.util.Arrays.toString(rusCharString). The junk that you see is the hash code of that array.
If you want to print the hash code separately, use rusCharArray.hashCode()

StringIndexOutOfBoundsException while decoding

I am using ISO9075 decoder in my application. When I try to decode the following String
ISO9075.decode("mediaasset_-g9mdob83oozsr5n_xadda")
its giving the following exception
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 22
at java.lang.String.charAt(Unknown Source)
at org.alfresco.util.ISO9075.matchesEncodedPattern(ISO9075.java:128)
at org.alfresco.util.ISO9075.decode(ISO9075.java:176)
at Test1.main(Test1.java:9)
What may be the problem. Please guide me.
EDIT
Here is my code
public class Test1 {
public static void main(String args[])
{
String s = "mediaasset_-g9mdob83oozsr5n_xadda";
System.out.println(ISO9075.decode(s));
}
}
Thanks.
I just tested it with code found here and could not get your exception.
public static void main(String args[]) {
String s = "mediaasset_-g9mdob83oozsr5n_xadda";
System.out.println(ISO9075.decode(s)); //prints mediaasset_-g9mdob83oozsr5n_xadda
}
public static class ISO9075 {
//I have removed the parts not used by your main()
private static boolean matchesEncodedPattern(String string, int position) {
return (string.length() > position + 6)
&& (string.charAt(position) == '_') && (string.charAt(position + 1) == 'x')
&& isHexChar(string.charAt(position + 2)) && isHexChar(string.charAt(position + 3))
&& isHexChar(string.charAt(position + 4)) && isHexChar(string.charAt(position + 5))
&& (string.charAt(position + 6) == '_');
}
private static boolean isHexChar(char c) {
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
return true;
default:
return false;
}
}
public static String decode(String toDecode) {
if ((toDecode == null) || (toDecode.length() < 7) || (toDecode.indexOf("_x") < 0)) {
return toDecode;
}
StringBuffer decoded = new StringBuffer();
for (int i = 0, l = toDecode.length(); i < l; i++) {
if (matchesEncodedPattern(toDecode, i)) {
decoded.append(((char) Integer.parseInt(toDecode.substring(i + 2, i + 6), 16)));
i += 6;// then one added for the loop to mkae the length of 7
} else {
decoded.append(toDecode.charAt(i));
}
}
return decoded.toString();
}
}

code for calculations in android

I'm working with an application right now for our project in school. My application is about Resistor Color Code calculation. My codes are working, but in displaying the values, I used the value as string. My problem is I want to make my result value as 1.2K ohms, 1.5M ohm or 5.4M ohms, just like that. Because in my codes the result will display 1200 ohms, 1500K ohms or 5400K ohms. Help me Please. Thanks in advance for the help.
This is my code for a, b, c, d and value is the display in EditText.
calcu is a button.
calcu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//for first band
if (a=="Black")
a = " ";
if (a=="Brown")
a = "1";
if (a=="Red")
a = "2";
if (a=="Orange")
a = "3";
if (a=="Yellow")
a = "4";
if (a=="Green")
a = "5";
if (a=="Blue")
a = "6";
if (a=="Violet")
a = "7";
if (a=="Gray")
a = "8";
if (a=="White")
a = "9";
//for second band
if (b=="Black")
b = "0";
if (b=="Brown")
b = "1";
if (b=="Red")
b = "2";
if (b=="Orange")
b = "3";
if (b=="Yellow")
b = "4";
if (b=="Green")
b = "5";
if (b=="Blue")
b = "6";
if (b=="Violet")
b = "7";
if (b=="Gray")
b = "8";
if (b=="White")
b = "9";
//for multiplier
if (c=="Black")
c = " ";
if (c=="Brown")
c = "0";
if (c=="Red")
c = "00";
if (c=="Orange")
c = "000";
if (c=="Yellow")
c = "0000";
if (c=="Green")
c = "00000";
if (c=="Blue")
c = "000000";
if (c=="Violet")
c = "0000000";
if (c=="Gray")
c = "00000000";
if (c=="White")
c = "000000000";
//for Tolerance
if (d=="Brown")
d = "1";
if (d=="Red")
d = "2";
if (d=="Green")
d = "0.5";
if (d=="Blue")
d = "0.25";
if (d=="Violet")
d = "0.1";
if (d=="Gray")
d = "0.05";
if (d=="Gold")
d = "5";
if (d=="Silver")
d = "10";
Value.setText(a + b + c + "\u2126" + " " + "\u00B1" + d + "%" + " Tolerance");
int result = getTheResult();
String Result = "";
if(result > 0 && result < 1000) Result = "" + result + " Ohms";
else if(result >= 1000 && result < 1000000) Result = "" + (result / 1000) + "K Ohms";
else if (result >= 1000000) Result = "" + (result / 1000000) + "M Ohms";
else Result = "Invalid Value";
import javax.swing.JOptionPane;
public class Resistance {
String digit_band1_color;
String digit_band2_color;
String multiplier_band3_color;
String tolerance_band4_color;
int temp1,temp2,temp3;
double temp4;
double result;
public Resistance(String a,String b,String c,String d){
digit_band1_color=a;
digit_band2_color=b;
multiplier_band3_color=c;
tolerance_band4_color=d;
switch (digit_band1_color){
case "Black":
temp1=0;
break;
case "Brown":
temp1=1;
break;
case "Red":
temp1=2;
break;
case "Orange":
temp1=3;
break;
case "Yellow":
temp1=4;
break;
case "Green":
temp1=5;
break;
case "Blue":
temp1=6;
break;
case "Voilet":
temp1=7;
break;
case "Grey":
temp1=8;
break;
case "White":
temp1=9;
break;
}
switch (digit_band2_color){
case "Black":
temp2=0;
break;
case "Brown":
temp2=1;
break;
case "Red":
temp2=2;
break;
case "Orange":
temp2=3;
break;
case "Yellow":
temp2=4;
break;
case "Green":
temp2=5;
break;
case "Blue":
temp2=6;
break;
case "Voilet":
temp2=7;
break;
case "Grey":
temp2=8;
break;
case "White":
temp2=9;
break;
}
switch (multiplier_band3_color){
case "Black":
temp3=0;
break;
case "Brown":
temp3=1;
break;
case "Red":
temp3=2;
break;
case "Orange":
temp3=3;
break;
case "Yellow":
temp3=4;
break;
case "Green":
temp3=5;
break;
case "Blue":
temp3=6;
break;
case "Voilet":
temp3=7;
break;
case "Grey":
temp3=8;
break;
case "White":
temp3=9;
break;
}
switch (tolerance_band4_color){
case "Brown":
temp4=1;
break;
case "Red":
temp3=2;
break;
case "Orange":
temp4=0.05;
break;
case "Yellow":
temp4=0.02;
break;
case "Green":
temp4=0.5;
break;
case "Blue":
temp4=0.25;
break;
case "Voilet":
temp4=0.1;
break;
case "Grey":
temp4=0.01;
break;
case "Gold":
temp4=5;
break;
case "Silver":
temp4=10;
break;
}
result=Math.pow(10,temp3);
System.out.println("Resistance = "+temp1+temp2+result+"+-"+temp4+"%");
}
public static void main(String[] args) {
String a=JOptionPane.showInputDialog(null,"Please Enter Color in Proper Format like this Black,Brown etc");
String b=JOptionPane.showInputDialog(null,"Please Enter Color in Proper Format like this Black,Brown etc");
String c=JOptionPane.showInputDialog(null,"Please Enter Color in Proper Format like this Black,Brown etc");
String d=JOptionPane.showInputDialog(null,"Please Enter Color in Proper Format like this Black,Brown etc");
Resistance calculator=new Resistance(a,b,c,d);
}
}

Categories