How to parse the returned string? - java

How to parse the returned string to get the key and value of each pair, and put it all in the collection?What kind of regex use?
#Override
public String toString() {
return "Продукт {" +
" Название продукта = '" + _productName + '\'' +
", Код продукта = '" + _productCode + '\'' +
", Марка металла = '" + _brandMetal + '\'' +
", Вес продукта = " + _weight +
'}';
}
like this:
#Override
public String toString() {
return "Название продукта = " + this.GetProductName() + ";" + " Код продукта = " + this.GetProductCode() + ";" + " Марка металла = " + this.GetBrandMetal() + ";" + " Вес продукта = " + this.GetWeightOfProduct() + ";";
}

Related

Updating SQLite - near "WHERE": syntax error (code 1 SQLITE_ERROR):

I recently added a column to my SQLite database, since adding this column a button which I had which sets the value of one of the database columns to "1" or "0" have now started to crash my application when the button tries to use the updateData(), so I assume thats where my issue is, is in the code or the syntax of the updateData()
Error:
android.database.sqlite.SQLiteException: near "WHERE": syntax error
(code 1 SQLITE_ERROR): , while compiling: UPDATE my_manager SET
location_name = 'blarney stone' , location_county =
'Cork',location_description = 'jj',location_route =
'1',location_position = '2',location_longg = 'null',location_lat =
'null',location_url = 'JJ',location_url2 = 'jj',location_url3 = 'jj',
WHERE location_id = '1'
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native
Method)
Code:
void updateData(String id, String row_id, String name, String county, String description, String
in_route, String position, String lat, String longg, String url, String url2, String url3) {
System.out.println(TABLE_NAME+"; "+row_id +"; "+ name +"; "+ county+"; "+ description+"; " + lat
+"; "+ longg+"; "+ url +"; "+ url2 +"; "+ url3 +"; ");
SQLiteDatabase db = this.getReadableDatabase();
db.execSQL("UPDATE " + "my_manager" + " SET location_name = "+"'"+ name + "' " + ", " +
"location_county = " + "'"+ county + "'"+ "," +
"location_description = " + "'"+ description + "'" + "," +
"location_route = " + "'"+ in_route + "'" + "," +
"location_position = " + "'"+ position + "'" + "," +
"location_longg = " + "'"+ longg + "'" + "," +
"location_lat = " + "'"+ lat + "'" + "," +
"location_url = " + "'"+ url + "'" + "," +
"location_url2 = " + "'"+ url2 + "'" + "," +
"location_url3 = " + "'"+ url3 + "'" + "," + " WHERE location_id = "+"'"+ row_id+"'");
Sorry my friend don't take this as a bad but how about you avoid to much unnecessary "+" if it is possible.
void updateData("UPDATE my_manager SET location_name = '"+ name + "', " +
"location_county = '"+ county + "', " +
"location_description = '"+ description + "'," +
"location_route = '"+ in_route + "', " +
"location_position = '" + position + "', " +
"location_longg = '" + longg + "'," +
"location_lat = '" + lat + "'," +
"location_url = '" + url + "', " +
"location_url2 = '"+ url2 + "', " +
"location_url3 = '"+ url3 + "' WHERE location_id = '"+ row_id+"'");

How to replace following code with the following changes. Please see the code snippet

I want to change the firstPattern with the secondPattern. When I am using Pattern Matcher / String.replaceAll()
I am using the following logic to replace
System.out.println(fileContent.replaceAll(firstPattern, secondPattern));
I am getting following error =
at java.util.regex.Pattern.error(Pattern.java:1955)
at java.util.regex.Pattern.closure(Pattern.java:3157)
at java.util.regex.Pattern.sequence(Pattern.java:2134)
at java.util.regex.Pattern.expr(Pattern.java:1996)
at java.util.regex.Pattern.compile(Pattern.java:1696)
at java.util.regex.Pattern.<init>(Pattern.java:1351)
at java.util.regex.Pattern.compile(Pattern.java:1028)
at java.lang.String.replaceAll(String.java:2223)
It is happening because in the first pattern I have used "{" and "}" brace. So it is throwing an error. Don't know how to replace these.
String firstPattern = "text.append(\" where \");\r\n" +
" text.append(\"ObjId\" + \" = \");\r\n" +
" text.append(getObjId().toString());\r\n" +
"\r\n" +
" text.append(\" and \");\r\n" +
" text.append(\" LastCngDTime = \");\r\n" +
"\r\n" +
" try {\r\n" +
" tempString = dbaExecObj.Get_DataConverter()\r\n" +
" .SmsTimeStampToDBMSTimestampString(LastCngDTime,\r\n" +
" \"-WQ\");\r\n" +
" } catch (Exception e) {\r\n" +
" throw new SmsTntResult(new SmsSystemResult(logger,\r\n" +
" SmsCsvWhoAmI.getMyFullyQualifiedMethodName(),\r\n" +
" SmsTntCodes.DATA_TYPE_CONVERSION_ERROR,\r\n" +
" SmsSystemResult.ERROR));\r\n" +
" }\r\n" +
"\r\n" +
" text.append(tempString);";
String secondPattern = "text.append(\" where \");\r\n" +
" text.append(\"ObjId\" + \" = \");\r\n" +
" text.append(getObjId().toString());\r\n" +
"\r\n" +
" text.append(\" and \");\r\n" +
" text.append(\" LastCngDTime = \");\r\n" +
"\r\n" +
" try {\r\n" +
" tempString = dbaExecObj.Get_DataConverter()\r\n" +
" .SmsTimeStampToDBMSTimestampString(LastCngDTime,\r\n" +
" \"-WQ\");\r\n" +
" } catch (Exception e) {\r\n" +
" throw new SmsTntResult(new SmsSystemResult(logger,\r\n" +
" SmsCsvWhoAmI.getMyFullyQualifiedMethodName(),\r\n" +
" SmsTntCodes.DATA_TYPE_CONVERSION_ERROR,\r\n" +
" SmsSystemResult.ERROR));\r\n" +
" }\r\n" +
"\r\n" +
" text.append(\"CAST(\");" +
" text.append(tempString);" +
" text.append(\" AS datetime)\");";
String fileContent = new Scanner(new File(filePath)).useDelimiter("\\A").next();
System.out.println(fileContent.replaceAll(firstPattern, secondPattern));

Syntax Error: Encountered "RECURRING"

I was trying to write a SQL statement that updates my database with the new information given. When I run the program it gives me an error saying that it has encountered "RECURRING" at line 1, column 401. What does this mean? I've added to the database using this word before, so I don't understand what the issue is. Here's the code...
stmt.executeUpdate("UPDATE \"CUSTOMERS\" SET FIRSTNAME = " + "'" + customer.getFirstName() + "'" + ", LASTNAME = " + "'" + customer.getLastName() + "'" + ", EMAIL = " + "'" + customer.getEmail() + "'" + ", PHONE = " + "'" + customer.getPhone() + "'" + ", ADDRESS = " + "'" + customer.getAddress() + "'" + ", GROUPONNUMBER = " + "'" + customer.getGrouponNumber() + "'" + ", NOTES = " + "'" + customer.getNotes() + "'" + ", ONETIME = " + customer.getOneTime() + ", RECURRING = " + customer.getRecurring()+ ", NONRESPONSIVE = " + customer.getNonResponsive() + " WHERE FIRSTNAME = " + "'" + active.getFirstName() + "'" + " AND LASTNAME = " + "'" + active.getLastName() + "'" + " AND EMAIL = " + "'" + active.getEmail() + "'" + "AND PHONE = " + "'" + active.getPhone() + "'" + "AND ADDRESS = " + "'" + active.getAddress() + "'" + "AND GROUPONNUMBER = " + "'" + active.getGrouponNumber() + "'" + "AND NOTES = " + "'" + active.getNotes() + "'" + " AND ONETIME = " + active.getOneTime() + "AND RECURRING = " + active.getRecurring() + "AND GROUPON = " + active.getGroupon() + "AND NONRESPONSIVE = " + active.getNonResponsive());
I've haven't used SQL too much, so I don't really know what I'm doing to be honest.

Sqlite Database not updating using java

I am trying to update my new database but for some reason after executing my Prepared Statement the data remains the same. It returns no errors or any exceptions. Please have a look at my code and see if you can help me. Thanks in advance.
try {
connect();
PreparedStatement update = con.prepareStatement("UPDATE generalInfo SET "
+ "site" + " = '"
+ siteTt + "' , "
+ "area" + " = '"
+ areaTt + "' , "
+ "unit" + " = '"
+ unitTt + "' , "
+ "unitName" + " = '"
+ unitNameTt + "' , "
+ "drawing" + " = '"
+ drawingTt + "' , "
+ "system" + " = '"
+ systemTt + "' , "
+ "stream" + " = '"
+ streamTt + "' , "
+ "product" + " = '"
+ productTt + "' , "
+ "equipmentLoc" + " = '"
+ equipLocTt + "' , "
+ "specificLoc" + " = '"
+ specificLocTt + "' , "
+ "camOperator" + " = '"
+ camTechTt + "' , "
+ "camSerial" + " = '"
+ camSerialTt + "' , "
+ "gasSurveyOperator" + " = '"
+ surveyTechTt + "' , "
+ "gasSurveySerial" + " = '"
+ surveySerialTt + "' , "
+ "equipmentDesc" + " = '"
+ equipDescTt + "' , "
+ "equipmentType" + " = '"
+ equipTypeTt + "' , "
+ "equipmentSize" + " = '"
+ equipSizeTt + "' , "
+ "equipmentID" + " = '"
+ equipIDTt + "' , "
+ "[Maintenance type]" + " = '"
+ repairTt + "' , "
+ "measurementPosition" + " = '"
+ sourceTt + "'"
+ " WHERE leakerID = " + Integer.parseInt(leakerIDCombo.getSelectedItem().toString())+";");
update.closeOnCompletion();
update.executeUpdate();
con.close();
System.out.println("saved");
} catch (SQLException ex) {
Logger.getLogger(RefineryData.class.getName()).log(Level.SEVERE, null, ex);
}
never forgett to commit your changes:
con.setAutoCommit(true);
update.closeOnCompletion();
update.executeUpdate();
con.close();
or
update.closeOnCompletion();
update.executeUpdate();
con.commit();
con.close();

Adding random arrays to random arrays in java to make it equal another array

I am learning java, and from what I can tell, what I am looking to do is a rare situation.
I am trying to use an API (kindof) to randomly generate musical notes. I want it to generate 20 times so i have it in a for loop. I realize that i could have used a list for this I just dont know how I could have implemented it. The question I have is, when I try to compile this code, the first part runs. It lets me make the seed. However after that it gives me
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at co.kbcomp.music.Main.main(Main.java:64)
What could I do to prevent this? I know that what I am doing is wrong. That much I dont need to be told. What I want to know is where am I going wrong.
package co.kbcomp.music;
import java.util.*;
import org.jfugue.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Please enter a number for the seed of the song");
long seed = in.nextLong();
Calendar cal = Calendar.getInstance();
Random rand;
//rand = new Random(cal.getTime());
rand = new Random(seed);
int NoteNumber = 0;
int NoteLength = 0;
int OctiveNumber = 0;
int ChordNumber = 0;
int InversionNumber = 0;
//int Duration = rand.nextInt(100 - 5) + 5;
//This keeps track of the iteration of the for loop.
String[] NN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] NL = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] IN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] CN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] ON = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
//This is what is being played
String[] note = { "A", "B", "C", "D", "E", "F", "G" };
String[] noteLength = {"", "w", "", "h", "", "q","", "i"};
String[] octive = { "","1","", "2", "", "3", "", "4", "", "5", "", "6", "", "7", "", "8", "", "9"};
String[] chord = { "", "maj", "", "min"};
String[] inversion = {"", "^", "", "^^", "", "^^^", "", "^^^^", "", "^^^^^"};
String[] key = {"",""};
String keys= " ";
String randstr = " ";
//this is the loop that defines the music legnth
for (int i = 0; i < 21; i++) {
NoteNumber = rand.nextInt(7);
NoteLength = rand.nextInt(8);
OctiveNumber = rand.nextInt(18);
ChordNumber = rand.nextInt(4);
InversionNumber = rand.nextInt(10);
NN[i] = note[NoteNumber]; // This randomly generates the note to be played.
NL[i] = noteLength[NoteLength]; // This randomly generates the length of the note.
ON[i] = octive[OctiveNumber]; // This defines the octive to be played in.
CN[i] = chord[ChordNumber]; // This is defines the major or the minor
IN[i] = inversion[InversionNumber]; // IN[i] = inversion[InversionNumber];
key[i] = NN[i] + NL[i] + ON[i] + CN[i] + IN[i];
//randstr = c[0] + " " + c[1] + " " + c[2] + " " + c[3] + " " + c[4] + " " + c[5] + " " + c[6] + " " + c[7] + " " + c[8] + " " + c[9] + " " + c[10] + " " + c[11] + " " + c[12] + " " + c[13] + " " + c[14] + " " + c[15] + " " + c[16];
keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] + " " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] + " " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " + key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] + " " + key[19] + " " + key[20]);
}
System.out.println(key);
Player player = new Player();
Pattern pattern = new Pattern(key[0]);
player.play(pattern);
}
}
You declare your key array with a length of two:
String[] key = {"",""};
But then later in your for loop, you try to access elements beyond the length of your array:
keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] +
" " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] +
" " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " +
key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] +
" " + key[19] + " " + key[20]);
Since your array has only a length of two, when you try to access the third element (at array index 2), you get an ArrayIndexOutOfBoundsException.
String[] key = {"",""};
...
for (int i = 0; i < 21; i++) {
...
key[i] = ....
Do you see the problem?
As you array in only 20 in length
then this
for (int i = 0; i < 21; i++) {
is going to cause an overflow
It should be < 20
Plus this code is meaningless as your key is only an array of 2
keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] + " " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] + " " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " + key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] + " " + key[19] + " " + key[20]);

Categories