简体   繁体   中英

How can I capture recurring user inputs in a loop and then display them all at the end of the program?

I am a beginner to Java and have written a 260+ line code, menu-driven, procedural-type program. Unfortunately, I must abide by the rules that pertain to Academic Conduct of my university and I cannot paste what code I have here at this exact moment in time, although I will do my best to explain my conundrum in the hopes that more knowledgeable folk can point me in the right direction. I don't necessarily expect solutions in the form of code. My countless Internet searches have been fruitless and I'm kind of lost and frustrated, even with all my reading materials, countless search query combinations and hours poring over forums, including this one.

Basically, my program is almost finished. It's a reservations system that has a few menus asking for user input. There are a few do-while iterations and conditional statements that allow the user to return back to the main menu once they've entered their inputs, along with some basic error validation.

My main issue is with the final submenu; I enter three values using Scanner (all strings) and I can even print those three values to the console prior to being returned to the main menu. If I enter that same submenu again and enter three different inputs, it overwrites the previous set of inputs. Now, I understand that this is to be expected each time the "nextLine" method is invoked but I must capture each individual set of inputs for a reservations summary at the end of the program.

I've tried to store the values in both single and multidimensional arrays along with for (foreach?) loops but they simply fill up the entire index range with the same three values. I've also tried string concatenation, StringBuilder and ArrayLists but the continuous overwriting of those values makes it near impossible for me to achieve anything meaningful with them. Ideally, I just want to enter the values, preserve them somehow, enter and save more values and then retrieve them all at the very end of the program so I can display them using formatted Strings. The maximum number of entries I can make in the submenu is 10. At the risk of asking a vague question, what method would strike you as being the most suitable here? Once I know what tree I have to bark up, I can simply research my way to an answer. This "100 different ways of reaching the same satisfactory outcome" nature of Java - and dare I say programming languages in general - is rather overwhelming for a beginner like me.

Not sure I understand the issue, but it seems like you could add your input values to a List<String[]>

List<String[]> data = new ArrayList<>();

// get your first 3 data, add them to the list
data.add(new String[] {"value 1", "value 2", "value 3"});

// get your first 3 new data, add them to the list
data.add(new String[] {"value 4", "value 5", "value 6"});

Then you can print them out

for(String[] values: data) {
    System.out.println(values[0] + " | " + values[1] + " | " + values[2] );
}

Outputs

value 1 | value 2 | value 3
value 4 | value 5 | value 6

An ArrayList would be suitable for the situation. It allows you to add new items to it dynamically, which is exactly what you are trying to do here, isn't it?

But first, I suggest you encapsulate the "three values" that you kept talking about into a class. From what you said, they represent some information about reservations. I'll call this class Reservation :

class Reservation {
    // name these variables properly!
    private String someValue1;
    private String someValue2;
    private String someValue3;

    @Override // this returns a formatted string representation of a reservation
    public String toString() {
        return "someValue1: " + someValue1
             + "\nsomeValue2: " + someValue2
             + "\nsomeValue3: " + someValue3;
    }

    public Reservation(String someValue1, String someValue2, String someValue3) {
        this.someValue1 = someValue1;
        this.someValue2 = someValue2;
        this.someValue3 = someValue3;
    }
}

Now you can create an ArrayList<Reservation> :

// put this outside of any method
static ArrayList<Reservation> reservations = new ArrayList<>();

Whenever the user goes to the submenu, you will do something like this, right?

String someValue1 = scanner.nextLine();
String someValue2 = scanner.nextLine();
String someValue3 = scanner.nextLine();

After those lines, create a Reservation :

Reservation reservation = new Reservation(someValue1, someValue2, someValue3);

And put it into the reservations list:

reservations.add(reservation);

When you want to print all the reservations, you just:

for(Reservation r : reservations) {
    System.out.println(r);
    System.out.println();
}

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