0

If this works and the other doesn't, what is the difference?

The other one reads a file , and then sets the constructor as 4 arrays each on with its own values and then uses the return to output the strings

0

I thought your problem was with a search.
How do those things you just mentioned affect the search?

You need to isolate the search method from the rest of the code and see why it works in one program and does not work in the other. Add printlns to the search method to show everything that it looks at. Run the two different programs and see what prints out that is different. Something must be different or the search would work in both programs.

0

I thought your problem was with a search.
How do those things you just mentioned affect the search?

You need to isolate the search method from the rest of the code and see why it works in one program and does not work in the other. Add printlns to the search method to show everything that it looks at. Run the two different programs and see what prints out that is different. Something must be different or the search would work in both programs.

Thank you for all of the help I believe I can work on it.

0

You know I really can't figure out why is this happening do you think initializing another string is a good idea?

0

What do you mean by that?

Well initializing another string.
I really can't find out why the statements wont work when the constructor is initialized by the File reader, but it will work if i initialize the variables on spot. I'm getting a bit frustrated.

0

Did you add some printlns to show the contents of the variables for both cases: when it works and when it fails?

0

Did you add some printlns to show the contents of the variables for both cases: when it works and when it fails?

Yes I did, I made a println for the getText and after the statement is executed.
The first statement works therefore I receive "pokey
pokey", but when I type in sakie I only receive sakie which means the statement is not executing.
and that is what I don't understand

0

when I type in sakie I only receive sakie which means the statement is not executing.

What statement is not executing?

The code you posted has missing class definitions and will not compile for me.

Edited by NormR1: n/a

0

What statement is not executing?

The code you posted has missing class definitions and will not compile for me.

That was not the original one since you told me to make a smaller program. the original one reads from a textfile into an array of constructors:

static  snakes[] snakesArray = new snakes[4];
try{
            int i = 0;
           /* Jgraphs File file = new File("snake.txt");*/File file = new File("C:\\Users\\SOAD\\Documents\\ComputerScience\\Assignment4\\src\\snake.txt");
            Scanner scanner = new Scanner(file);
            scanner.useDelimiter("%");
            while(scanner.hasNext()){
               snakesArray[i] = new snakes(scanner.next(),scanner.nextBoolean(),scanner.nextInt(),scanner.nextDouble());
               i++;
            }
         }
             catch(Exception e){
               e.printStackTrace();
            }

Then when I press the search button , it will basically replace a textfield which shows a name.

private class Listener implements ActionListener
      {
         public void actionPerformed(ActionEvent e){
            search.getText(); System.out.println(search.getText());
            if(search.getText().equalsIgnoreCase(snakesArray[0].snake1())==true){
                    nameField.setText(snakesArray[0].snake1());  
                    System.out.println(nameField.getText()); search.setText("Success");
            }            
            else{
            if(search.getText().equalsIgnoreCase(snakesArray[1].snake1())==true){
                nameField.setText(snakesArray[1].snake1());
                System.out.println(nameField.getText());
            }
            
        }
           
     }
           }

Edited by vaironl: n/a

0

Have the the snake class's constructor print out all the values it gets so you can see if it is being initialized correctly.

0

Have the the snake class's constructor print out all the values it gets so you can see if it is being initialized correctly.

Yeah everything is fine

0

Have the the snake class's constructor print out all the values it gets so you can see if it is being initialized correctly.

By the way NormR1 I'm really sorry for bothering you so much, I really understand a programmer must find all errors by himself but I really am getting a bit sad that I can't solve it.

0

You need to add printlns every place where the data could change and add printlns every place that you look at the data and test the data.
Some where you are assuming the data is there and it is not or you are not looking in the right place. Add enough printlns to show you where the data is and what it looks like.

Edited by NormR1: n/a

0

You need to add printlns every place where the data could change and add printlns every place that you look at the data and test the data.
Some where you are assuming the data is there and it is not or you are not looking in the right place. Add enough printlns to show you where the data is and what it looks like.

Ok, I figure out it is executing the else statement but for some reason the if statement is not executing like if the value is incorrect.

0

Print out ALL of the values of the variables being tested in the if statement so you can see what they are. When you see the values you should be able to understand what is happening.

0

Print out ALL of the values of the variables being tested in the if statement so you can see what they are. When you see the values you should be able to understand what is happening.

All of the values are fine , I really don't know what to do anymore. Ill just tell my teacher.

0

If the values are fine, then you are not using them correctly in your if statement.
Check that the logic of the if statement is correct.

Post the following 4 things:
the println code that prints out the values used in the if statement
What is printed out by the above printlns
the if statement that is not working the way you expect
An explanation of what you expect the if statement to do that it is not doing.

0

If the values are fine, then you are not using them correctly in your if statement.
Check that the logic of the if statement is correct.

Post the following 4 things:
the println code that prints out the values used in the if statement
What is printed out by the above printlns
the if statement that is not working the way you expect
An explanation of what you expect the if statement to do that it is not doing.

Println code:

System.out.println(search.getText());

The printed value is the string of the search textfield, which is use to type the name of the snake.

The if statement:

else if(search.getText().equalsIgnoreCase(snakesArray[1].snake1())==true ){
                    nameField.setText(snakesArray[1].snake1());
                        }

I believe this statement should compare the search current text, to the snakeArray[1] and if they are equal it should output that array's name.

0

What is the value of this?
snakesArray[1].snake1()
You need to print ALL the values used in the if statement.

What did the print statement print out? You posted 3 of the 4 things I asked for.

Your printlns should delimit the String. For example:
System.out.println("getText=" + search.getText() + "<");

Then you can see if there are blanks in the String.

0

What is the value of this?
snakesArray[1].snake1()
You need to print ALL the values used in the if statement.

What did the print statement print out? You posted 3 of the 4 things I asked for.

Your printlns should delimit the String. For example:
System.out.println("getText=" + search.getText() + "<");

Then you can see if there are blanks in the String.

It prints out the name. sakie

Edited by vaironl: n/a

0

There were two things to print out.
And they should be printed with delimiters to see if there are any spaces in the String.

0

There were two things to print out.
And they should be printed with delimiters to see if there are any spaces in the String.

I got a for loop instead of all of those statements. and Ill ask my teacher. Thanks allot for all of the help.
And once again sorry

0

Good luck.

I got everything to work, I had to hardcode the if and else-if loops instead of using a for loop.

0

Strange? What do you mean by if loops?

Well that would compare the typed strings to the array's string.
And I also saved the changes to the textfile by using a FileWriter.

0

Ok, Good luck.
I'll be gone for a while.

Thanks, I hope you have a good time doing whatever the occasion is.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.