I used an array the whole time for my app. But i only set the values after the app was created, with:
public String[][] stunde = new String [6][13];
public String[][] lehrer = new String [6][13];
stunde[1][1]= "SZ";
stunde[2][1]= "Bi";
stunde[3][1]= "";
stunde[4][1]= "DG2";
stunde[5][1]= "";
lehrer[1][1]= "Gt";
lehrer[2][1]= "Pön";
lehrer[3][1]= "";
lehrer[4][1]= "Lc";
lehrer[5][1]= "";
but now i wanted to set these values before, so that i could use them in another method.
Like this:
public String[][] stunde = {
{"SZ", "SZ", "Ku", "Ku", "M", "M", "GeL1", "EL2"},
{"Bi", "Bi", "EL2", "EL2", "Pl", "Pl","DG2","If"},
{"", "", "EL2","EL2", "","","M","Bi"},
{"DG2", "DG2", "","", "GeL1","GeL1","Pl","Ku"},
{"", "", "GeL1","GeL1", "If","If","","SZ","","","Sp","Sp"}
};
But after i tried it like in the second code my app started to crash after i opened it.
Any ideas why?
Your old code used indexes on the top-level array starting at one, not zero. It looks like the rest of your app relies on that numbering as well.
Add a "fake" row and column to fix the problem:
public String[][] stunde = {
/*0*/ {"", "", "", "", "", "", "", "", ""},
/*1*/ {"", "SZ", "SZ", "Ku", "Ku", "M", "M", "GeL1", "EL2"},
/*2*/ {"", "Bi", "Bi", "EL2", "EL2", "Pl", "Pl","DG2","If"},
/*3*/ {"", "", "", "EL2","EL2", "","","M","Bi"},
/*4*/ {"", "DG2", "DG2", "","", "GeL1","GeL1","Pl","Ku"},
/*5*/ {"", "", "", "GeL1","GeL1", "If","If","","SZ","","","Sp","Sp"}
};
Now your row 0 remains unused, and the rest of your app that wants rows 1 through 5 would find the data where it used to be before. Same goes for column numbering.
Note: Once you get this under control and the app no longer crashes, a long-term approach to this would be changing the code that uses the arrays to index 0..4 instead of 1..5.
Related
I'm trying to add multiple records in a List of List of Objects and I don't know how to do it (java).
The only way I've found it possible is by doing it literally:
List<List<Object>> values = Arrays.asList(
Arrays.asList("84935", "01/02/2020", "01/02/2020", "resolved", "XXX", "XXX", "XXX", "XXX", "1", "1", "15", "0", "2020"),
Arrays.asList("84936", "02/02/2020", "02/02/2020", "resolved", "XXX", "XXX", "XXX", "XXX", "1", "1", "15", "0", "2020"),
Arrays.asList("84937", "03/02/2020", "03/02/2020", "resolved", "XXX", "XXX", "XXX", "XXX", "1", "1", "15", "0", "2020")
);
I want to read a .csv file and write in it that list. When I try with .add or set I get a nullpointer Exception and I haven't been able to find a solution.
Is it possible to add the records automatically with a loop? It is mandatory that the List is List<List<Object>> because it's the only way that Google Sheets Api allows.
Thank you.
Yes it's absolutely possible to write a list of lists inside a loop:
For example:
List<List<String>> list = new ArrayList<>();
for (String line: lines) {
list.add(Arrays.asList(line.split(","));
}
If you're reading a CSV file you'd be better off using a third party library such as Apache CSVParser.
However if you insist on doing it yourself:
List<List<String>> fields = Files.lines(fileName)
.map(l -> Arrays.asList(l.split(",")))
.collect(Collector.toList());
How can i remove the middle border of jtable.
From this ..
To This ..
here is my code
Object rowData[][] = { { "", "", "", "", "", "", "" }, //4 empty row
{ "", "", "", "", "", "", "" },
{ "", "", "", "", "", "", "" },
{ "", "", "", "", "", "", "" }
};
Object columnNames[] = { "File Type", "Total File", "Size(GB)", " ",
"File Type", "Total File", "Size(GB)" };
JTable table = new JTable(rowData, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
one way t do the smilier thing is to draw 2 tables with spacing between them.
Note there will be a some of work to keep 2 tables in sync as one in case of sorting and deleting.
Lets say I have this list of words:
String[] stopWords = new String[]{"i","a","and","about","an","are","as","at","be","by","com","for","from","how","in","is","it","not","of","on","or","that","the","this","to","was","what","when","where","who","will","with","the","www"};
Than I have text
String text = "I would like to do a nice novel about nature AND people"
Is there method that matches the stopWords and removes them while ignoring case; like this somewhere out there?:
String noStopWordsText = remove(text, stopWords);
Result:
" would like do nice novel nature people"
If you know about regex that wold work great but I would really prefer something like commons solution that is bit more performance oriented.
BTW, right now I'm using this commons method which is lacking proper insensitive case handling:
private static final String[] stopWords = new String[]{"i", "a", "and", "about", "an", "are", "as", "at", "be", "by", "com", "for", "from", "how", "in", "is", "it", "not", "of", "on", "or", "that", "the", "this", "to", "was", "what", "when", "where", "who", "will", "with", "the", "www", "I", "A", "AND", "ABOUT", "AN", "ARE", "AS", "AT", "BE", "BY", "COM", "FOR", "FROM", "HOW", "IN", "IS", "IT", "NOT", "OF", "ON", "OR", "THAT", "THE", "THIS", "TO", "WAS", "WHAT", "WHEN", "WHERE", "WHO", "WILL", "WITH", "THE", "WWW"};
private static final String[] blanksForStopWords = new String[]{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};
noStopWordsText = StringUtils.replaceEach(text, stopWords, blanksForStopWords);
Create a regular expression with your stop words, make it case insensitive, and then use the matcher's replaceAll method to replace all matches with an empty string
import java.util.regex.*;
Pattern stopWords = Pattern.compile("\\b(?:i|a|and|about|an|are|...)\\b\\s*", Pattern.CASE_INSENSITIVE);
Matcher matcher = stopWords.matcher("I would like to do a nice novel about nature AND people");
String clean = matcher.replaceAll("");
the ... in the pattern is just me being lazy, continue the list of stop words.
Another method is to loop over all the stop words and use String's replaceAll method. The problem with that approach is that replaceAll will compile a new regular expression for each call, so it's not very efficient to use in loops. Also, you can't pass the flag that makes the regular expression case insensitive when you use String's replaceAll.
Edit: I added \b around the pattern to make it match whole words only. I also added \s* to make it glob up any spaces after, that's maybe not necessary.
You can make a reg expression to match all the stop words [for example a , note space here]and end up with
str.replaceAll(regexpression,"");
OR
String[] stopWords = new String[]{" i ", " a ", " and ", " about ", " an ", " are ", " as ", " at ", " be ", " by ", " com ", " for ", " from ", " how ", " in ", " is ", " it ", " not ", " of ", " on ", " or ", " that ", " the ", " this ", " to ", " was ", " what ", " when ", " where ", " who ", " will ", " with ", " the ", " www "};
String text = " I would like to do a nice novel about nature AND people ";
for (String stopword : stopWords) {
text = text.replaceAll("(?i)"+stopword, " ");
}
System.out.println(text);
output:
would like do nice novel nature people
IdeOneDemo
There might be better way.
This is a solution that does not use regular expressions. I think it's inferior to my other answer because it is much longer and less clear, but if performance is really, really important then this is O(n) where n is the length of the text.
Set<String> stopWords = new HashSet<String>();
stopWords.add("a");
stopWords.add("and");
// and so on ...
String sampleText = "I would like to do a nice novel about nature AND people";
StringBuffer clean = new StringBuffer();
int index = 0;
while (index < sampleText.length) {
// the only word delimiter supported is space, if you want other
// delimiters you have to do a series of indexOf calls and see which
// one gives the smallest index, or use regex
int nextIndex = sampleText.indexOf(" ", index);
if (nextIndex == -1) {
nextIndex = sampleText.length - 1;
}
String word = sampleText.substring(index, nextIndex);
if (!stopWords.contains(word.toLowerCase())) {
clean.append(word);
if (nextIndex < sampleText.length) {
// this adds the word delimiter, e.g. the following space
clean.append(sampleText.substring(nextIndex, nextIndex + 1));
}
}
index = nextIndex + 1;
}
System.out.println("Stop words removed: " + clean.toString());
Split text on whilespace. Then loop through the array and keep appending to a StringBuilder only if it is not one of the stop words.
I'm trying to create a multi-dimensional array in Java and I have it set up correctly however at the end it is saying '{' expected when there's already one there. This is the error line within the code
{
"Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59","50.05"
};
Any suggestions on a way to fix this problem?
Edit:
Before this line is the rest of the array and this coding:
import javax.swing.JOptionPane;
public class CMS_Program
{
public CMS_Program()
{
String[][] names = new String[][]
{
{ Array here
All { are closed off too at the end.
Lot of context is still missing from your question. Anyway, the direct initialization of a String[][] ought basically to be done as follows:
String[][] names = new String[][] {
{ "Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59", "50.05" },
{ "Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59", "50.05" },
{ "Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59", "50.05" },
{ "Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59", "50.05" },
{ "Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59", "50.05" }
};
However, you're better off using a List<Person> where the Person class look like this.
public class Person {
private String name;
private String id; // ??
private Gender gender;
private String city; // ???
private Double time; // Or so?
// ...
// Add/generate c'tor/getter/setter/equals/hashcode and other boilerplate.
}
This way you can just end up with
List<Person> persons = new ArrayList<Person>();
persons.add(new Person("Gerald Field", "U18", Gender.MALE, "Bourges", 14.01, 26.59, 50.05));
// ...
Just work with real objects/entities and don't fiddle low-level with complex arrays. Your code will become more self-documented and better maintainable.
Try this:
String[][] twoDimensional = {{"00", "01"}, {"10", "11"}};
It looks like you are doing this:
String[][] names = new String[][]
{
{
"Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59","50.05"
};
Note that there is a missing closing '}'
If the closing brace is not missing then the semicolon needs to be after the second closing brace and not the first.
This should work out fine.
String[][] names = new String[][]
{
{"ramalam", "wam wam"},
{"ramalam", "wam wam"}
};
Could it be that you had a semi-colon after the array?
This is valid:
String[][] names = new String[][]
{
{
"Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59","50.05"
}
};
I can't see how this differs from your source though...
{ and } are the beginning and end symbols of an array, and , is used to delimit the elements in the array..
If you create a multidimensional array (basically an array of array you need to use {..} for the array that is declared, as well as for any element within, because those are arrays too.
So, use something like this:
String[][] myMultiDimensionalArray = new String[][]
{
{
"Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59","50.05"
},
{
"Name Lastname", "U18", "Male", "Bourges", "14.01", "26.59","50.05"
}
}
What the error is trying to say is that it sees only one dimension, and it was let to believe that there will be two.
My data has been placed into an array, but unfortunately not in the order I want it in...
String[][] databaseToArray = {
//{"Name", "Channel", "Description", "Amount", "isReady"},
{"John", "Nick", "likes", "2", "yes" },
{"Drew", "MTV", "dislikes", "4", "no" },
{"Fred", "CNN", "okay", "3", "no" },
{"Beth", "Fox", "valid", "1", "yes" }
};
How do I manipulate this array so that when I loop through it the order is by the amount , similar to SELECT * FROM "databaseToArray" ORDER BY "Amount" aka
String[][] reorganizedArray = {
//{"Name", "Channel", "Description", "Amount", "isReady"},
{"Beth", "Fox", "valid", "1", "yes" },
{"John", "Nick", "likes", "2", "yes" },
{"Fred", "CNN", "okay", "3", "no" },
{"Drew", "MTV", "dislikes", "4", "no" }
};
You'll want to pass a Comparator to the Arrays.sort method. I haven't done Java in 5 years, but it should be easily doable. Maybe someone can clean up this example I'm about to write, since I'm pretty certain I'll get something wrong.
String[][] databaseToArray = {
//{"Name", "Channel", "Description", "Amount", "isReady"},
{"John", "Nick", "likes", "2", "yes" },
{"Drew", "MTV", "dislikes", "4", "no" },
{"Fred", "CNN", "okay", "3", "no" },
{"Beth", "Fox", "valid", "1", "yes" }
};
Arrays.sort(databaseToArray, new Comparator<String[]>() {
public int compare(String[] a, String[] b) {
return a[3].compareTo(b[3]);
}
});
I found the sort method on the processing website, but this is only for One-Dimensional Arrays...
http://processing.org/reference/sort_.html
float[] a = { 3, 5, 2, 4, 1 };
a = sort(a);
println(a);
// Prints 1, 2, 3, 4, 5