Code generation in java bean with for each loop - java

This is my function which return null always.. But i want the output as RECY0 for the first time and then second time RECY1 an so on...
public String generateRecycleCode() {
Integer recycleNoIncremnt=0;
String code= null;
for(MaterialRecycleIssueBean materialRecycleIssueBean : materialRecycleIssueBeanlist){
materialRecycleIssueBean.recyleCode = "RECY"+recycleNoIncremnt++;
code = materialRecycleIssueBean.recyleCode;
}
return code;
}

Related

Date Validation in Java fails [duplicate]

This question already has answers here:
If statement gives condition is always true
(4 answers)
Closed 2 years ago.
I'm totally new to Java programming and I'm trying to create a Java FX project. I've followed tutorials about the date validation method but it seems to fail.In this certain part I have to make a list with objects inserted by a user in text fields. That includes a date but it needs to be valid.
Below in this piece of code, the object I need to get validated is datep . I've created a method in which if the string is valid, it should set my flag to true and return it. Before the list is created I inserted an if statement to check whether that my flag is set to true which means that the date is verified according to the format.When I run it,it creates the list whatsoever even if the date is invalid.Am I putting the if statement in the wrong part? Cause I think the method is fine.
#Override
public void handle(MouseEvent event) {
if (event.getSource() == NewrentBtn) {
String vehiclen =OximaTxT.getText();
String clientn = ClientTxT.getText();
String store = StoreTxT.getText();
String storer = StorerTxT.getText();
String timerp = TimeTxT.getText();
String timer = TimerTxT.getText();
String datep = DateTxT.getText(); // <-------------
String dater = DaterTxT.getText();
Integer sum = Integer.parseInt(SumTxT.getText());
if(flag = true) { // <------------
createRental(id, vehiclen, store, datep, timerp, clientn, storer, dater, timer, sum);
clearTextFields();
}
}
public boolean Checkdate(String datep) { // <-------------
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date BOD = null;
df.setLenient(false);
try
{
BOD = df.parse(datep); // <----------------
flag = true;
}
catch(Exception e)
{
flag = false;
}
return flag;
}
public void createRental(int id,String vehiclen,String store,String datep,String timerp,String clientn,String storer,String dater,String timer,int sum ) {
Rental m = new Rental(id,vehiclen,store,datep,timerp,clientn,storer,dater,timer,sum);
RentalList.add(m);
rentalTableView.getItems().add(m);
}
From the looks of what you are trying to achieve here is my suggestion to modify the code.
First of all let me explain to you two issues i found: the first one is that you are missing the call to the validation method of the Date, that is the call to the CheckDate(datep) when you receive the text input and store the flag variable, or so it seems as we dont have the full code (which is ok ); and second you are missing a =in the if(flag = true), it should be if(flag == true)
So here is the full code:
#Override
public void handle(MouseEvent event) {
if (event.getSource() == NewrentBtn) {
String vehiclen =OximaTxT.getText();
String clientn = ClientTxT.getText();
String store = StoreTxT.getText();
String storer = StorerTxT.getText();
String timerp = TimeTxT.getText();
String timer = TimerTxT.getText();
String dater = DaterTxT.getText();
Integer sum = Integer.parseInt(SumTxT.getText());
String datep = DateTxT.getText();
boolean flag = Checkdate(datep);
if(flag == true) {
createRental(id,vehiclen,store,datep,timerp,clientn,storer,dater,timer,sum);
clearTextFields();
}
}
}
This way you are verifying if the date is correctly formatted and continue the process if it is according to your scheme.
Finally i have three recommendations as you are new to java programming:
For all methods the first letter should always be in lowercase like public boolean checkDate() this way you can differentiate a method from a Class, which will always start in Uppercase like public class Product. The only exception for this is the constructor of a class.
You should never mix the graphical interface logic, with the logical processing logic. This is: you should keep the processing part in one package and the graphic component in another and relate both of them by creating an instance of the processing logic in the graphical interface.
The user input validation should be directly made in the handler method with try-catch clauses like the following.
Here:
public void handle(MouseEvent event) {
if (event.getSource() == NewrentBtn) {
String vehiclen =OximaTxT.getText();
String clientn = ClientTxT.getText();
String store = StoreTxT.getText();
String storer = StorerTxT.getText();
String timerp = TimeTxT.getText();
String timer = TimerTxT.getText();
String dater = DaterTxT.getText();
Integer sum = Integer.parseInt(SumTxT.getText());
try {
String datep = DateTxT.getText();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
df.parse(date);
createRental(id,vehiclen,store,datep,timerp,clientn,storer,dater,timer,sum);
clearTextFields();
} catch (ParseException e) {
/* Here you handle what happens when if fails, you can create a JDialog to show
the error or create an alert, whatever you need */
e.printStackTrace();
}
}
}
And voila a cleaner version

Java: How to write a generalized function for this?

How to a write a single generalized for these? I mean the function should take parameters and return the desired string.
String fullName = driver.findElement(By.className("full-name")).getText();
String title = driver.findElement(By.className("title")).getText();
String locality = driver.findElement(By.className("locality")).getText();
String industry = driver.findElement(By.className("industry")).getText();
String connections = driver.findElement(By.xpath("//div[#class='member-connections']/strong")).getText();
String profileLink = driver.findElement(By.className("view-public-profile")).getText();
The function should be something like this:
String getInfo(String className, String byType) {
return driver.findElement(By.byType(className)).getText();
}
EDIT:
I have written this function, but I am not sure how to append byType with By.
static String getInfo(WebDriver driver, String byType, String byParam) {
return driver.findElement(By. + byType + (byParam)).getText();
}
Thanks!
This seems way easier than others are answering so I'm going to put my neck on the line. and say, what's wrong with this...
public String get(WebDriver driver, By by) {
return driver.findElement(by).getText();
}
..and using it like...
String a = get(urDriver, By.className(someName));
String b = get(urDriver, By.xpath(somePath));
You may try this:
public String byXpath(String xpath) {
return driver.findElement(By.xpath(xpath)).getText();
}
public String byClass(String $class) {
return driver.findElement(By.className($class)).getText();
}
Edited:
public String by(By by) {
return driver.findElement(by).getText();
}
String x = by(By.className(name));
String y = by(By.xpath(path));

Type mismatch: cannot convert from String to long

I am getting the above error when I am trying to set a flag and returning the value of the flag.I am writing the code below:
public static long insertEpaymentResDtls(PortalEpaymentResVO portalEpaymentResVO) {
// TODO Auto-generated method stub
String flag = null;
SppPaymentRequestKiosk sppPaymentRequestKiosk = new SppPaymentRequestKiosk();
SppUserEformData sppUserEformData = new SppUserEformData();
sppUserEformData = genericDao.findById(SppUserEformData.class, SppUserEformDataId.class,
new SppUserEformDataId(portalEpaymentResVO.getMtrxId(), portalEpaymentResVO.getOxitrxId(), Integer.parseInt(portalEpaymentResVO.getTrxStatus())));
try
{
if (sppPaymentRequestKiosk != null)
{
sppPaymentRequestKiosk.setStatus(portalEpaymentResVO.getTrxStatus());
}
sppPaymentRequestKiosk = genericDao.save(sppPaymentRequestKiosk);
if (sppPaymentRequestKiosk != null)
{
flag = "Success";
}
}
catch (Exception e)
{
GLOGGER.error("Exceptin occured at the time of updateStatus in EformServiceImpl." + e.getMessage());
}
return flag;//ERROR IN THIS LINE
}
This is the code fragment.Please help.
Your method signature states it returns primitive long.
Since you are returning String flag, you must change your return type to String.
As in:
public static String insertEpaymentResDtls(etc...
Change you method return type to String, how can you decalre long return and return String flag
public static String insertEpaymentResDtls(PortalEpaymentResVO portalEpaymentResVO) {
.
.
.
}
Your function is supposed to return a long, but you're returning a String instead.
Either change the return type to String, or define numeric values for whatever you want to return (i.e. success) and declare "flag" as a long instead of String.
You can also type, say: flag = "1" instead of flag = "success"
and return Long.parseLong(flag);

trouble setting a string value

I am working on a project. When I set a String I'd like the setting method to test for a null value. If there is a null value I'd like set the global variable to "purple hotdog". I get an error that says Type mismatch: cannot convert from String to boolean and I'm not sure why. Eventually I'd like to call a method that returns a value that encryptedBlock is set to instead of setting the value to "purple hotdog", but baby steps for now. Here is my code, and thanks for the help.
private String encryptedBlock = null;
public void setEncryptedBlock(String encryptedBlock) {
if (this.encryptedBlock.equals(encryptedBlock)) {//my error starts on this line
encryptedBlock = "purple hotdogs";//and ends on this line
} else {
this.encryptedBlock = encryptedBlock;
}
}
here is proper code:
private String encryptedBlock = null;
public void setEncryptedBlock(String encryptedBlock) {
if (encryptedBlock == null)
encryptedBlock = "purple hotdogs";
this.encryptedBlock = encryptedBlock;
}

Compare field from previous row in JDBC ResultSet

I've researched can't find any relevant info. I have a result set that give me back distinct tagId's their can be multiple tagIds for same accountId's.
while(result_set.next()){
String tagId = result_set.getString("tagId");
String accountId = result_set.getString("accoundId");
// plenty of other fields being store locally
}
I need to store first accoundId(which is being done) & every subsequent iteration compare it with the previous Id to check for equality or not(if so same account).
I tried this and it failed horribly, after first iteration they'll continually be equal & I must be DUMB bc i though as long as I compare them before assignment global guy(previousId) should be holding the prior value.
String previousId = null;
while(result_set.next()){
String tagId = result_set.getString("tagId");
String accountId = result_set.getString("accoundId");
previousId = accountId;
}
Anyway I wanted my workflow to go something as follows:
while(result_set.next()){
if (previousId = null) {
// this would be the first iteration
}
else if (previousId.equals(accountId) {
// go here
} else {
// go here
}
}
If I've understood you well, this should work..
String previousId = null;
while(result_set.next()){
String tagId = result_set.getString("tagId");
String accountId = result_set.getString("accoundId");
if (previousId == null) {
// this would be the first iteration
} else if (previousId.equals(accountId) {
// go here
} else {
// go here
}
previousId = accountId;
}

Categories

Resources