0

This program asks the user to input a number and then brings back the details from an list. how do I do this?

         do {
          Scanner in = new Scanner(System.in);             
            System.out.println("Enter Number <terminated by 1>");

             }  while (!input.equals("-1"));
                System.out.println("Session Over");

        } catch (Exception e) {   
            System.out.println(e);
        }
    }
}

output:

Enter Number <terminated by 1>
123456
Person Number: 12
  • 1
    Not that it solves anything but you shouldn't create many instances of Scanner which will handle `System.in`. So move line creating it before your `do{..}` loop. – Pshemo Mar 14 '16 at 16:10
  • Call `e.printStacktrace()` to know which instruction throws the Exception. – Arnaud Mar 14 '16 at 16:12
  • is not your real code. It is not compiling. – ByeBye Mar 14 '16 at 16:15
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – ryanyuyu Mar 14 '16 at 16:57

4 Answers4

1

while (input != -1); would be the right way to compare two integer values.

Mathews Mathai
  • 1,707
  • 13
  • 31
Phil
  • 29
  • 2
0

In java you can't test variables in do-while loop when variable is inside loop:

do {
    int i = 10;
} while (i > 5);

will not compile.

Also, int input; doesn't have equals method as int type is primitive.

ByeBye
  • 6,650
  • 5
  • 30
  • 63
0

First remove the Scanner initialization from the loop and put it in the try block, as Pshemo pointed out.

try {
    Scanner in = new Scanner(System.in);
do {
    yada yada yada
}

Then try wrapping your negation in your while loop as in:

while (!(input.equals("-1")));
Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
nur-sh
  • 203
  • 1
  • 11
0

Pro Tip: use an IDE.

Your code is incorrect for at least two reasons:

  1. your int input is defined inside the do block, so it is not visible after it closes
  2. you use .equals on a primitive type

Correct your code as follows:

public class Client {

    public static void main(String[] arg) {
        Client c = new Client();
        c.run();
    }

    private void run() {
        StudentData p = new StudentData();
        List<StudentDetailsType> personDetailsList = (List<StudentDetailsType>) p.getList();


        // input defined outside the do block so it is visible in while clause
        int input;
        // declare the scanner just one time here, so it will be closed automatically as the try/catch block ends
        try(Scanner in = new Scanner(System.in)) {
            do {
                System.out.println("Enter 6 digit Student Number <terminated by -1>");
                input = in.nextInt();
                for (StudentDetailsType q : personDetailsList) {

                    if (q.getStudentNumber() == input) {
                        System.out.println("Student Number: " + q.getStudentNumber() + "\n" + "Student Name: "
                                + q.getStudentName() + "\n" + "Result 1: " + q.getResult1() + "\n" + "Result 2: "
                                + q.getResult2() + "\n" + "Result 3: " + q.getResult3());
                        break;
                    }
                }
            } while (input != -1); // use an int comparison (!= that means not equals)

            System.out.println("Session Over");

        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
Francesco Pitzalis
  • 2,042
  • 1
  • 16
  • 20