Hey i have written some kind of Binary Search Tree, which has a insert method.
So it gets a Object to insert, a Char Array and a Integer which gives it the Index to look at.
So this is the insert method :
public void insert(Buchstabe pBuchstabe,char[] pChar,int pStelle)
{
if(pBuchstabe==null)
return;
if(baum.isEmpty())
{
baum=new BinaryTree(pBuchstabe);
}
else
if(pStelle <= pChar.length)
{
if(pChar[pStelle] == '.')
{
Mybaum lTree=this.getLeftTree();
lTree.insert(pBuchstabe,pChar,pStelle+1);
this.baum.setLeftTree(lTree.baum);
}
else
if(pChar[pStelle]=='-')
{
Mybaum lTree=this.getRightTree();
lTree.insert(pBuchstabe,pChar,pStelle+1);
this.baum.setLeftTree(lTree.baum);
}
}
}
I have a Method which passes the required Parameters (in this case) : A Object Buchstabe,then the Char Array['.','.'] and the integer 0 to the insert method.
And i get a out of bounds error :
java.lang.ArrayIndexOutOfBoundsException: 2
at Mybaum.insert(Mybaum.java:22)
at Mybaum.insert(Mybaum.java:25)
at Mybaum.insert(Mybaum.java:25)
at Mörserbaum.einlesen(Mörserbaum.java:42)
Does anyone know what ive made wrong ?
Looks like you have an issue
if(baum.isEmpty())
{
baum=new BinaryTree(pBuchstabe);
}
else
**if(pStelle <= pChar.length)**
{
**if(pChar[pStelle] == '.')**
{
Mybaum lTree=this.getLeftTree();
lTree.insert(pBuchstabe,pChar,pStelle+1);
this.baum.setLeftTree(lTree.baum);
}
if(pChar[pStelle] == '.') -- you get indexOutOfBound bc/ you need to say
if(pChar[pStelle-1] == '.') .. since Java array index starts from 0, if the length is 5, last index would be pChar[4]...
there could be more issues with this code, since we don't have full code/context i can't speculate more.. but this is one of the reason you could get indexoutofbound
public void einlesen()
{
Buchstabeenschlange sch = new Buchstabeenschlange();
for(int i = 0;i<codeTabelle.length;i++)
{
Buchstabe a = new Buchstabe(alphabet[i],codeTabelle[i]);
if(a == null)
{
System.out.println("Buchstabe mit Error == "+a);
}
System.out.println("Buchstabe == "+a);
sch.hinzufuegen(a);
System.out.println("------------");
}
List l = sch.gibListe();
sch.druckeListe();
l.toFirst();
while(l.hasAccess())
{
Buchstabe buch = (Buchstabe) l.getObject();
char[] code = buch.getCode().toCharArray();
baum.insert(buch,code,0);
l.next();
}
TreeViewGUI view = new TreeViewGUI(baum);
}
This creates the object Buchstabe and sorts it in a List so that you have the shortest Strings at the beginning.
Then it inserts them into a Binaray Tree and displays it.
Related
My method applyBonus() from the class BonusEmporium gets bonus objects from the arrayList of City objects and applies them to a Player object.
when i write my function like this i have no problem testing it:
public class BonusEmporium extends Bonus {
public BonusEmporium(int cities) {
this.setBonusType(BonusType.EMPORIUM);
this.cities=cities;
setCity(new ArrayList<City>(cities));
}
public void applyBonus(Player player){
Bonus bonus=getCity().get(0).getBonus().get(0);//gets the first bonus from the first
//city
bonus.applyBonus(player);
Bonus bonus=getCity().get(0).getBonus().get(1);//gets the second bonus from the first
//city
bonus.applyBonus(player);
Bonus bonus=getCity().get(1).getBonus().get(0);//gets the first bonus from the
//second city
bonus.applyBonus(player);
}
}
The problem is when i want to run it only while the arraylists contain elements, how do i check that an element from an array is empty?
public class BonusEmporium extends Bonus {
public BonusEmporium(int cities) {
this.setBonusType(BonusType.EMPORIUM);
this.cities=cities;
setCity(new ArrayList<City>(cities));
}
public void applyBonus(Player player){
int i=0,j=0;
while(j<cities){
while(getCity().get(j).getBonus().get(i)!=null){//In theory this should
//verify that the element bonus i from the city j is not empty
//but i get NullPointerException
Bonus bonus=getCity().get(j).getBonus().get(i);
bonus.applyBonus(player);
i++;
}
j++;
}
}
}
It's difficult to tell what you are asking, but you should be able to avoid the error by handling the message chain better.
public void applyBonus(Player player){
List<City> cities = getCity();
for(int i = 0; cities != null && i < cities.size(); i++){
City c = cities.get(i);
List<Bonus> bonuses = c.getBonus();
for (int j = 0; bonuses != null && j < bonuses.size(); j++) {
Bonus b = bonuses.get(j);
if (b != null)
b.applyBonus(player);
}
}
}
You check if the element is empty like this:
if(array != null && array.get(index) == null) // element is null when empty
To check if the array as a whole is empty (has no elements):
if(array != null && array.isEmpty())
In general the problem in java is chaining getters like this:
getCity().get(j).getBonus().get(i)!=null
You can get nullpointer exception from 4 places here. You can check them all like:
if (getCity()!=null &&
getCity().get(j) &&
getCity().get(j).getBonus() &&
getCity().get(j).getBonus().get(i)!=null){
....
}
Other way could be encapsulating whole code in try - catch and catching NPE, which is more like hack.
I also suggest using foreach instead while and don't store array size in separate variables, because you can always get it using
myArr.getSize()
And also check javadoc about ArrayList constructor:
new ArrayList<City>(cities)
because this is likely not doing what you expect
Short story:
init all ArrayLists in constructor (don't specify size)
use foreach loops
don't put nulls into the arrays
and you should be fine without checking for empty elements
I have a list of type Model_BarcodeDetail which conatins attributes like barcode, area,location,color etc.
when I enter any barcode in the edittext, I want to search that barcode in the list(list can have n number of similar barcodes with similar area and location or different area and location), if the barcode I entered and the similar barcode that is presnt in my list have same area and location then I want to doSomething() else doSomethingElse().
The code I tried is:
private List<String> barcodeList = new ArrayList<String>();
barcode = editText_barcode.getText().toString().trim();
if ((scanned_barcode != null
&& scanned_barcode.equalsIgnoreCase(barcode))) {
if ((!barcodeList.contains(barcode)) ) {
// if barcode I entered does not contains in the list
// It is working fine
barcodeList.add(barcode);//barcodeList contains only barcode
}
else if (barcodeList.contains(barcode) ) {
data = list.get(barcodeList.indexOf(barcode));
// here is the problem
// here I want to get data of the barcode that have similar area and
location
if (data.getArea() == selected_area
&& data.getLocation() == selected_loc) {
doSomething();
} else {
doSomethingElse();
}
}
Search your string in array list and get Object and then check location of barcode, here is sample code:
barcode = editText_barcode.getText().toString().trim();
if ((scanned_barcode != null
&& scanned_barcode.equalsIgnoreCase(barcode))) {
Model_BarcodeDetail model_barcodeDetail=getBarcodeDetails(barcode);
// for handling array do this in loop
if (model_barcodeDetail!=null && model_barcodeDetail.getArea() == selected_area && model_barcodeDetail.getLocation() == selected_loc) {
doSomething();
}else{
doSomethingElse();
}
}
/* your list can contain n number of similar bar code then change return type of this function to Model_BarcodeDetail[] */
private Model_BarcodeDetail getBarcodeDetails(Sttring barcode){
for (Model_BarcodeDetail model_barcodeDetail : list) {
if (barcode.eqauals(model_barcodeDetail.getBarcode)){
return model_barcodeDetail;
}
}
return null;
}
When your list looks something like:
List<Model_BarcodeDetail> list = new ArrayList<Model_BarcodeDetail>()
you can use an foreach loop:
barcode = editText_barcode.getText().toString().trim();
if ((scanned_barcode != null
&& scanned_barcode.equalsIgnoreCase(barcode))) {
if ((!barcodeList.contains(barcode))) {
// if barcode I entered does not contains in the list
// It is working fine
}
for (Model_BarcodeDetail model_barcodeDetail : list) {
if (model_barcodeDetail.getArea() == selected_area && model_barcodeDetail.getLocation() == selected_loc) {
doSomething();
break;
}
}
// Nothing found
doSomethingElse();
}
I am new to java and I want to create a very simple "word completion " program. I will be reading in a dictionary file and recursively adding the words into a Node array (size 26). I believe I have managed to do this successfully but I am not sure how to go through and print the matches. For the sake of testing, I am simply inserting 2 words at the moment by calling the function. Once everything is working, I will add the method to read the file in and remove junk from the word.
For example: If the words "test" and "tester" are inside the tree and the user enters "tes", it should display "test" and "tester".
If somebody could please tell me how to go through and print the matches (if any), I would really appreciate it. Full code is below.
Thank you
What you implemented is called "trie". You might want to look at the existing implementations.
What you used to store child nodes is called a hash table and you might want to use a standard implementations and avoid implementing it yourself unless you have very-very specific reasons to do that. Your implementation has some limitations (character range, for example).
I think, your code has a bug in method has:
...
else if (letter[val].flag==true || word.length()==1) {
return true;
}
If that method is intended to return true if there are strings starting with word then it shouldn't check flag. If it must return true if there is an exact match only, it shouldn't check word.length().
And, finally, addressing your question: not the optimal, but the simplest solution would be to make a method, which takes a string and returns a node matching that string and a method that composes all the words from a node. Something like this (not tested):
class Tree {
...
public List<String> matches(CharSequence prefix) {
List<String> result = new ArrayList<>();
if(r != null) {
Node n = r._match(prefix, 0);
if(n != null) {
StringBuilder p = new StringBuilder();
p.append(prefix);
n._addWords(p, result);
}
}
return result;
}
}
class Node {
...
protected Node _match(CharSequence prefix, int index) {
assert index <= prefix.length();
if(index == prefix.length()) {
return this;
}
int val = prefix.charAt(index) - 'a';
assert val >= 0 && val < letter.length;
if (letter[val] != null) {
return letter[val].match(prefix, index+1);
}
return null;
}
protected void _addWords(StringBuilder prefix, List<String> result) {
if(this.flag) {
result.add(prefix.toString());
}
for(int i = 0; i<letter.length; i++) {
if(letter[i] != null) {
prefix.append((char)(i + 'a'));
letter[i]._addWords(prefix, result);
prefix.delete(prefix.length() - 1, prefix.length());
}
}
}
}
Maybe a longshot here, but why don't you try regexes here? As far as i understand you want to match words to a list of words:
List<String> getMatches(List<String> list, String regex) {
Pattern p = Pattern.compile(regex);
ArrayList<String> matches = new ArrayList<String>();
for (String s:list) {
if (p.matcher(s).matches()) {
matches.add(s);
}
}
return matches
}
I am in a beginning class for programming and try to combine 2 lists to make one list, putting the new list in numerical order. The part I am having trouble with is, allowing the code to loop, repeating the steps so that it runs through the total original loops to complete the final list which is a combination of all the numbers from the original lists. Any guidance for the loop would be appreciated. Thank you.
import inClass.list.EmptyListException;
import inClass.list.List;
public class InitialLists {
public static void main(String[] args) {
List<Integer> intObject1 = new List<Integer>();{
intObject1.insertAtFront(25);
intObject1.insertAtFront(19);
intObject1.insertAtFront(3);
intObject1.print();}
List<Integer> intObject2 = new List<Integer>();{
intObject2.insertAtFront(120);
intObject2.insertAtFront(1);
intObject2.print();}
List<Integer> combinedList = new List<Integer>();
int object1 = intObject1.removeFromBack();
int object2 = intObject2.removeFromBack();
while(intObject1.removeFromBack() != null && intObject2.removeFromBack() != null){
try {
{
if (intObject1.removeFromBack() > intObject2.removeFromBack()) {
combinedList.insertAtFront(object2);
intObject1.insertAtBack(object1);
}
else if (intObject2.removeFromBack() < intObject1.removeFromBack()) {
combinedList.insertAtFront(object1);
intObject2.insertAtBack(object2);
}
else if (intObject1.removeFromBack() == intObject2.removeFromBack()) {
combinedList.insertAtFront(object1);
}
}
combinedList.print();
object1 = intObject1.removeFromBack();
object2 = intObject2.removeFromBack();
} // end try
catch (EmptyListException emptyListException) {
emptyListException.printStackTrace();
} // end catch
} //end while
} // end main
}// end class
What about:
List<Integer> combinedList = new ArrayList<Integer>();
combinedList.addAll(intObject1);
combinedList.addAll(intObject2);
Collections.sort(combinedList);
Or am I missing something?
To merge two files / lists / streams you need a loop that looks a bit like this
WHILE NOT FINISHED
GET SMALLEST VALUE FROM INPUTS
APPEND SMALLEST VALUE TO OUTPUT
So how will you know that you are finished?
How will you get the smallest of the next item in each list?
The code I have written above is called pseudocode; it is a way of describing the steps of an algorithm. Keep expanding each step until you have pseudocode that you can implement in your chosen language, in this case Java.
Hope that helps ...
I guess your problem is because of possible uneven size of two lists. Try putting while condition as below:
Integer object1 = intObject1.removeFromBack();
Integer object2 = intObject2.removeFromBack();
while(object1 != null || object2!= null){
if(object1 ==null){
//safe to assume object2 is not null as both not null together (that is the termination condition)
combinedList.insertAtFront(object2);
}else if(object2 ==null){
//safe to assume object1 is not null as both not null together (that is the termination condition)
combinedList.insertAtFront(object1);
}else{
//put you normal condition of handling object1 and object2 being not null
if (object1.intValue() > object2.removeFromBack()) {
combinedList.insertAtFront(object2);
intObject1.insertAtBack(object1);
}
else if (object2.intValue() < object1.intValue()) {
combinedList.insertAtFront(object1);
intObject2.insertAtBack(object2);
}
else if (object1.intValue() == object2.intValue()) {
combinedList.insertAtFront(object1);
}
}
object1 = null;
object2 = null;
try{
object1 = intObject1.removeFromBack();
}catch (EmptyListException emptyListException) {
//do nothing
} // end catch
try{
object2 = intObject2.removeFromBack();
}catch (EmptyListException emptyListException) {
//do nothing
} // end catch
}
Also please note: There are better way of doing the merge of two sorted list elements. This approach is advised in light of your little known custom List class.
I am reading in a .csv file sort of like a spreadsheet in excel. There are a certain number of columns, determined by the file, and I read each line into a string array using the .split(",") method. I then put this into an array list so it can hold all of the string arrays without giving it a specific size. However, when I go to sort the array list using Collections.sort(), the program breaks. What could the problem be? Here is my code to sort:
Collections.sort(stringList, new Comparator < String[] > () {
public int compare(String[] strings, String[] otherStrings) {
return -1 * (strings[sortNum].compareTo(otherStrings[sortNum]));
}
});
Two points:
Don't multiply the result of compare by -1 to reverse a comparison. Integer.MIN_VALUE * -1 is still Integer.MIN_VALUE. Instead, reverse the order of the comparison itself
My guess is that you've actually got some rows without enough columns. Perhaps you should put those at the end?
Something like:
Collections.sort(stringList, new Comparator < String[] > () {
public int compare(String[] x1, String[] x2) {
if (x1.length > sortNum && x2.length > sortNum) {
return x2[sortNum].compareTo(x1[sortNum]);
}
if (x1.length > sortNum) {
return 1;
}
if (x2.length > sortNum) {
return -1;
}
return x2.length - x1.length;
}
});
Alternatively, filter your list first to make absolutely sure that all rows have enough columns.
Well, either strings[sortNum] or otherStrings[sortNum] could be out of bounds. You need to do some checks to prevent that. Also, strings[sortNum] or otherStrings[sortNum] could be null. I bet you're running into one of these 2 things. What does the call stack indicate?
Try using this
First your class comparator with a constructor:
public class MyStringArrayComparator implements Comparator<String[]>{
Integer sortNum;
public MyStringComparator(Integer index) {
sortNum = index;
}
#Override
public int compare(String[] strings, String[] otherStrings) {
return -1*(strings[sortNum].compareTo(otherStrings[sortNum]));
}
}
and in your code
Collections.sort(stringList,new MyStringArrayComparator<String[]>(index));
Hope that works for you
Sharing code in case someone need to do the sort on multiple columns.
public final class ArrayComparatorWithIndex<T extends Comparable<T>> implements Comparator<T[]>
{
private final int[] indexToSort;
public ArrayComparatorWithIndex(int[] indexToSort)
{
if(indexToSort == null || indexToSort.length == 0){
throw new IllegalArgumentException("Index to use for sorting cannot be null or empty.");
}
this.indexToSort = indexToSort;
}
#Override
public int compare(T[] str, T[] otherStr)
{
int result= 0;
for (int index : indexToSort)
{
result= str[index].compareTo(otherStr[index]);
if (result != 0){
break;
}
}
return result;
}
}
//Example how to use it:
int[] indexForSorting= new int[] { 1, 3 };
Collections.sort(stringList, new ArrayComparatorWithIndex<String>(indexForSorting));
I suspect you might have a closure problem in reference to the 'sortNum' variable. See Jon Skeet's closure article for some guidance, even though it deals with closures in C# it should still be relevant. Even if you don't have this issue, it's a good read. :)
you can provide default values for empty "cells":
public int compare(String[] strings, String[] otherStrings) {
String one, other;
one = other = ""; // default value
if (sortNum<strings.length && strings[sortNum] != null) {
one = strings[sortNum];
}
if (sortNum<otherStrings.length && otherStrings[sortNum] != null) {
other = otherStrings[sortNum];
}
return -1 * (one.compareTo(other));
}