Program 3 : String Class and Scanner
Explore the String class and command-line arguments by developing a program that manipulates strings and processes arguments. Additionally, demonstrate the use of varargs and the Scanner class for input.
java
import java.util.Scanner;
public class SimpleStringExplorer {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java SimpleStringExplorer <some-text>");
return;
}
String initialText = args[0];
System.out.println("Argument received: \""
+ initialText + "\"");
System.out.println("\n## String Manipulation ##");
manipulateString(initialText);
System.out.println("\n## Varargs Demonstration ##");
String sentence = joinStrings(" ", "Varargs", "is", "simple.");
System.out.println("Joined sentence: " + sentence);
System.out.println("\n## Scanner Demonstration ##");
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
scanner.close();
}
public static void manipulateString(String text) {
System.out.println("Length: " + text.length());
System.out.println("Uppercase: " + text.toUpperCase());
System.out.println("Lowercase: " + text.toLowerCase());
// Get the character at a specific index
if (!text.isEmpty()) {
System.out.println("Character at index 0: "
+ text.charAt(0));
}
// Check if the string starts with a certain prefix
System.out.println("Starts with 'Hello': "
+ text.startsWith("Hello"));
// Find the index of a character or substring
System.out.println("Index of 'J': "
+ text.indexOf('J'));
// Replace all occurrences of a character
System.out.println("Replacing 'a' with '@': "
+ text.replace('a', '@'));
// Get a portion of the string
if (text.length() > 5) {
System.out.println("Substring from index 5: "
+ text.substring(5));
}
// Demonstrate trimming whitespace from the beginning and end
String paddedText = " some spaces ";
System.out.println("Trimming '"
+ paddedText + "': '" + paddedText.trim() + "'");
}
public static String joinStrings(String separator, String... words) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < words.length; i++) {
sb.append(words[i]);
// Add the separator unless it's the last word
if (i < words.length - 1) {
sb.append(separator);
}
}
return sb.toString();
}
}
java SimpleStringExplorer "HelloJava"
Argument received: "HelloJava"
## String Manipulation ##
Length: 9
Uppercase: HELLOJAVA
Lowercase: hellojava
Character at index 0: H
Starts with 'Hello': true
Index of 'J': 5
Replacing 'a' with '@': HelloJ@v@
Substring from index 5: Java
Trimming ' some spaces ': 'some spaces'
## Varargs Demonstration ##
Joined sentence: Varargs is simple.
## Scanner Demonstration ##
Please enter your name: Alex
Hello, Alex!
Mess of a Program
java
import java.util.Scanner;
class Box {
double width, height, depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
public String toString() {
return "The dimensions are " + width
+ " by " + height + " by " + depth + ".";
}
}
public class ToStringDemo {
// Method using varargs
static void printWords(String... words) {
System.out.println("Words passed using varargs:");
for (String word : words) {
System.out.println(word);
}
}
public static void main(String[] args) {
// Box and toString
Box b = new Box(10, 12, 14);
String s = "The box b is " + b;
System.out.println("Printing b: " + b); // Converted to string using toString
System.out.println("Printing s: " + s);
// String manipulation
String s2 = "Face the failure until the failure fails to face you";
int start = 4;
int end = 37;
char[] buf = new char[end - start];
s2.getChars(start, end, buf, 0);
System.out.println("Extracted substring using getChars:");
System.out.println(buf);
// Command-line arguments
System.out.println("The number of command-line arguments: "
+ args.length);
for (int i = 0; i < args.length; i++) {
System.out.println("Argument "
+ (i + 1) + ": " + args[i]);
}
// Varargs method
printWords("Java", "String", "Scanner", "Varargs");
// Scanner input
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String input = sc.nextLine();
System.out.println("You entered: " + input);
sc.close();
}
}
Case 1: No Command-line Arguments
text
Printing b: The dimensions are 10.0 by 12.0 by 14.0.
Printing s: The box b is The dimensions are 10.0 by 12.0 by 14.0.
Extracted substring using getChars:
the failure until the failure fa
The number of command-line arguments: 0
Words passed using varargs:
Java
String
Scanner
Varargs
Enter a sentence: change is constant
You entered: change is constant
Case 2: With Command-line Arguments
text
F:\MSRIT\JAVA\Lab>java ToStringDemo Hello world
Printing b: The dimensions are 10.0 by 12.0 by 14.0.
Printing s: The box b is The dimensions are 10.0 by 12.0 by 14.0.
Extracted substring using getChars:
the failure until the failure fa
The number of command-line arguments: 2
Argument 1: Hello
Argument 2: world
Words passed using varargs:
Java
String
Scanner
Varargs
Enter a sentence: this is java lab
You entered: this is java lab