I advise you to go with Scanner instead of DataInputStream. Scanner is specifically designed for this purpose and introduced in Java 5. See the following links to know how to use Scanner.
- Java Documentation
- Java Tutorial
Example
Scanner s = new Scanner(System.in);
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
Answer from Jomoos on Stack OverflowI advise you to go with Scanner instead of DataInputStream. Scanner is specifically designed for this purpose and introduced in Java 5. See the following links to know how to use Scanner.
- Java Documentation
- Java Tutorial
Example
Scanner s = new Scanner(System.in);
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
Use BufferedReader and InputStreamReader classes.
BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
String line=buffer.readLine();
Or use java.util.Scanner class methods.
Scanner scan=new Scanner(System.in);
Videos
Say I am given this condition in a loop:
While ((i = br.readLine()) != null)
How does the system know to keep going on the scan? I’m not asking about the null- I’m aware it terminates once the file reaches a null line, but how does readLine() keep incrementing the line which is being read?
For a loop traversal, you would need a for loop with i++ for example... I don’t see any incrementing going on?
Second question: how can I look for a specific string in a file? == “” didn’t work.