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;
			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.

Recommended Answers

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

Jump to Post

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 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.