I have written an ID3 and it prints the data. However, this is not very readable when the data sets become large. Is there any more human readable way this can be done?
Example of output given by the code below:
if( Outlook == "Sunny") {
if( Humidity == "High") {
PlayTennis = "No";
} else if( Humidity == "Normal") {
PlayTennis = "Yes";
}
} else if( Outlook == "Overcast") {
PlayTennis = "Yes";
} else if( Outlook == "Rain") {
if( Wind == "Weak") {
PlayTennis = "Yes";
} else if( Wind == "Strong") {
PlayTennis = "No";
}
}
I would ideally like to get a graph as follows from the above output:
Is there something I could use instead of these methods to give a tree automatically. The end goal is to analyse the tree depth etc?
Take a look at https://code.google.com/p/treelayout/
It is the layout code used by antlr and seems to work pretty well.
Related
I think this counts for any other language as well, but I'm using Java for Android in this example here. I'm trying to validate a form, but it has multiple inputs.
if( !anuncio.getEstado().isEmpty() ){
if( !anuncio.getCategoria().isEmpty() ){
if( !anuncio.getTitulo().isEmpty() ){
if( !valor.isEmpty() && !valor.equals("0") ){
if( !anuncio.getTelefone().isEmpty() ){
if( !anuncio.getDescricao().isEmpty() ){
saveForm();
}else {
showErro("please fill the entire form");
}
}else {
showErro("please fill the entire form");
}
}else {
showErro("please fill the entire form");
}
}else {
showErro("please fill the entire form");
}
}else {
showErro("please fill the entire form");
}
}else {
showErro("please fill the entire form");
}
Is there any way or method to simplify this? Because let's say if it were 10 inputs, it would be a horrible code doing 10 ifs, you know? How do I simplify this?
In your case this would suffice:
if( !anuncio.getEstado().isEmpty()
&& !anuncio.getCategoria().isEmpty()
&& !anuncio.getTitulo().isEmpty()
&& !valor.isEmpty() && !valor.equals("0")
&& !anuncio.getTelefone().isEmpty()
&& !anuncio.getDescricao().isEmpty() ) {
safeForm();
} else {
showErro("please fill the entire form");
}
For more complex code you may i.e. want to count errors and show messages later if count > 0 etc.
PS also I believe you wanted to write "showError" not "showErro"
More elegant way to write it:
if(!containsEmptyText(anuncio.getEstado(), anuncio.getCategoria(), anuncio.getTitulo(),
valor, anuncio.getTelefone(), anuncio.getDescricao()) && !valor.equals("0") ){
saveForm();
}else
showErro("please fill the entire form");
private static boolean containsEmptyText(String... textArray){
for(String text : textArray)
if(text.isEmpty())
return true;
return false;
}
When trying to write the following code, it comes up with 3 errors. Syntax error on token 'else', insert 'assignmentoperator expression' to complete assignment and that cannot convert from string to boolean. This is for a combo box and when I choose from the combo box, I want to remove the other sites from a list. 'site' is a variable in main and c.site is a parameter in another agent. Can someone explain what to do.
if ( site ) {
if ( c.site.equals( "x" ) ) {
cavernIterator.remove();
continue;
}
}
else {
if ( c.site.equals( "y" ) ) {
cavernIterator.remove();
continue;
}
}
else {
if ( c.site.equals( "z" ) ) {
cavernIterator.remove();
continue;
}
}
else {
( c.site.equals( "a" ) ) {
cavernIterator.remove();
continue;
}
}
You can't have several else blocks for the same if. Each else block has to be attached to its own if:
if(a) {
...
} else {
if (b) {
...
}
else {
if (c) {
...
}
else {
...
}
}
}
Or, with a shorthand for all this, you can have a single-statement block inside the else, so it looks like this:
if (a) {
...
}
else if (b) {
...
}
else if (c) {
...
}
else {
...
}
This second one is nearly the same as the first, as it takes advantage of not having to wrap a statement block in braces if your statement block only has single statement, like this:
if (myCondition)
System.out.println("My condition passed");
else
System.out.println("My condition did not pass");
Else if should be written this way :
if (...) {
// Code here
} else if (...) {
// Code here
} else {
// Code here
}
Also your cases seem similar
String[] sites = {"x","y","z","a"};
boolean contains = Arrays.stream(sites).anyMatch(c.site::equals);
if (contains) {
cavernIterator.remove();
continue;
}
EDIT: Considering site is a string
String site = "x"; // Default value provided
String[] sites = {"x","y","z","a"};
boolean condition = site.equals(c.site) && Arrays.stream(sites).anyMatch(c.site::equals);
if (condition) {
cavernIterator.remove();
continue;
}
if ( site ) {
if ( c.site.equals( "x" ) ) {
}
}
equals
if(site && c.site.equals("x")){
}
And your last else is messed up.
Likely look on some Java tutorials because you write the least efficient way possible. This is your code in short:
if (site && (c.site.equals("x") || c.site.equals("y") || c.site.equals("z") || c.site.equals("a"))) {
cavernIterator.remove();
}
For a project at University, I have to create a game of Tic Tac Toe.
I have this for loop with if statements to search through the 2D array of 3x3 size, and return if it's either X or O (enum). That results in showing which side has won the game.
However, the problem I have is that if the 2D array is not complete, as in if all the 9 boxes are not filled with X or O, the method shows a NullPointerException.
Edit: I have to add that I require the empty grid to be null as few other unit tests assume grid[][] is initialized as null.
Error:
Exception in thread "main" java.lang.NullPointerException
at TicTacToeImplementation.whoHasWon(TicTacToeImplementation.java:80)
at ApplicationRunner.main(ApplicationRunner.java:24)
Code:
public enum Symbol {
X, O
}
private Symbol winner;
public Symbol whoHasWon() {
for (Symbol xORo : Symbol.values()) {
if ((grid[0][0].equals(xORo) &&
grid[0][1].equals(xORo) &&
grid[0][2].equals(xORo))) {
winner = xORo;
isGameOver = true;
break;
} else if ((grid[1][0].equals(xORo) &&
grid[1][1].equals(xORo) &&
grid[1][2].equals(xORo))) {
winner = xORo;
isGameOver = true;
break;}
else if { //Code carries on to account for all 8 different ways of winning
} else {
isGameOver = true;
}
}
return winner;
}
You can use multiple ways to ignore the "null" exception with an empty array.
The 1st way is to fill it with a different default symbol such as E. So when you initialize your arry at the beginning, instead of making it all empty and null, you can fill it with E's
for(int i=0;i<=2;i++){
for(int k=0;k<=2;k++){
grid[i][k] = "E";
}
}
Add this to beginning to fill it with E's first instead of nulls.
Another method is to find how to ignore the nulls using try or the following methods that can be found in this linkhttps://www.javacodegeeks.com/2012/06/avoid-null-pointer-exception-in-java.html:
I won't be going into it because I believe the 1st method is easier to use and implement. However, depending on your requirements for your assignment, I would look at both just to be sure.
Hope this helps, Good luck!
You can change the comparasion of String.The code may be like this ;
public Symbol whoHasWon() {
for (Symbol xORo : Symbol.values()) {
if ((grid[0][0] == xORo.name() &&
grid[0][1] == xORo.name() &&
grid[0][2] == xORo.name())) {
winner = xORo;
isGameOver = true;
break;
} else if ((grid[1][0] == xORo.name() &&
grid[1][1] == xORo.name() &&
grid[1][2] == xORo.name())) {
winner = xORo;
isGameOver = true;
break;}
else if { //Code carries on to account for all 8 different ways of winning
} else {
isGameOver = true;
}
}
return winner;
}
Enum like your's implemented
public enum Symbol{
X, O
}
}
As stated in this post, you can use either equals() or == to compare enums but using == is null safe while equals() isn't.
So basically, just write your checks like this:
if (grid[0][0] == xORo &&
grid[0][1] == xORo &&
// etc.
However, if you want to use the equals() method, you could just write a method that checks for null then compares the two values and returns the result:
public boolean isEqual(Symbol s1, Symbol s2) {
if (s1 != null && s1.equals(s2)) {
return true;
}
return false;
}
You could then call the isEqual() method like this:
if (isEqual(grid[0][0], xORo) &&
isEqual(grid[0][1], xORo) &&
// etc.
Compare three boolean values and display the first one that is true.
Hey guys, I am trying to write a program that compares three boolean values and displays the first true one. I am comparing three words for their length, and it will display the longest. The error that I am getting is that my else tags aren't working. Take a look at the code.
//Check which word is bigger
if (len1 > len2)
word1bt2 = true;
if (len2 > len3)
word2bt3 = true;
if (len1 > len3)
word1bt3 = true;
//Check which word is the longest
if (word1bt2 == true && word1bt3 == true);
System.out.println(wor1);
else if (word2bt3 == true);
System.out.println(wor2);
else System.out.println(wor3);
I have set boolean values for word1bt2, word2bt3 and word1bt3. In eclipse, I am getting a syntax error under the elses in my code above. Any help would be great!
if (word1bt2 == true && word1bt3 == true);
Is wrong, you need to remove the semicolon:
if (word1bt2 == true && word1bt3 == true)
Same for the elses
else (word2bt3 == true);
Is wrong too, it should be
else if (word2bt3 == true)
Side note: boolean values can be used as condition, so your if statements should be
if (word1bt2 && word1bt3) // The same as if (word1bt2 == true && word1bt3 == true)
How to compare three boolean values?
Dont!
If you find yourself needing to compare three variable you may as well cater for any number of variables immediately - there's no point hanging around - do it properly straight away.
public String longest(Iterator<String> i) {
// Walk the iterator.
String longest = i.hasNext() ? i.next() : null;
while (i.hasNext()) {
String next = i.next();
if (next.length() > longest.length()) {
longest = next;
}
}
return longest;
}
public String longest(Iterable<String> i) {
// Walk the iterator.
return longest(i.iterator());
}
public String longest(String... ss) {
// An array is iterable.
return longest(ss);
}
Remove the ; and change it with brackets {}.
if (word1bt2 && word1bt3) {
System.out.println(wor1);
} else if (word2bt3) {
System.out.println(wor2);
} else {
System.out.println(wor3);
}
Issue with the else blocks: use {} insteaad of () to enclose instructions...
Remove the ; at the first if!!!!! - Quite common mistake, with very puzzling results!
//Check which word is the longest
if (word1bt2 == true && word1bt3 == true) { //leave ; and always add bracket!
System.out.println(wor1);
}
else if(word2bt3 == true)
{
System.out.println(wor2);
}
else {
System.out.println(wor3);
}
if you need a condition in an else branch, you have to use if again - plain else won't have such a feature...
ALWAYS use brackets for bodies of if statements, loops, etc!!!
Be extremely careful NOT to use ; in the lines that don't behave well with it:
if statements
for loops
while() {...} loops' while statement
try this, if lenght are equal then s1 is considered as Bigger. Also i have not added null check
public class Test {
public static void main(String[] args) {
String word1 = "hi";
String word2 = "Hello";
String word3 = "Hell";
String Bigger = null;
if(word1.length() >= word2.length() && word1.length() >= word3.length() ){
Bigger = word1;
}else if(word2.length() >= word1.length() && word2.length() >= word3.length()){
Bigger = word2;
}else if(word3.length() >= word2.length() && word3.length() >= word1.length()){
Bigger = word3;
}
System.out.println(Bigger);
}
}
I can't find the problem that causes my while loop not to work.
When I run the program and press a radio button, I get this error code:
Syntax error, insert "while ( Expression ) ;" to complete DoStatement
Here is my loop:
int i = 1;
boolean x;
//for (i = 0; i < 6; i++) {
do{
warning.setText(" FEL!");
i++;
while(x == false);{
if(e.getSource() == buttonOK){
if(buttonDollar.isSelected() == false){
x = false;
}
if(buttonEuro.isSelected() == false){
x = false;
}
if(buttonPund.isSelected() == false){
x = false;
}
if(buttonKrona.isSelected() == false){
x = false;
}
break;
}
}
}
You are missing the syntax for "while" element
From the sun site (I am guessing that this is java)
do {
statement(s)
} while (expression);
I think you need a closing curly brace before the while
do{
warning.setText(" FEL!");
i++;
}while(x == false);
The whole structure is all wrong, you're looking to do something very simple:
if(e.getSource() == buttonOK)
{
if( !buttonDollar.isSelected() && !buttonEuro.isSelected()
&& !buttonPund.isSelected() && !buttonKrona.isSelected() )
{
warning.setText(" FEL!");
}
}
From a UI perspective, it's better to ensure that one radio button is always selected (as this is what your users will likely expect).