Cant join 2 strings in javafx - java

Hey am trying to make a javafx program that join two strings but i am getting error when trying to join them with join command. My javafx code is
String name = "John";
String lastname= "doe";
String fullname = name.join(lastname);
I don't know this is correct or it's my foolishness to use this as i am beginner to javafx. I hope you can resolve my problem. Thanks in advance

String name = "John";
String lastname= "doe";
String fullname = String.join(" ", name, lastname);
As in the documentation:
public static String join(CharSequence delimiter, CharSequence... elements)
But you could simply use concatenation:
String fullname = name + " " + lastname;

Related

Convert array of string or integers to comma separated integer or string for sql in condition

public List<String> getCategory(final String logicalUnitCode, final String logicalUnitIdent, final String keyword) {
return entityManager
.createNativeQuery(
"select name from " + logicalUnitCode + " where " + keyword + "::text in(" + logicalUnitIdent + "::text)")
.getResultList();
}
In some case keyword is int8 and logicalUnitIdent is String of integers for example: ("1,2,3,4,5") , and in some cases keyword is varchar and logicalUnitIdent is string (("test,test1,test2"). I want to use one query for both of this cases. Any suggestion how can i achive that and is it possible? To cast both to text or varchar? Im using postgres
before calling getCategory() you create logicalUnitIdent String and pass. But modify the query to "in("+logicalUnitIdent+")"
logicalUnitIdent should be "1,2,3" or "'string1','string2','string3'
To convert List of string to single quoted and comma separated string use the below code
In JDK 8 or above use this
List<String> stringList = new ArrayList<String>(Arrays.asList("string1","string2","string3"));
String list= String.join(",", stringList
.stream()
.map(name -> ("'" + name + "'"))
.collect(Collectors.toList()));
System.out.println(list); // prints 'string1','string2','string3'

Creating a class with fields java

I'm trying to create a class that represents a person's name and that has fields representing the first name, middle initial, and last name, (also several methods).
When I try to run my code, I receive a Illegal modifier for the local class Name; only abstract or final is permitted error and cannot find a way to resolve this. Please help me resolve any errors so that my code will perform the desired (and commented) tasks. Thank you in advance.
public class Name {
String first_name;
String middle_initial;
String last_name;
//constructor that accepts first name, middle initial, and last name as parameters and initializes the Name object's state with those values
public Name(String first_name, String middle_initial, String last_name) {
first_name = "John";
middle_initial = "Q.";
last_name = "Public";
}
//method to return the person's name in first name, middle initial, and last name order.
public String getNormalOrder() {
String name = first_name + " " + middle_initial + " " + last_name;
return name;
}
//method to return the person's name in reversed order: last name, first name, and middle name.
public String getReverseOrder() {
String name = last_name + ", " + first_name + " " + middle_initial;
return name;
}
//method to return the full name in first, middle, last order, in String form
public String toString() {
return first_name + " " + middle_initial + " " + last_name;
}
public static void main(String[] arg){
first_name = "John";
middle_initial = "Q.";
last_name = "Public";
getNormalOrder();
getReverseOrder();
toString();
}
}
I suggest you start over with a file Name.java.
From the top, you can only have one public class of the same name as the file it is contained within.
public class Name {
}
Now, your field definitions are fine.
The constructor, however, should not hard-code the values of "John Q. Public", as this Name class can represent any instance of Name.
Therefore, you need to assign the fields using this, which is needed to "qualify" which variable you are using since they are the same variable names. (If you renamed String first_name here, to String fName, for example, then first_name = fName would be possible).
public Name(String first_name, String middle_initial, String last_name) {
this.first_name = first_name;
this.middle_initial = middle_initial;
this.last_name = last_name;
}
Then, in the main method, you want to make an instance of this object.
Name name = new Name("John", "Q.", "Public");
That's it.
But if you want to display those values, simply calling your methods isn't correct, as it needs to know which Name value to call the methods on. You also need to print the String values that are returned to even see them.
System.out.println(name.getNormalOrder());
System.out.println(name); // calls name.toString()

How to make select query with StringTokenizer?

I'm trying to make a query with selected fields for final user from view (JSP) to controller, but I don't know how.
For example, I have this parameters from view (JSP)
IDUSER,>,2,OR,USERNAME,=,'KURT'
So, I'll want to have something like this,
SELECT IDUSER, USERNAME FROM TABLE_NAME WHERE IDUSER > 2 OR USERNAME = 'KURT'
but I have next result
SELECT null FROM TABLE_NAME WHERE IDUSER > 2 OR USERNAME = 'KURT'
I'm parsing string with StringTokenizer class, where query is: String query = request.getParameter("data"); and data is IDUSER,>,2,OR,USERNAME,=,'KURT'.
StringTokenizer field = new StringTokenizer(query, ",");
nFields = field.countTokens();
System.out.println("nFields: " + nFields);
String[] fields = new String[nFields];
for(int i = 0; i < fields.length; i++) {
while(field.hasMoreTokens()) {
fields[i] = field.nextToken();
}
System.out.println("fields[i]: " + fields[i]);
myQuery = "SELECT " + fields[i] + " FROM "+tableName+ " WHERE ";
System.out.println("myQuery 1: " + myQuery);
}
StringTokenizer token= new StringTokenizer(query, "|,");
while(token.hasMoreTokens()) {
myQuery = myQuery + token.nextToken() + " ";
}
System.out.println("QUERY RESOLVED: " + myQuery);
PLEASE HELP ME
Here is the solution after minor tweak in your query (redefined the separators)
public static void main(String[] args) {
// Redefine the separators as single , separators is difficult to process
//You would need to define possible operators like this (#OR# , #AND# ) ,surrounded by special characters to identify.
String query ="IDUSER_>_2#OR#USERNAME_=_'KURT'";
String tableName="TESTTABLE";
String operator=null;
//you can choose operator conditionally
if(query.contains("#OR#")) operator="#OR#";
// if(query.contains("#AND#")) operator="#AND#";
//Used split instead of Tokenizer.
String cols[]= query.split(operator);
String myQuery = "SELECT ";
String select="";
for(String col:cols){
if(!select.isEmpty()){
select+=" , ";
}
// Only the first element is retrieved (for select)
select+=col.split("_")[0];
}
myQuery+=select+" FROM "+tableName+ " WHERE ";
// Removes all special charecters (like, # and _ with white space)
String subQuery = query.replaceAll("#", " ");
subQuery=subQuery.replaceAll("_", "");
myQuery+=subQuery;
System.out.println("QUERY RESOLVED: " + myQuery);
}
Note : ',' is replaced with '_' and operators are surrounded by '#'
Cheers!!
I think the problem is this line in your while loop:
myQuery = "SELECT " + fields[i] + " FROM "+tableName+ " WHERE ";
This will keep changing the value of myQuery as the while loop executes.
Maybe you need to replace this with:
myQuery = "SELECT " + fields[0] + " FROM "+tableName+ " WHERE ";
break;
I am assuming your selection criteria is the first field in the parameter from your view.
Do not see where does IDPERFIL come from. Also, I don't like this:
while(field.hasMoreTokens()) {
fields[i] = field.nextToken();
}
That will iterate tokenizer to the end, and stop at last element. I am sure you don't want this. Fix that, tell where IDPERFIL come from, and then, maybe, you'll understand answer by yourself. Otherwise, I'll try to help further.

deleting a record which selected by user?

I am coding
String Name = txtName.getText();
String Surname = txtSurname.getText();
st.executeUpdate("DELETE from list WHERE Name=#Name and Surname=#Surname");
but it doesn't delete the records. Any problem with the syntax? Please help.
You need to replace #name and #surname with the actual values.
and add ' around your values:
DELETE from list WHERE Name='#Name' and Surname='#Surname'
String Name = txtName.getText().Trim();
String Surname = txtSurname.getText().Trim();
String query = "DELETE from list WHERE Name='" + Name + "' and Surname='" + Surname + "'";
st.executeUpdate(query);
try:
st.executeNonQuery("DELETE from list WHERE Name=?e and Surname=?");
and pass the name and surname as parameters.
Use this
If the table list exists .
This will work .
String Name = txtName.getText();
String Surname = txtSurname.getText();
st.executeUpdate("DELETE from list WHERE Name='"+Name"' and Surname='"+Surname"'");
I fixed it like that:
st.executeUpdate("DELETE from list WHERE Name='"+txtName.getText()+"'" + "and Surname='" + txtSurname.getText()+"'");
Thank you all

String new line (trying to connect java to Oracle DB)

I'm connecting Java to Oracle database, everything's is going pretty okay, except the following problem:
This works okay when I write the whole string within one couple of quotes:
String command =
"SELECT FIRST_NAME, PHONE_NUMBER, SALARY FROM EMPLOYEES WHERE SALARY < 5000";
BUT as the one above doesn't look well-designed code I wanted to split the lines:
String command = new StringBuilder(
"SELECT FIRST_NAME, PHONE_NUMBER, SALARY\n")
.append("FROM EMPLOYEES\n")
.append("WHERE SALARY < 5000;")
.toString();
And meanwhile, I tried plus (+) instead of StringBuilder, but again messed up...
Please help((
String command = new StringBuilder(
"SELECT FIRST_NAME, PHONE_NUMBER, SALARY ")
.append("FROM EMPLOYEES ")
.append("WHERE SALARY < 5000")
.toString();
Using StringBuilder.append() for a constant value is less efficient than writing the constant directly:
String command =
"SELECT FIRST_NAME, " +
" PHONE_NUMBER, \n " +
" SALARY \n " +
"FROM EMPLOYEES \n" +
"WHERE SALARY < 5000";
Salaam,
Why use newline character [\n] which is not understood by sql engines?
It should work without it

Categories

Resources