Quickblox requestBuilder [or] - java

requestBuilder.or("userPhone",myPhone,phoneList);
}
}
QBCustomObjects.getObjects("image", requestBuilder, new QBCallbackImpl() {
"phoneList" is arrayList of Strings.
now on my device this code work well but on samsung devices i have crash :
"java.lang.IllegalArgumentException: Illegal character in query at index 147: https://api.quickblox.com/data/image.."
now i know for sure that the arrayList making the problem, because if i put instead of phoneList just , "00000", "09878889" - it's work fine.
what to do?
thanks..
Edit:
ArrayList<String> al = new ArrayList<String>();
HashSet<String> hs = new HashSet<String>();
hs.addAll(phoneList);
al.clear();
al.addAll(hs);
String[]arrString = new String [al.size()+1];
for (int j = 0; j < al.size(); j++) {
String str = al.get(j).toString();
arrString[j+1]= str;
}
arrString[0]= myPhone;
requestBuilder.or("userPhone",arrString);
this is my solution, but i discovered that if "arrString" is bigger than 600+ it's doesn't work, why is that?

Let me explain how OR operator works:
1) for example, you have name field
To get all records with name Alex OR Garry use next query:
requestBuilder.or("name", "Alex", "Garry");
2) for example, you have name field and age field
To get all records with name Alex OR age 22 use next query:
requestBuilder.or("name", "Alex");
requestBuilder.or("age", "22");
Try somtehing like this

Related

how to convert list<objct> into string []

Is it possible to convert this type List<Jadval> into String[] wordList?
I read the words from database with like this :
public static List<Jadval> jadvalList = new ArrayList<Jadval>();
JadvalDB jadvalDB = new JadvalDB(GameActivity.this);
jadvalList = jadvalDB.getWords(myPos + 1);
and now i want to put jadvalList values into String[] wordList.
i use this code to set the values :
for (int i = 0; i < jadvalList.size(); i++) {
wordList[i] = (jadvalList.get(i).toString());
}
but I get the error that wordList is empty .
any idea?
You can use streams and complete it in one line like this:
String[] wordList = jadvalList.stream().map(a->a.toString()).toArray(String[]::new);

How to append data in object and display its content on one page in MongoDB JAVA?

