Reputation:
import java.io.File;
import java.util.Scanner;
public class scoresedit {
public static void main(String[] args) throws Exception{
System.out.println("Reads in a file at the beginning of a loop, displays these top 3 scores by name and their respective score." +
"\nEnter a new name and score following a prompt. " +
"\nIf the new name and score exceeds one of the top three scores," +
"\nplaces the new name in the top three, moving all lower scores down, dropping the lowest score." +
"\nSaves the results in the file top3Scores.txt." +
"\nStops the loop when exit is entered for a name.");
File r = new File ("top3Scores.txt");
System.out.println(r);
String str = readSource (r);
System.out.println(str);
String[] strArray = new String[] {str};
Scanner in = new Scanner(System.in);
System.out.print("Enter a new name and score or 'exit' to quit:");
String nameAndScore = in.nextLine();
String[] nameAndScoreArray = new String[] {nameAndScore};
String nameAndScoreArray1[] = nameAndScore.split(" ");
while (nameAndScore != "exit") {
if (nameAndScoreArray[1] > strArray[1]) }
}
private static String readSource(File r) throws Exception{
Scanner in;
in = new Scanner (r);
String str = "";
while (in.hasNext())
str += in.nextLine() +"\n";
in.close();
return str;
}
}
I keep getting the error "The operator > is undefined for the argument type(s) java.lang.String, java.lang.String" when I try to compare the two arrays and I don't know how to fix it. The code isn't finished; obviously I'm adding more under the if statement but I want to resolve this first.
Upvotes: 1
Views: 146
Reputation: 7086
You are trying to compare nameAndScoreArray[1]
and strArray[1]
which are strings, I assume they contain the scores of two teams and the one with the highest score wins, so you want to use Integer.parseInt(String string)
in order to see which one has the highest score:
if (Integer.parseInt(nameAndScoreArray[1]) > Integer.parseInt(strArray[1]))
Upvotes: 6
Reputation: 20442
You can not compare String
with the operators <
, >
, instead you need to use compareTo.
Upvotes: 4