0

I'm fairly new to programming so I'm currently stuck on figuring out how to make my code work cleaner. As of right now there are some random dummy lines in my code to make sure i dont skip part certain part of the loops. I was wondering if there are any ways to avoid it.

public static void main(String arg[]) {

        String CandidateID;
        String Name;
        String Option1;
        int Test1;
        int Test2;
        String dummy;
        Scanner sc = new Scanner(System.in);
        ArrayList<TestResult> StudentResults = new ArrayList<TestResult>();
        do {
            dummy = sc.nextLine();
            System.out.println("Enter student data? y/n");
            Option1 = sc.nextLine();
            if (Option1.equals("y")) {
                System.out.println("Enter Candidate ID:");
                CandidateID = sc.nextLine();
                System.out.println("Enter Name:");
                Name = sc.nextLine();
                System.out.println("Enter Test 1:");
                Test1 = sc.nextInt();
                System.out.println("Enter Test 2:");
                Test2 = sc.nextInt();
                TestResult TestResult = new TestResult(CandidateID, Name, Test1, Test2);
                StudentResults.add(TestResult);
            }
        }
        while (!Option1.equals("n"));
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • You haven't really described what happens without that in enough detail for us to help you. (As an aside, I'd also *strongly* advise you to start following Java naming conventions...) – Jon Skeet Apr 21 '22 at 11:33
  • You are mixing use of nextLine() and nextInt(). These handle the position of the internal cursor differently. You may find [this question](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) useful. – vsfDawg Apr 21 '22 at 12:17

1 Answers1

0

If you don't use the value inside the dummy variable, you can just execute sc.nextLine(); without assigning its return value to a variable. It'll have the same effect, because the function is still being called.

benicamera
  • 750
  • 1
  • 7
  • 24