Scanner scanner = new Scanner(System.in);
int c = 0;
while(scanner.hasNext()){
System.out.println(++c + " " + scanner.nextLine());
}
scanner.close();
// use while instead of normal for loop.
// If you need to read a file than BufferReader is the best way to do it, as explained above.