简体   繁体   中英

How to print out a String line by line after searching it in Java

I want to search a String and print the lines I require My code currently just prints the entire string, however I need to print just the lines that contain what I need. for ex: if there are 2 lines in my String that contain the word math, I need to print just those 2 lines.

My Code

package sample;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.BufferedWriter;
import java.io.FileWriter;

public class Main extends Application {
    Stage window;
    Scene scene1, scene2, scene3;
    @Override
    public void start(Stage primaryStage) throws Exception {
        ReadTutor r = new ReadTutor();
        window = primaryStage;
        window.setTitle("Tutor Finder");
        Label labelA1 = new Label("Welcome to the best online tutor\nproviding service in the world");
        Button buttonA2 = new Button("Find a tutor!");
        Button buttonA1 = new Button("Become a tutor!");
        Button buttonB1 = new Button("Click here to go back");
        Button buttonB2 = new Button("Submit");
        Button buttonC1 = new Button("Search");
        Button buttonC2 = new Button("Click here to go back");
        buttonB1.setOnAction(e -> window.setScene(scene1));
        buttonC2.setOnAction(e -> window.setScene(scene1));
        buttonA1.setOnAction(e -> window.setScene(scene2));
        buttonA2.setOnAction(e -> window.setScene(scene3));
        Label labelB1 = new Label("First Name:");
        TextField textB1 = new TextField();
        Label labelB2 = new Label("Last Name:");
        TextField textB2 = new TextField();
        Label labelB3 = new Label("Subject:");
        TextField textB3 = new TextField();
        Label labelB4 = new Label("Age:");
        TextField textB4 = new TextField();
        Label labelB5 = new Label("Contact No#:");
        TextField textB5 = new TextField();
        Label labelB6 = new Label("Country: ");
        TextField textB6 = new TextField();
        Label labelC1 = new Label("Type in the Subject:");
        TextField textC1 = new TextField();
        VBox layout = new VBox(20);
        layout.setAlignment(Pos.CENTER);
        layout.getChildren().addAll(labelA1, buttonA1, buttonA2);
        scene1 = new Scene(layout, 500, 500);
        VBox layout2 = new VBox(20);
        layout2.getChildren().addAll(labelB1, textB1, labelB2, textB2, labelB3,
                textB3, labelB4, textB4, labelB5, textB5, labelB6, textB6, buttonB2, buttonB1);
        buttonB2.setOnAction(l -> {
            try {
                BufferedWriter bw = new BufferedWriter(
                        new FileWriter("C:\\Users\\Salman\\Desktop\\Tutor Details\\output.txt", true));
                bw.write(textB1.getText()+" ");
                bw.write(textB2.getText()+" ");
                bw.write(textB3.getText()+" ");
                bw.write(textB4.getText()+" ");
                bw.write(textB5.getText()+" ");
                bw.write(textB6.getText());
                bw.newLine();
                bw.close();
                textB1.clear();
                textB2.clear();
                textB3.clear();
                textB4.clear();
                textB5.clear();
                textB6.clear();
            } catch (Exception e) {
                return;
            }
        });
        buttonC1.setOnAction(e -> {
            r.openFile();
            r.readLine();
            r.closeFile();
        });
        scene2 = new Scene(layout2, 500, 600);
        VBox layout3 = new VBox(20);
        layout3.setAlignment(Pos.CENTER);
        layout3.getChildren().addAll(buttonC2, labelC1, textC1, buttonC1);
        scene3 = new Scene(layout3, 500, 500);
        window.setScene(scene1);
        window.show();
    }
    public static void main (String[]args){
        launch(args);
    }
}

ReadTutor Class

package sample;

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


public class ReadTutor {

    public String data;
    public Scanner x;
    String a, b, c, d, e, f;

    public void openFile(){
        try {
            x = new Scanner(new File("C:\\Users\\Salman\\Desktop\\Tutor Details\\output.txt"));
        }
        catch(Exception e){
            System.out.println("couldn't find file");
        }
    }

    public void readFile(){
        while(x.hasNextLine()){
            a = x.next();
            b = x.next();
            c = x.next();
            d = x.next();
            e = x.next();
            f = x.next();

        }
    }


    public void readLine() {
        while (x.hasNextLine()) {
            data = x.nextLine();
            if (data.contains("math")) {
                System.out.println(data);
            }
            else if(data.contains("phys")){
                System.out.println(data);
            }
            else if(data.contains("comp")){
                System.out.println(data);
            }
            else if(data.contains("account")){
                System.out.println(data);
            }
        }
    }
    public void closeFile(){
        x.close();
    }
}

I basically just need to know how to search each line of a string and print the contents If it includes a word I specified.

Maybe this will help you

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
  public static void main(String[] args) {
    Pattern pattern = Pattern.compile("math", Pattern.CASE_INSENSITIVE);
    String mystr="Life is like a math if it goes to easy something is worng";
    Matcher matcher = pattern.matcher(mystr);
    boolean matchFound = matcher.find();
    if(matchFound) {
      System.out.println("Match found");
    } else {
      System.out.println("Match not found");
    }
  }
}

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