As per the following codes:
char Secgrp = 0;
try{
String a = (String) comboName.getSelectedItem();
String fill = "Select recDate, attendant, grp from log order by id DESC";
ps = conn.prepareStatement(fill);
rs = ps.executeQuery();
String x = cDate();
while (rs.next()){
java.sql.Date dDate = rs.getDate("recDate");
String bName = rs.getString("attendant");
char[] cbuf = new char[1];
rs.getCharacterStream("grp").read(cbuf);
char theValue = cbuf[0];
String grp = Character.toString(theValue);
if(x.equals(dDate.toString()) && group1 == theValue && a.equals(bName)){
Secgrp = theValue++ ;
}
else if(x.equals(dDate.toString()) && theValue > group1 && a.equals(bName))
{ Secgrp = theValue++;
}
else if(x.equals(dDate.toString()) && group1 > theValue && a.equals(bName))
{ Secgrp = group1;
}
As per the above codes, I'm trying to check if a char is equal to my resultset record. e.g. if A is equal to A, if date = date and name = name than do something.
But when I setText(String.valueOf(Secgrp)), no char is being inserted.
Please advice how can I retrieve a char e.g 'A' from resultSet.
Solution: Add breaks after each blocks
if(x.equals(dDate.toString()) && group1 == theValue && a.equals(bName)){
Secgrp = theValue++ ;
Break;
}
else if(x.equals(dDate.toString()) && theValue > group1 && a.equals(bName))
{ Secgrp = theValue++;
Break;
}
else if(x.equals(dDate.toString()) && group1 > theValue && a.equals(bName))
{ Secgrp = group1;
Break;
}
Related
I have the following :
String contain 5* ;
char opr ;
int data ;
I would like to store the 5 in the data and * in the opr
any idea how to do it ?
as i know if the string contain only Integer then I will split and the parse it but in this case the String contain int and char
Input :
String input = 5* ;
I want the Output will be like this :
this.opr = *
this.data = 5
A simple way to achieve that (if you don't want to use Regex) is to do something like this:
String temp ="";
// read every char in the input String
for(char c: input.toCharArray()){
// if it's a digit
if(Character.isDigit(c)){
temp +=c; // append it
}
else{ // at the end parse the temp String
data = Integer.parseInt(temp);
opr = c;
break;
}
}
//test
System.out.println("Input: " + input
+ "\t Data: " + data
+ "\t Opr: " + opr);
Test
Input: 5* Data: 5 Opr: *
Input: 123* Data: 123 Opr: *
Under the dubious assumption that the issue is parsing a String in the form of number followed by op, this code will achieve the desired result.
public static void main(String[] args)
{
final String input = "5*";
int data = -1;
String op = "";
Pattern pat = Pattern.compile("([\\d]+)[\\s]*(.*)");
Matcher m = pat.matcher(input);
if (m.matches() && m.groupCount() == 2) {
data = Integer.parseInt(m.group(1));
op = m.group(2).trim();
}
System.out.printf("%2d with op %s%n", data, op);
}
Output:
5 with op *
String input = 5*;
char[] splitted = input.toCharArray();
char opr = splitted[1];
int data = Character.getNumericValue(splitted[0]);
you may also try this with 3 different inputs as below. It will print 0 for incorrect DATA and blank for incorrect OPERAND.
String input = "5*";
//String input = "109/";
//String input = "109b3*";
int data = 0;
char opr = ' ';
int len = input.length();
if ( len > 1 ) {
String data_s = input.substring(0, len - 1);
try {
data = Integer.valueOf(data_s).intValue();
} catch(Exception e) {
}
opr = input.substring(len - 1). charAt(0);
if ( opr != '+' && opr != '-' && opr != '*' && opr != '/' ) {
opr = ' ';
}
}
System.out.println("Input:" + input + " Data:" + data + " Opr:" + opr);
public void searchKlijenta(KlijentiFormEvent klijentiFormEvent) throws SQLException {
String nazivK = klijentiFormEvent.getNaziv();
String adresaK = klijentiFormEvent.getAdresa();
String gradK = klijentiFormEvent.getGrad();
String drzavaK = klijentiFormEvent.getDrzava();
String telefonK = klijentiFormEvent.getTelefon();
String faxK = klijentiFormEvent.getFax();
String mailK = klijentiFormEvent.getMail();
String mobitelK = klijentiFormEvent.getMobitel();
String oibK = klijentiFormEvent.getOib();
String ugovorK = klijentiFormEvent.getUgovor();
String osobaK = klijentiFormEvent.getOsoba();
if (nazivK.length() == 0)
nazivK = null;
if (adresaK.length() == 0)
adresaK = null;
if (gradK.length() == 0)
gradK = null;
if (drzavaK.length() == 0)
drzavaK = null;
if (telefonK.length() == 0)
telefonK = null;
if (faxK.length() == 0)
faxK = null;
if (mailK.length() == 0)
mailK = null;
if (mobitelK.length() == 0)
mobitelK = null;
if (oibK.length() == 0)
oibK = null;
if (ugovorK.length() == 0)
ugovorK = null;
if (osobaK.length() == 0)
osobaK = null;
klijentiSearchModel.clear();
String sql = "select * from zavrsni.klijenti where naziv like '"+nazivK+"' or adresa like '"+adresaK+"' or grad like '"+gradK+"' or drzava like '"+drzavaK+"' or telefon like '"+telefonK+"' or fax like '"+faxK+"' or mail like '"+mailK+"' or mobitel like '"+mobitelK+"' or oib like '"+oibK+"' or ugovor like '"+ugovorK+"' or osoba like '"+osobaK+"' ";
Statement selectStmt = con.createStatement();
ResultSet result = selectStmt.executeQuery(sql);
while(result.next()) {
int id = result.getInt("id");
String naziv = result.getString("naziv");
String adresa = result.getString("adresa");
String grad = result.getString("grad");
int posBr = result.getInt("posBr");
String drzava = result.getString("drzava");
String telefon = result.getString("telefon");
String fax = result.getString("fax");
String mail = result.getString("mail");
String mobitel = result.getString("mobitel");
String oib = result.getString("oib");
String ugovor = result.getString("ugovor");
String osoba = result.getString("osoba");
KlijentiModelSearch klijentSearch = new KlijentiModelSearch(id, naziv, adresa, grad, posBr, drzava, telefon, fax, mail, mobitel, oib, ugovor, osoba);
klijentiSearchModel.add(klijentSearch);
}
result.close();
selectStmt.close();
}
Can i write this code shorter? I think of "if" statement?
Perhaps through a while loop?
Method that is use for search some client in database. This method work fane but this if-statement i want write shorter.
Thanks
EDIT SOLVED:
public void traziKlijenta(KlijentiFormEvent klijentiFormEvent) throws SQLException {
String nazivK = returnNullIfEmptys(klijentiFormEvent.getNaziv());
String adresaK = returnNullIfEmptys(klijentiFormEvent.getAdresa());
String gradK = returnNullIfEmptys(klijentiFormEvent.getGrad());
String drzavaK = returnNullIfEmptys(klijentiFormEvent.getDrzava());
String telefonK = returnNullIfEmptys(klijentiFormEvent.getTelefon());
String faxK = returnNullIfEmptys(klijentiFormEvent.getFax());
String mailK = returnNullIfEmptys(klijentiFormEvent.getMail());
String mobitelK = returnNullIfEmptys(klijentiFormEvent.getMobitel());
String oibK = returnNullIfEmptys(klijentiFormEvent.getOib());
String ugovorK = returnNullIfEmptys(klijentiFormEvent.getUgovor());
String osobaK = returnNullIfEmptys(klijentiFormEvent.getOsoba());
klijentiSearchModel.clear();
String sql = "select * from zavrsni.klijenti where naziv like '%"+nazivK+"%' or adresa like '%"+adresaK+"%' or grad like '%"+gradK+"%' or drzava like '%"+drzavaK+"%' or telefon like '%"+telefonK+"%' or fax like '%"+faxK+"%' or mail like '%"+mailK+"%' or mobitel like '%"+mobitelK+"%' or oib like '%"+oibK+"%' or ugovor like '%"+ugovorK+"%' or osoba like '%"+osobaK+"%' ";
Statement selectStmt = con.createStatement();
ResultSet result = selectStmt.executeQuery(sql);
while(result.next()) {
int id = result.getInt("id");
String naziv = result.getString("naziv");
String adresa = result.getString("adresa");
String grad = result.getString("grad");
int posBr = result.getInt("posBr");
String drzava = result.getString("drzava");
String telefon = result.getString("telefon");
String fax = result.getString("fax");
String mail = result.getString("mail");
String mobitel = result.getString("mobitel");
String oib = result.getString("oib");
String ugovor = result.getString("ugovor");
String osoba = result.getString("osoba");
KlijentiModelSearch klijentSearch = new KlijentiModelSearch(id, naziv, adresa, grad, posBr, drzava, telefon, fax, mail, mobitel, oib, ugovor, osoba);
klijentiSearchModel.add(klijentSearch);
}
result.close();
selectStmt.close();
}
private String returnNullIfEmptys(String value) {
if (value == null || value.length() == 0) {
return null;
}
return value;
}
With your actual code, #khelwood proposition in your comment question is the best approach.
Other solutions have overhead and change your design without bringing a added value .
public static String returnNullIfEmpty(String value){
if (value == null || value.length() == 0){
return null;
}
return value;
}
Then you can call it in this way :
nazivK = returnNullIfEmpty(nazivK);
adresaK= returnNullIfEmpty(adresaK);
EDIT
With the edit of your question, you could include processing as the time where you retrieve the value from the klijentiFormEvent object :
String nazivK = returnNullIfEmpty(klijentiFormEvent.getNaziv());
String adresaK = returnNullIfEmpty(klijentiFormEvent.getAdresa());
...
You simply have to put your arrays/lists ... whatever those things are ... into another array or list.
Then you iterate that array/list.
Done.
And hint: your naming could be improved dramatically. Your names should indicate what the "thing" behind the variable actually is.
Also you can use Map<String, List<?>> to store your lists/arrays/strings. for example with List:
Map<String, List<?>> map = new HashMap<>();
map.put("nazivK", new ArrayList<>());
map.put("adresaK", new ArrayList<>());
//.....
//replace all lists with null
map.replaceAll((s, list) -> list.isEmpty() ? null : list);
//or just remove it
for(Iterator<Map.Entry<String, List<?>>> it = map.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, List<?>> entry = it.next();
if(entry.getValue().isEmpty()) {
it.remove();
}
}
As it was suggested by GhostCat, put your values into array/list.
You can do for example something like this (I suppose those values are Strings):
/* Order in array nazivK, adresaK, gradK, drzavaK, telefonK,
faxK, mailK, mobitelK, oibK, ugovorK, osobaK */
String values[] = new String[11];
for (String val: values) {
if (val == null || val.length() == 0) {
val = null;
}
}
I have a series of notifications which appear two at a time to display a series of a total of 6-10 instructions (depending on the results of a web service call.) This portion of the code is working fine. The problem is when clicking BACKWARDS through the notifications cycle (if you click any of the two displayed notifications it will display the previous two notifications) it gets hungup at step 5 and does not display the 5th notifications and I'm having a bit of trouble determining why.
SCREENSHOT:
http://datasettings.site90.net/Screenshot_2013-10-03-11-37-20.png
SPECIFIC SOURCE:
private void setNotificationStrings(Bundle extras) {
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
// The code below sets the values of the current (field and value),
// previous, and beforePrevious
// Those hold the strings to display in the 2 notifications and the
// strings to go back to
// if the user decides to back track
if (i == count) {
done = true;
}
if (extras != null) {
if (((tm.getSimOperator()).equals(getString(R.string.numeric_tmo)))) {
Log.w(TAG, "Extras not null");
previousValue = extras.getString(getString(R.string.config_name_label));
value = extras.getString(getString(R.string.apn_label));
if (previousValue != null && value == null) {
previousField = getString(R.string.config_name_label);
previousValue = valuez.getAsString("name");
Log.w("previousValue", previousValue);
beforePreviousField = "step1";
beforePreviousValue = "> New APN";
//
field = getString(R.string.apn_label);
value = valuez.getAsString("apn");
extras.clear();
currentStep = 2;
if (i == 1) {
j++;
}
Log.d("currentStep = ", " 2");
} else {
previousValue = extras.getString(getString(R.string.apn_label));
if (proxyArr.size() > 0)// if proxy and port are not null
value = extras.getString(getString(R.string.proxy_label));
else {
value = extras.getString(getString(R.string.mmsc_label));
}
if (previousValue != null && value == null) {
previousField = getString(R.string.apn_label);
beforePreviousField = getString(R.string.config_name_label);
beforePreviousValue = valuez.getAsString("name");
if (proxyArr.size() > 0) {
field = getString(R.string.proxy_label);
value = valuez.getAsString("proxy");
} else {
field = getString(R.string.mmsc_label);
value = valuez.getAsString("mmsc");
}
extras.clear();
currentStep = 3;
Log.d("currentStep = ", " 3");
} else {
if (proxyArr.size() > 0) {// if proxy and port are not
// null
previousValue = extras.getString(getString(R.string.apn_label));
value = extras.getString(getString(R.string.port_label));
if (previousValue != null && value == null) {
previousField = getString(R.string.apn_label);
beforePreviousField = getString(R.string.config_name_label);
beforePreviousValue = valuez.getAsString("name");
field = getString(R.string.port_label);
value = valuez.getAsString("port");
extras.clear();
currentStep = 4;
Log.d("currentStep = ", " 4");
} else {
previousValue = extras.getString(getString(R.string.port_label));
value = extras.getString(getString(R.string.mmsc_label));
if (previousValue != null && value == null) {
previousField = getString(R.string.port_label);
beforePreviousField = getString(R.string.proxy_label);
beforePreviousValue = valuez.getAsString("proxy");
field = getString(R.string.mmsc_label);
value = valuez.getAsString("mmsc");
extras.clear();
currentStep = 5;
Log.d("currentStep = ", " 5");
} else {
previousValue = extras.getString(getString(R.string.port_label));
if (mmsproxyArr.size() > 0) {// if mmsproxy
// and
// mmsport
// are not
// null
value = extras.getString(getString(R.string.mms_proxy_label));
} else {
value = extras.getString(getString(R.string.type_label));
}
if (previousValue != null && value == null) {
previousField = getString(R.string.mmsc_label);
beforePreviousField = getString(R.string.port_label);
beforePreviousValue = valuez.getAsString("port");
if (mmsproxyArr.size() > 0) {
field = getString(R.string.mms_proxy_label);
value = valuez.getAsString("mmsproxy");
} else {
field = getString(R.string.type_label);
value = getString(R.string.type);
}
extras.clear();
currentStep = 6;
if (i < nameArr.size() - 1 && currentStep == 6) {
i++;
Log.d("currentStep = ", " 6");
} else {
if (mmsproxyArr.size() > 0) {// if
// mmsproxy
// and
// mmsport
// are
// not
// null
previousValue = extras.getString(getString(R.string.mms_proxy_label));
value = extras.getString(getString(R.string.mms_port_label));
if (previousValue != null && value == null) {
previousField = getString(R.string.mms_proxy_label);
beforePreviousField = getString(R.string.mmsc_label);
beforePreviousValue = valuez.getAsString("mmsc");
field = getString(R.string.mms_port_label);
value = valuez.getAsString("mmsport");
extras.clear();
currentStep = 7;
Log.d("currentStep = ", " 7");
} else {
previousValue = extras.getString(getString(R.string.mms_port_label));
value = extras.getString(getString(R.string.type_label));
if (previousValue != null && value == null) {
previousField = getString(R.string.mms_port_label);
beforePreviousField = getString(R.string.mms_proxy_label);
beforePreviousValue = valuez.getAsString("mmsproxy");
field = getString(R.string.type_label);
value = getString(R.string.type);
extras.clear();
currentStep = 8;
Log.d("currentStep = "," 8");
FULL SOURCE:
https://docs.google.com/document/d/1EjAo4bpnEjj6jExt7RY28EiA-fZIa1ExMBOfxzdGc3I/edit?usp=sharing
I work with j2me polish 2.0.7, in eclipse 3.4.2, with wtk2.5.2_01.
I create control which draws text: normal, bold, and italic.
The code below is parsing raw text, and search for * and _ symbols, if found than add to draw vector the text and the drawer, and it's just stops after getting second time to the line 58:
String test = new String(raw_text_buff.substring(iter));
it stops in raw_text_buff.substring(iter), ONLY in debug mode..
raw text is: bla bla bla *1000* bla bla
Full code:
private String raw_text = "bla bla bla *1000* bla bla";
Vector draw_items = null;
private void prepareText()
{
char open_char = 0;
int open_pos = 0;
Object []param = null;
StringBuffer sb = new StringBuffer();
String raw_text_buff = new String(raw_text);
int iter = 0;
boolean was_reset = false;
while(true)
{
char c = raw_text_buff.charAt(iter);
if(iter == raw_text_buff.length() || c == '*' || c == '_')
{
if(sb.length() > 0)
{
BmFont viewer = null;
String str = sb.toString();
if(open_char == '*' && null != bm_strong)
{
viewer = bm_strong.getViewer(str);
}else
if(open_char == '_' && null != bm_italic)
{
viewer = bm_italic.getViewer(str);
}else if(null != bm_normal)
{
viewer = bm_normal.getViewer(str);
}else
{
}
param = new Object[2];
param[0] = str;
param[1] = viewer;
if(null == draw_items)
draw_items = new Vector();
draw_items.addElement(param);
sb = new StringBuffer();
if(open_char == 0 && (c == '*' || c=='_'))
open_char = c;
else
open_char = 0;
String test = new String(raw_text_buff.substring(iter)); // stucks here.
raw_text_buff = test;
iter = 0;
was_reset = true;
}else
{
open_char = c;
}
if(iter == raw_text_buff.length())
break;
}else
{
sb.append(c);
}
++iter;
}
}
What I'm doing wrong?
I have this method in my database class.and I want to get a part of data from a column which is "dateOfBirth" in MySQL table ,but I don't know why the list.size() is "0" but when i use System.out.println() in my code it will show just the first line of sql table ,although I have two rows!!!
my method:
public static int getBirthPercent(String i) throws SQLException {
Statement stmt = conn.createStatement();
List<String> list = null;
if (i.equals("O")) {
ResultSet rst = stmt.executeQuery("SELECT dateOfBirth from birthtable");
while (rst.next()) {
String s1 = rst.getString(1);
if (rst.wasNull()) {
s1 = null;
}
String s2 = s1.substring(s1.length() - 4);
int s3 = Integer.parseInt(s2);
if (list == null && s3 < 1970) {
list = new ArrayList<String>();
list.add(s2);
} else {
list = new ArrayList<String>(0);
}
}
}
if (i.equals("N")) {
ResultSet rst = stmt.executeQuery("SELECT dateOfBirth from birthtable");
while (rst.next()) {
String s1 = rst.getString(1);
if (rst.wasNull()) {
s1 = null;
}
String s2 = s1.substring(s1.length() - 4);
int s3 = Integer.parseInt(s2);
if (list == null && s3 > 2000) {
list = new ArrayList<String>();
list.add(s2);
System.out.println(list);
} else {
list = new ArrayList<String>(0);
}
}
}
it will return "0" for all "if" situation but the System.out.println() ,shows [2006] which is one of my row's column's year,although I have two rows it must show [2006,2009].but it doesn't!!!
Now try this code, and let us know. Cheers.
public static int getBirthPercent(String i) throws SQLException {
Statement stmt = conn.createStatement();
ResultSet rst = null;
List<String> list = new ArrayList<String>();
if (i.equals("O")) {
rst = stmt.executeQuery("SELECT dateOfBirth from birthtable");
while (rst.next()) {
String s1 = rst.getString(1);
if (s1 != null && !s1.isEmpty()) {
String s2 = s1.substring(s1.length() - 4);
int n = Integer.parseInt(s2);
if (n < 1970) {
list.add(s2);
}
}
}
}
if (i.equals("N")) {
rst = stmt.executeQuery("SELECT dateOfBirth from birthtable");
while (rst.next()) {
String s1 = rst.getString(1);
if (s1 != null && !s1.isEmpty()) {
String s2 = s1.substring(s1.length() - 4);
int n = Integer.parseInt(s2);
if (n > 2000) {
list.add(s2);
}
}
}
}
System.out.println(list);
}
Enough refactoring for now. Try to do more for yourself. For example,
look into commons-lang StringUtils to replace null checking,
use Date object to store dates and use rs.getDate() instead,
you can use Calendar object to get the year out. Or even SimpleDateFormat object would work too
etc...