Skip to content

Commit 0ac8d3a

Browse files
committed
String reading methods in java
1 parent 3322c2a commit 0ac8d3a

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
5+
public class Main {
6+
public static void main(String[] args) throws IOException {
7+
// constructing a BufferedReader obj
8+
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
9+
10+
System.out.print("Enter a string: ");
11+
String str = reader.readLine();
12+
13+
System.out.println("You entered: ");
14+
System.out.println("\t"+str);
15+
}
16+
17+
}

Input method/Console Method/Main.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Main {
2+
public static void main(String[] args) {
3+
// no constructor is needed
4+
5+
System.out.print("Enter your string: ");
6+
String str = System.console().readLine();
7+
8+
System.out.println("You entered: ");
9+
System.out.println("\t"+str);
10+
11+
}
12+
}

Input method/Scanner Class/Main.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.util.Scanner;
2+
3+
public class Main{
4+
public static void main(String[] args) {
5+
// constructing a Scanner class obj
6+
Scanner scan = new Scanner(System.in);
7+
8+
System.out.print("Enter a string: ");
9+
String str = scan.nextLine();
10+
11+
System.out.println("You entered: ");
12+
System.out.println("\t"+str);
13+
}
14+
}

Input method/Swing Method/Main.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// GUI
2+
import javax.swing.JOptionPane;
3+
4+
public class Main {
5+
public static void main(String[] args) {
6+
// no constructor is needed
7+
8+
String str = JOptionPane.showInputDialog("Enter a string:");
9+
JOptionPane.showMessageDialog(null, "You entered: " + str);
10+
}
11+
}

0 commit comments

Comments
 (0)