1

// 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.");
JJJ
  • 32,902
  • 20
  • 89
  • 102
cscontrol
  • 73
  • 7
  • the System.out.println("1920: " + input.nextInt()); works but if I call it again to output.println, it moves the curser to the next Int in file – cscontrol Jul 30 '18 at 15:43
  • Your code makes it print the first Int on console, then the SECOND Int to output.println. I want it to print the first Int for both – cscontrol Jul 30 '18 at 15:47

1 Answers1

1

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

Martín Zaragoza
  • 1,717
  • 8
  • 19