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);
Related
I have the following code:
BufferedReader metaRead = new BufferedReader(new FileReader(metaFile));
String metaLine = "";
String [] metaData = new String [100000];
while ((metaLine = metaRead.readLine()) != null){
metaData = metaLine.split(",");
for (int i = 0; i < metaData.length; i++)
System.out.println(metaData[0]);
}
This is what's in the file:
testTable2 Name java.lang.Integer TRUE test
testTable2 age java.lang.String FALSE test
testTable2 ID java.lang.Integer FALSE test
I want the array to have at metaData[0] testTable2, metaData[1] would be Name, but when I run it at 0 I get testtable2testtable2testtable2, and at 1 I'd get NameageID and OutOfBoundsException.
Any ideas what to do in order to get the result I want?
Just print metaData[i] instead of metaData[0] and split each string by "[ ]+" (that means "1 or more spaces"):
metaData = metaLine.split("[ ]+");
As a result, you will get the following arrays:
[testTable2, Name, java.lang.Integer, TRUE, test]
[testTable2, age, java.lang.String, FALSE, test]
[testTable2, ID, java.lang.Integer, FALSE, test]
The code snippet to the preceding output results:
while ((metaLine = metaRead.readLine()) != null) {
metaData = metaLine.split("[ ]+");
for (int i = 0; i < metaData.length; i++)
System.out.print(metaData[i] + " ");
System.out.println();
}
Also, I've written your task by using Java 8 and Stream API:
List<String> collect = metaRead
.lines()
.flatMap(line -> Arrays.stream(line.split("[ ]+")))
.collect(Collectors.toList());
And, finally, there is the most straight-forward way:
final int LINES, WORDS;
String[] metaData = new String[LINES = 5 * (WORDS = 3)]; // I don't like it
int i = 0;
while ((metaLine = metaRead.readLine()) != null) {
for (String s : metaLine.split("[ ]+")) metaData[i++] = s;
}
Correct your code following line inside the for loop,
System.out.println(metaData[0]);
As
System.out.println(metaData[i]);
Although my answer may not fit completely with your question. But as i can see, your file format is TSV or CSV.
May be you should consider using OpenCSV
for your problem.
The library will handle reading, splitting process for you.
I have a list
List<List> rows = (List<List>) responseMap.get("data");
[[FRPP, PE103, , USD], [FRPP, PE313AHMR, , USD]
And I want to set the data to the fields of a bean (which represents all the fields for each line)
ArrayList data = new ArrayList();
for (int i = 0; i < 10; i++) {
Bean line = new Bean();
line.setField1("element1");
line.setField2("element2");
line.setField3("element3");
line.setField4("element4");
data.add(line);
}
How can I do that?
Using JDK 1.6, Windows
You are getting list in the format of [[FRPP, PE103, , USD], [FRPP, PE313AHMR, , USD].
Use ArrayList.get(int) to get value of each index value.
for (int i = 0; i < rows.size(); i++) {
Bean line = new Bean();
ArrayList al=(ArrayList)rows.get(i);//now [FRPP, PE103, , USD]
line.setField1((String)al.get(0));//FRPP
line.setField2((String)al.get(1));//PE103
line.setField3((String)al.get(2));//
line.setField4((String)al.get(3));//USD
data.add(line);
}
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.
This is my jsoup parser to extract soap content
doc = Jsoup.parse(getxml,"", Parser.xmlParser());
Elements taux = doc.select("taux");
Elements devise = doc.select("devise");
Elements datecours = doc.select("dateCours");
Elements libelle = doc.select("libelle");
Elements quotite = doc.select("quotite");
Elements fixing = doc.select("fixing");
Maybe this will help you. After getting the elements from the web service, assuming that all of the strings contain a large string with separated values, do the following:
String[] deviseSeparated = devise.split(" ");
String[] datecourSeparated = datecour.split(" ");
String[] libelSeparated = libel.split(" ");
String[] quotSeparated = quot.split(" ");
String[] fixSeparated = fix.split(" ");
and after that, assuming that all of the arrays are the same size, just execute this for loop, to initiate the objects:
for (int i = 0; i < deviseSeparated.length; i++) {
PostList.add(new convertor_pst(deviseSeparated[i],datecourSeparated[i],libelSeparated[i],quotSeparated[i],fixSeparated[i]));
}
Is this what you are looking for?
I am parsing an File , sometimes i get a small line or big line .
Is it possible to append Line with extra tokens incase if the line is small ??
This is my program
private static String[] LISTOFFIELDS = null;
String fields = "A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z";
String line = "OIUU||HHH|INBVC INB|PP|NN|OO|PPPPP||";
String[] totaltokens = line.split("\\|", LISTOFFIELDS.length);
private static HashMap<String, Object> ParsedValues(String[] totaltokens) {
HashMap<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < LISTOFFIELDS.length; i++) {
String fieldName = LISTOFFIELDS[i];
map.put(fieldName, totaltokens[i]);
}
}
As you can see if the line is small i will get
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
Here i can avoid ArrayIndexOutOfBoundsException if i reduce the String fields to String fields = "A|B|C|D|E|F|G|H|";
Is it possible to avoid the Exception , without doing any modifications to the String fields ??
Check the length of the string before going to split and getting its substring.
Just reduce the number of iterations in your loop:
int size = Math.min(LISTOFFIELDS.length, totaltokens.length);
for (int i = 0; i < size; i++) {
String fieldName = LISTOFFIELDS[i];
map.put(fieldName, totaltokens[i]);
}