Using a scanner in a non

I have a code that searches for words in a text file, but the scanner that runs it is located in the public static void main(String[] args) for the class. I need to be able to move the scanner into a string, so that rather than inputting the word that I want to search for in the code manually as a word in quotes, I can find it by referencing the string in another class's main method, with a constructor that takes in words. However, when I try to move the scanner out of main and into a new public string, I have to change System.out.println(arr[0]) to return arr[0], and the program won't run unless I change the string type to static. When I do that, nothing comes out in the console, as opposed to the correct output being displayed when run in main.
Here is my code: import java.io.FileInputStream; import java.util.ArrayList; import java.util.List; import java.util.Scanner;

public class Database {
    public String arr45;
    public String hi(String firstName, String lastName){
Scanner theScanner = null;




    try{    
     theScanner = new Scanner(new FileInputStream("Cast.txt"));}

    catch(Exception ex){
        ex.printStackTrace();
    }

    while( theScanner.hasNext()){
    String movie = theScanner.nextLine();
    String[] arr = movie.split("/");

    if(movie.contains(lastName + ", " + firstName))
    {

        theScanner.close();
        String arr45 = arr[0];  


    }

    }
    return arr45;
    }
    public static void main( String[] args ){


}
    }

This code works when everything inside string is in main, and

if(movie.contains(lastName + ", " + firstName))

has the actual name that I'm searching for in quotes in the parentheses and if return is replaced with

System.out.println.  

I've tried a lot of things but I can't seem to figure out how to turn the original code into a string that takes in two variables and does the same thing. Below is the original code that worked correctly:

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Database {
    public static String arr2 = "";

    public static void main( String[] args ){
    Scanner theScanner = null;




    try{    
     theScanner = new Scanner(new FileInputStream("Cast.txt"));}

    catch(Exception ex){
        ex.printStackTrace();
    }

    while( theScanner.hasNext()){
    String movie = theScanner.nextLine();
    String[] arr = movie.split("/");

    if(movie.contains("Hanks, Tom"))
    {

    System.out.println(arr[0]); 
    }
    }

    theScanner.close();
    }

}

Last thing- for sure. Probably. I was required to make a method that would search for all the actors with a certain word in their name and I got it to work, but it only runs for one line of text. Please tell me how I can get this to go to the next line and do it again when the line is finished:

        while( theScanner6.hasNextLine()){
        String movie = theScanner6.nextLine();
        String[] arr = movie.split("/");

            for(int i = 1; i< 400; i++){

                    if(arr[i].contains(wordD)){
                        int placeholder = i;
                        String[] firstW = arr[placeholder].split(","); 
                         movie = theScanner6.nextLine();

                        System.out.println(firstW[1] + " " + firstW[0]);
                        arr45 = arr[placeholder];   

                    }

            }


        }
        while( !theScanner6.hasNextLine()){
            theScanner6.close();
        }

        return arr45;
        }

If I understand your question first instantiate your Database then call the method,

public static void main( String[] args ){
    Database db = new Database();
    db.hi("first", "last");
}

Also,

String arr45 = arr[0];  

should be

arr45 = arr[0];  

or you shadow the field defined earlier (and then the shadow goes out of scope).

链接地址: http://www.djcxy.com/p/96114.html

上一篇: 如何在2dArray循环中的一行中插入多个输入

下一篇: 使用非扫描仪