I want to insert this type of model in like :
{
_id: POST_ID
title: TITLE_OF_POST,
by: POST_BY,
questions: [
{
QID:1,
Question:"text"
},
{
QID:1,
Question:"text"
}
]
}
I want to model the above situation using
Document document=new Document("topic",topic)
.append("empid",empid)
.append("teacher", teacher)
.append("date",d)
.append("questions",[
for (int i = 0; i < questions.length; i++) {//This is not correct.
String string = questions[i];}]);
I want to insert questions in object name questions, then display them on one page using for loop on qid then displaying all the questions on one page using servlet out.println. I want to select document then iterate over qid and display all questions.
example:
Title of Assignment \n
Teacher Name \n
Question 1: Content of question 1. \n
Question 2: Content of Question 2.
for creating Document instance you are using absolutely wrong Java syntax.
in Java you can't use something like:
.append("questions",[ for (int i = 0; i < questions.length; i++)
you should create firstly list of questions, and after that use this list while preparing Document instance:
List<Map<String, Object>> questions = new ArrayList<>();
questions.add(new HashMap<String, Object>(){{
put("QID", 1);
put("Question", "text");
}});
questions.add(new HashMap<String, Object>(){{
put("QID", 2);
put("Question", "text");
}});
Document document = new Document("_id", 1001)
.append("topic", "topic")
.append("empid", 5)
.append("teacher", "teacher")
.append("date", 555)
.append("questions", questions);
collection.insertOne(document);
for retrieving previously inserted item from collection you could use the following:
Document foundDocument = collection.find(new Document("_id", 1001)).first();
List<Map> foundQuestions = (List) foundDocument.get("questions");
for (Map foundQuestion: foundQuestions) {
Integer qid = (Integer) foundQuestion.get("QID");
String questionValue = foundQuestion.get("Question").toString();
System.out.println(qid + " : " + questionValue);
}

How to select random text value from specific row using java

I have three input fields.
First Name
Last item
Date Of Birth
I would like to get random data for each input from a property file.
This is how the property file looks. Field name and = should be ignored.
- First Name= Robert, Brian, Shawn, Bay, John, Paul
- Last Name= Jerry, Adam ,Lu , Eric
- Date of Birth= 01/12/12,12/10/12,1/2/17
Example: For First Name: File should randomly select one name from the following names
Robert, Brian, Shawn, Bay, John, Paul
Also I need to ignore anything before =
FileInputStream objfile = new FileInputStream(System.getProperty("user.dir "+path);
in = new BufferedReader(new InputStreamReader(objfile ));
String line = in.readLine();
while (line != null && !line.trim().isEmpty()) {
String eachRecord[]=line.trim().split(",");
Random rand = new Random();
//I need to pick first name randomly from the file from row 1.
send(firstName,(eachRecord[0]));
If you know that you're always going to have just those 3 lines in your property file I would get put each into a map with an index as the key then randomly generate a key in the range of the map.
// your code here to read the file in
HashMap<String, String> firstNameMap = new HashMap<String, String>();
HashMap<String, String> lastNameMap = new HashMap<String, String>();
HashMap<String, String> dobMap = new HashMap<String, String>();
String line;
while (line = in.readLine() != null) {
String[] parts = line.split("=");
if(parts[0].equals("First Name")) {
String[] values = lineParts[1].split(",");
for (int i = 0; i < values.length; ++i) {
firstNameMap.put(i, values[i]);
}
}
else if(parts[0].equals("Last Name")) {
// do the same as FN but for lastnamemap
}
else if(parts[0].equals("Date of Birth") {
// do the same as FN but for dobmap
}
}
// Now you can use the length of the map and a random number to get a value
// first name for instance:
int randomNum = ThreadLocalRandom.current().nextInt(0, firstNameMap.size(0 + 1);
System.out.println("First Name: " + firstNameMap.get(randomNum));
// and you would do the same for the other fields
The code can easily be refactored with some helper methods to make it cleaner, we'll leave that as a HW assignment :)
This way you have a cache of all your values that you can call at anytime and get a random value. I realize this isn't the most optimum solution having nested loops and 3 different maps but if your input file only contains 3 lines and you're not expecting to have millions of inputs it should be just fine.
Haven't programmed stuff like this in a long time.
Feel free to test it, and let me know if it works.
The result of this code should be a HashMap object called values
You can then get the specific fields you want from it, using get(field_name)
For example - values.get("First Name"). Make sure to use to correct case, because "first name" won't work.
If you want it all to be lower case, you can just add .toLowerCase() at the end of the line that puts the field and value into the HashMap
import java.lang.Math;
import java.util.HashMap;
public class Test
{
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
// set the value of "in" here, so you actually read from it
HashMap<String, String> values = new HashMap<String, String>();
String line;
while (((line = in.readLine()) != null) && !line.trim().isEmpty()) {
if(!line.contains("=")) {
continue;
}
String[] lineParts = line.split("=");
String[] eachRecord = lineParts[1].split(",");
System.out.println("adding value of field type = " + lineParts[0].trim());
// now add the mapping to the values HashMap - values[field_name] = random_field_value
values.put(lineParts[0].trim(), eachRecord[(int) (Math.random() * eachRecord.length)].trim());
}
System.out.println("First Name = " + values.get("First Name"));
System.out.println("Last Name = " + values.get("Last Name"));
System.out.println("Date of Birth = " + values.get("Date of Birth"));
}
}

Remove words in a string array until it becomes empty

I have "help","me","please" in a String array and I want to remove "help" from the array, i.e. after returning it as a string, remove "me" after returning it as a string, then remove the last one too, thereby making the string array empty.
I used this
public String showCurrentString(){
EditText ed = (EditText)findViewById(R.id.ed);
String edText = ed.getText().toString();
String nn =edText;
String[] blure = nn.split(" ");
int Index = 1;
for(String check : blure){
if(Arrays.asList(blure).contains(edText)){
PlaySound(StringName() + ".mp3");
Toast.makeText(getApplicationContext(),check,Toast.LENGTH_LONG).show();
But I don't know how to delete each word after toasting it.
So my main question is on how to delete a words that have been parsed or (that I have already used) from a string array.
I think you could delete the first word by doing :
nn = nn.substring(nn.indexOf(' '), nn.length);
(You can do it multiple time, just check that nn.trim().length != 0)
Suppose you have following values:
edText = "Help Me Please"
blure[0] = "Help"
blure[1] = "Me"
blure[2] = "Please"
Then this code should work fine for you:
public String showCurrentString(){
EditText ed = (EditText)findViewById(R.id.ed);
String edText = ed.getText().toString();
String nn =edText;
List<String> blure = new LinkedList<String>(Arrays.asList(nn.split("
")));
ListIterator<String> iter = blure.listIterator();
while(iter.hasNext())
{
String itemToBeRemoved = iter.next();
iter.remove();
System.out.println("Removed "+itemToBeRemoved+"!");
}
}

String cannot be added to List using Object in Java

I am working on a JSF based Web Application where I read contents from a file(dumpfile) and then parse it using a logic and keep adding it to a list using an object and also set a string using the object. But I keep getting this error. I am confused where I am wrong. I am a beginner so can anyone be kind enough to help me?
List<DumpController> FinalDumpNotes;
public List<DumpController> initializeDumpNotes()
throws SocketException, IOException {
PostProcessedDump postProcessedDump = (PostProcessedDump) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("postProcessedDump");
List<DumpController> FinalNotes = new ArrayList<>();
if (postProcessedDump.getDumpNotes() == null) {
dumpNotes = new DumpNotes();
}
DumpListController dlcon = (DumpListController) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("dumpListController");
DumpInfo dumpinfo = dlcon.getSelectedDumpInfo();
String fileName = dumpinfo.getDate() + dumpinfo.getTime() + dumpinfo.getSeqNo() + dumpinfo.getType() + dumpinfo.getTape() + dumpinfo.getDescription() + ".txt";
if (checkFileExistsInWin(fileName)) {
postProcessedDump.setDumpnotescontent(getFileContentsFromWin(fileName));
String consolidateDumpnotes = getFileContentsFromWin(fileName);
String lines[];
String content = "";
lines = consolidateDumpnotes.split("\\r?\\n");
List<String> finallines = new ArrayList<>();
int k = 0;
for (int i = 0; i < lines.length; i++) {
if (!lines[i].equalsIgnoreCase("")) {
finallines.add(lines[i]);
k++;
}
}
for (int j = 0; j < finallines.size(); j++) {
if (finallines.get(j).startsWith("---------------------SAVED BY")) {
PostProcessedDump dump = new PostProcessedDump();
dump.setDumpMessage(content);
content = "";
FinalDumpNotes.add(dump);
} else {
content = content + finallines.get(j);
}
}
}
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("postProcessedDump", postProcessedDump);
return FinalDumpNotes;
}
I get the following error:
If you want to add instances of type PostProcessedDump to your List you should change it's type. Also, don't forget to initialize it. Something like,
List<PostProcessedDump> FinalDumpNotes = new ArrayList<>();
Also, Java naming convention is to start variable names with a lower case letter. FinalDumpNotes looks like a class, I would suggest something like
List<PostProcessedDump> processedList = new ArrayList<>();
Problems with your code:
List<DumpController> FinalDumpNotes;
You declare FinalDumpNotes to be a List of DumpController objects, but you never initialize it. In addition, your IDE is barfing on the following line of code:
FinalDumpNotes.add(dump);
because you are attempting to add a PostProcessedDump object to the List instead of a DumpController object.
For starters, you need to initialize your list like this:
List<DumpController> finalDumpNotes = new ArrayList<DumpController>();
Notice that I have made the variable name beginning with lower case, which is the convention (upper case is normally reserved for classes and interfaces).
I will leave it to you as a homework assignment to sort out the correct usage of this List.

Categories