mardi 20 janvier 2015

Java If Statement not working - variable is set correctly, and conditions are correct, but code inside the statement does not activate

Here is the code (The if statement can be found below.):



package functions;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class GetResults {
public static String results = null, inputData;
public static List<String> resultsList = new ArrayList<String>();
public static List<Integer> resultCordinates = new ArrayList<Integer>();
public static Integer lastIndex = 0, i = 0, j = 0;
public static int amount = 0;

@SuppressWarnings("deprecation")
// TODO Create multi-results options.
public static void getResults(String search) {
try {
FileInputStream fin2 = new FileInputStream("PlayerList.txt");
inputData = new DataInputStream(fin2).readLine();
fin2.close();
} catch (Exception exception) {
System.out.println("Could not read data file. Error 5.");
}
if(inputData != null) {
while(lastIndex != -1){
lastIndex = inputData.indexOf("," +search, lastIndex);
if(lastIndex != -1){
System.out.println("Before: " + amount + ":" + lastIndex + ":" + resultCordinates + ":" + i);
amount++;
resultCordinates.add(inputData.indexOf("," + search, lastIndex));
System.out.println(inputData.indexOf("," + search, lastIndex));
lastIndex += search.length();
i++;
System.out.println("After: " + amount + ":" + lastIndex + ":" + resultCordinates + ":" + i);
}

}
System.out.println("Amount is: " + amount);
if(amount == 0) {
System.out.println("Amount is zero.");
results = "No results were found";
} if(amount == 1) {
System.out.println("Amount is less than one. And: " + resultsList.get(j));
results = inputData.substring(resultCordinates.get(j), inputData.indexOf(",", resultCordinates.get(j) + 1));
results = results.replace(";", " is a ");
results = results.replace("|", " rated ");
results = results.replace("~", " from the ");
results = results.replace(",", "");
} if(amount > 1) {
for(amount = amount; amount == 0; amount--) {
System.out.println("Amount is greater than one. And: " + resultsList.get(j));
resultsList.add(inputData.substring(resultCordinates.get(j), inputData.indexOf(",", resultCordinates.get(j) + 1)));
results = "Check resultsList[]";
}
}
System.out.println("Test 1" + results + ":" + resultsList);
} else {
results = "No players were in database.";
}
System.out.println("Test 2" + results + ":" + resultsList);
}


}


And here is the class that calls the method:



package start;
import java.io.FileInputStream;

import javax.swing.JFrame;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;

import constructors.Window;
import functions.GetResults;

public class Start {

public static Integer buttonOneStage = 0, buttonTwoStage = 0;
public static Boolean fileExists = false, isError = false;
public static Window window1Placeholder;
public static Window window4Placeholder;

public static void main(String[] args) {
DocumentListener documentListener = new DocumentListener() {
public void changedUpdate(DocumentEvent documentEvent) {
System.out.println("ChangedUpdate");
doIt(false);
}
public void insertUpdate(DocumentEvent documentEvent) {
System.out.println("InsertUpdate");
doIt(false);
}
public void removeUpdate(DocumentEvent documentEvent) {
System.out.println("RemoveUpdate");
doIt(true);
}
public void doIt(Boolean isRemoveUpdate) {
Start.window4Placeholder.listModel.removeAllElements();
GetResults.getResults(Start.window4Placeholder.tf1.getText());
if(GetResults.results != "Check resultsList[]") {
System.out.println("Only one result");
Start.window4Placeholder.listModel.addElement(GetResults.results);
} else {
System.out.println("More than one result");
for(Integer i = 0; i < GetResults.resultsList.get(i).length(); i++) {
System.out.println(GetResults.resultsList.get(i));
Start.window4Placeholder.listModel.addElement(GetResults.resultsList.get(i));
}
} if(isRemoveUpdate && window4Placeholder.tf1.getText().length() == 0) {
System.out.println("Removing all elements.");
Start.window4Placeholder.listModel.removeAllElements();
window4Placeholder.label2.setText("");
} else {
window4Placeholder.label2.setText("Results: ");
}
}
};
Window window1 = new Window("Search for a player or add a player.", "", "", "", "", "", "", "", "Search for player", "Add player", "", "", true, false, false, false, false, false, false, false, true, true, false, false, null, false, JFrame.EXIT_ON_CLOSE, true, 300, 100, "Football Card Sorter");
window1Placeholder = window1;
Window window4 = new Window("Enter a Player name", "", "", "", "", "", "", "", "Main Menu", "", "", "", true, true, true, true, true, false, false, false, true, false, false, false, null, true, JFrame.HIDE_ON_CLOSE, false, 500, 200, "Search Results");
Start.window4Placeholder = window4;
Start.window4Placeholder.tf1.getDocument().addDocumentListener(documentListener);

try {
FileInputStream testfin = new FileInputStream("PlayerList.txt");
fileExists = true;
} catch(Exception exception) {
fileExists = false;
}
}


}


What I hope is that you can make that one if statement if GetResults.java to go through. When I run the code:



System.out.println(amount);
if(amount == 0) {
System.out.println("Amount is zero.");
results = "No results were found";
} if(amount == 1) {
System.out.println("Amount is less than one. And: " + resultsList.get(j));
results = inputData.substring(resultCordinates.get(j), inputData.indexOf(",", resultCordinates.get(j) + 1));
results = results.replace(";", " is a ");
results = results.replace("|", " rated ");
results = results.replace("~", " from the ");
results = results.replace(",", "");
} if(amount > 1) {
for(amount = amount; amount == 0; amount--) {
System.out.println("Amount is greater than one. And: " + resultsList.get(j));
resultsList.add(inputData.substring(resultCordinates.get(j), inputData.indexOf(",", resultCordinates.get(j) + 1)));
results = "Check resultsList[]";
}
}


None of the System.out.println()'s output anything except the first one, which says that amount = 2, so I think the block is just getting skipped! This problem is probably just something simple so please help me run this IF block! If you need me to expand on my question, please ask!


Aucun commentaire:

Enregistrer un commentaire