简体   繁体   中英

Buffered Reader read certain line and text

This is my first post, so i'm not sure how things work here. Basically, i need some help/advice with my code. The method need to read a certain line and print out the text after the inputted text and =

The text file would like

A = Ant

B = Bird

C = Cat

So if the user it input "A" it should print out something like

-Ant

So far, i manage to make it ignore "=" but still print out the whole file

here is my code:

public static void readFromFile() {
    System.out.println("Type in your word");
    Scanner scanner = new Scanner(System.in);
    String input = scanner.next();
    String output = "";
    try {
        FileReader fr = new FileReader("dictionary.txt");           
        BufferedReader br = new BufferedReader(fr);            
        String[] fields;
        String temp;
        while((input = br.readLine()) != null) {
            temp = input.trim();
            if (temp.startsWith(input)) {
                String[] splitted = temp.split("=");
                output += splitted[1] + "\n";
            }
        }
        System.out.print("-"+output);
    }
    catch(IOException e) {

    }
}

It looks like this line is the problem, as it will always be true.

if (temp.startsWith(input))

You need to have a different variables for the lines being read out of the file and for the input you're holding from the user. Try something like:

String fileLine;
while((fileLine = br.readLine()) != null)
    {
        temp = fileLine.trim();
        if (temp.startsWith(input))
        {
            String[] splitted = temp.split("=");
            output += splitted[1] + "\n";
        }
    }

try this, it works: STEPS:

1) read input using scanner 2) read file using bufferedreader 3) split each line using "-" as a delimiter 4) compare first character of line with input 5) if first character is equal to input then print the associated value , preceded by a "-"

import java.io.BufferedReader; 
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
import java.util.Scanner; 
class myRead{
    public static void main(String[] args) throws FileNotFoundException, IOException {
        System.out.println("Type in your word");
        Scanner scanner = new Scanner(System.in);
        String input = scanner.next();
        long numberOfLines = 0;
        BufferedReader myReader = new BufferedReader(new FileReader("test.txt")); 
        String line = myReader.readLine();
            while(line != null){
                String[] parts = line.split("=");
                if (parts[0].trim().equals(input.trim())) {
                System.out.println("-"+parts[1]);
                }
                line = myReader.readLine();
            } 
}
}

OUTPUT (DEPENDING ON INPUT):

- Ant
- Bird
- Cat

You can use useDelimiter() method of Scanner to split input text

scanner.useDelimiter("(.)*="); // Matches 0 or more characters followed by '=', and then gives you what is after `=`

The following code is something I've tried in IDEONE ( http://ideone.com/TBwCFj )

Scanner s = new Scanner(System.in);
        s.useDelimiter("(.)*=");
        while(s.hasNext())
        {
            String ss = s.next();
            System.out.print(ss);

        }
 /**
  * Output
  *
  */
  Ant
  Bat

You need to first split the text file by new line "\\n" (assuming after each "A = Ant", "B = Bird" ,"C = Cat" declaration it starts with a new line) and THEN locate the inputted character and further split that by "=" as you were doing.

So you will need two arrays of Strings (String[ ]) one for each line and one for the separation of each line into eg "A" and "Ant". You are very close.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM