简体   繁体   中英

same input calling two different methods in java

I'm trying to implement the main method in java for a KWIC. The issue I'm having is that I have to ask the user if they want to write the input from the console/file and write the output to the console/file. The first time asking the user for console/file works fine when I have to read, but when I ask them again for the output I believe it goes back into the first If condition. Here is the code for reference.

try {

        System.out.println("Please enter FILE to input from file or CONSOLE to input from console:");
        String userInput = "";
        while ((userInput = scannerWrapper.nextLine()) != "-1") {

            if (userInput.equals("CONSOLE")) {

                System.out.println("Please enter FILE to output from file or CONSOLE to output from console:");
                List<String> cshiftConsole = circularShifter.shiftLines(inputFromConsole.read());

                if (userInput.equals("CONSOLE")) {

                    System.out.println("Please enter lines to add, then enter -1 to finish:");

                    // Console

                    cshiftConsole = alphabetizer.sort(cshiftConsole);
                    outputToConsole.write(cshiftConsole);

                    for (String element : cshiftConsole) {
                        System.out.println(element);
                    }
                }

            }

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

This is my console output

Please enter FILE to input from file or CONSOLE to input from console:

CONSOLE

Please enter FILE to output from file or CONSOLE to output from console:

CONSOLE

Software Architecture

-1

Please enter lines to add, then enter -1 to finish:

Architecture Software

CONSOLE

Software Architecture

After the second CONSOLE(userInput) I should be asked to enter the lines, But this is taking CONSOLE as the input I want to circularly shift. Any help would be great thank you.

You have some issues in your code, and if I understand correctly you want to decide where to INPUT from, where to OUTPUT to and get a couple of Strings into a List .

  1. (userInput = scannerWrapper.nextLine()) != "-1" You compare Strings in Java using .equals(...) as you did in your code further below.

  2. Every time you're asking your user where he wants to input from, you just have to read it once, so instead of having it inside a while do it on an if .

  3. You create your list on every iteration, have it as an instance member instead.

  4. On every iteration you're printing your objects, wait until the user stops adding items first (they type -1 )

A better approach could be like this:

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

public class InputCycle {
    private Scanner scanner;
    private static final String CONSOLE = "CONSOLE";
    private static final String EXIT_CODE = "-1";
    private List<String> list;
    
    public static void main(String[] args) {
        new InputCycle().readAndWrite();
    }
    
    private void readAndWrite() {
        scanner = new Scanner(System.in);
        list = new ArrayList<String>();
        
        System.out.println("INPUT FROM CONSOLE / FILE");
        String input = scanner.nextLine(); // Read where to input from
        
        if (input.equals(CONSOLE)) { 
            System.out.println("OUTPUT TO CONSOLE / FILE");
            
            String output = scanner.nextLine(); // Read where to output to
            
            if (output.equals(CONSOLE)) {
                System.out.println("WRITE LINES (" + EXIT_CODE + " TO EXIT)");
                String line = "";
                do {
                    line = scanner.nextLine(); // Read every line
                    if (!line.equals(EXIT_CODE)) {
                        list.add(line);
                    }
                } while (!line.equals(EXIT_CODE));
            } else { // Write to a file with your own code
                System.out.println("Writing to a file");
            }
        } else { //Read from a file
            System.out.println("Reading from a file");
        }
        
        System.out.println("ALL LINES: ");
        
        list.forEach(line -> System.out.println(line)); //Print all the lines user wrote
    }
}

That has this output:

INPUT FROM CONSOLE / FILE
CONSOLE
OUTPUT TO CONSOLE / FILE
CONSOLE
WRITE LINES (-1 TO EXIT)
FOO
BAR
BANANA
-1
ALL LINES: 
FOO
BAR
BANANA

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