Search code examples
javaprintstream

Printstream input to a text file


// I'm searching for an Int in a text file, displaying that Int on console, creating a text file to print that Int. I get this instead of an Int:

java.util.Scanner[delimiters=\p{javaWhitespace}+][position=666][match valid=true][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]1920: ,
public static void findName(Scanner input, String name) throws FileNotFoundException {

        boolean find = false;
        while (find == false && input.hasNext()) {
            String search = input.next();

            if (search.startsWith(name) && search.endsWith(name)) {
                PrintStream output = new PrintStream(new File(name + ".txt"));

                output.println(name + ",");

                System.out.println("1920: " + input.nextInt());
                output.println("1920: " + input + ","); // need help here. HOW DO I GET THIS TO PRINT THE SAME INT, NOT GO TO THE SECOND INT?
                System.out.println("1930: " + input.nextInt());
                output.println("1930: " + input + ",");
                System.out.println("1940: " + input.nextInt());
                output.println("1940: " + input + ",");
                System.out.println("1950: " + input.nextInt());
                output.println("1950: " + input + ",");

                System.out.println("1960: " + input.nextInt());
                output.println("1960: " + input + ",");
                System.out.println("1970: " + input.nextInt());
                output.println("1970: " + input + ",");
                System.out.println("1980: " + input.nextInt());
                output.println("1980: " + input + ",");
                System.out.println("1990: " + input.nextInt());
                output.println("1990: " + input + ",");
                System.out.println("2000: " + input.nextInt());
                output.println("2000: " + input);

                find = true;

            }
        }
        if (find == false) {
            System.out.println("name not found.");

Solution

  • you're printing the input object instead of the int into the file.

    try this:

    int next = input.nextInt()
    System.out.println("1920: " + next );
    output.println("1920: " + next + ",");
    ... 
    

    Hope this helps