Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java First Line of File Does Not Equal User Input

gbradham

New Coder
Java:
import java.util.Scanner;
import java.io.*;

public class GB_Project3 {
    File english = new File("english.txt");
        File panjabi = new File("panjabi.txt");
        Scanner readEnglish = new Scanner(english);
        Scanner readPanjabi = new Scanner(panjabi);
        Scanner input = new Scanner(System.in);
        int count = 0;
        int line = 0;
        String translate = "";

        System.out.println("Enter an english word: ");
        String userInput = input.nextLine();

        while (readEnglish.hasNextLine()) {
            String englishInfo = readEnglish.nextLine();
            if (englishInfo.equals(userInput)) {
                line = count;
            }
            count++;
        }
        if (line == 0) {
            System.out.println("This word does not exist in our dictionary!");
        } else {
            for (int i = 0; i < line; i++) {
                translate = readPanjabi.nextLine();
            }
            System.out.println(translate);
        }

    }
}

The point of this code is to see if a user's input matches a text file, and then translate it to panjabi. It will do this fine for every single word EXCEPT the first line, "my". I have tried debugging this for hours and have no idea what to do. I am still a beginner and have tried searching for answers but have not found anything and have resorted to posting on a forum.
 
Your code did not compile for me. Did you leave out the main() method by mistake ? That would explain the superfluous curly bracket at the end. Anyway when I could compile it, the problem was clear. Your last loop should be

for (int i = 0; i <= line; i++)

assuming that both text files have the same number of lines. You were stopping just one short of the desired word.
 
Your code did not compile for me. Did you leave out the main() method by mistake ? That would explain the superfluous curly bracket at the end. Anyway when I could compile it, the problem was clear. Your last loop should be

for (int i = 0; i <= line; i++)

assuming that both text files have the same number of lines. You were stopping just one short of the desired word.
Yes, I figured out the problem was that I was stopping 1 short. Thank you.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom