How to read multiple lines at a time and display them?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to read multiple lines at a time and display them??

8th Sep 2016, 2:55 PM
chundru srilekha
chundru srilekha - avatar
2 Answers
+ 2
Scanner sc = new Scanner(System.in); System.out.println("Enter your input:"); String str = sc.nextLine(); System.out.println("Enter another input:"); String str2 = sc.nextLine(); System.out.println(str + " " + str2);
8th Sep 2016, 3:26 PM
Zen
Zen - avatar
+ 2
import java.io.File; import java.util.Scanner; import java.util.Formatter; public class Me { public static void main(String[] args) { try { Formatter f = new Formatter("test.txt"); f.format("%s %s %s", "1", "John", "Smith \r\n"); f.format("%s %s %s", "2", "Amy", "Brown"); f.close(); File x = new File("test.txt"); Scanner sc = new Scanner(x); while(sc.hasNext()) { System.out.println(sc.next()); } sc.close(); } catch (Exception e) { System.out.println("Error"); } } }
8th Sep 2016, 3:59 PM
Girish Grover