I want to make a class that takes a String and checks if that String is found in a .txt File. If it is found, I want to store the words following that String in a String array.
Here is what I have until now:

import java.io.File;
import java.util.Scanner;


public class ClassName {
	
	private Scanner scan;
	public String ch[] = new String[3];
	private String ID;
	
	public Champion(String id) {
		
		this.ID = id;
		
		try{
			scan = new Scanner(new File("myfile.txt"));
		}
		
		catch(Exception e) {
			System.out.println("Error: " + e);
		}
		
		while(scan.hasNext()) {
			
			String check = scan.next();
			
			if(check == ID) {
				for(int i=0; i<champion.length; i++) {
					this.ch[i] = scan.next();
				}
			}
		}
	}
	
	public void printValues() {
		System.out.printf("%s: %s %s %s", ID, ch[0], ch[1], ch[2]);
	}
	
}

Where the myfile.txt goes like: "abc 20 40 50" on each row"

From what I can tell the if statement does not work, but I can't understand why.
Help!

Recommended Answers

All 2 Replies

Don't use '==' to compare string values. Use the '.equals' method.

Lol, can't believe it was that easy. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.