Java please Write a program that accepts a string using the nextline() method. T
ID: 3682657 • Letter: J
Question
Java please
Write a program that accepts a string using the nextline() method. The input string consists of two words separated by a space. The program then writes the same two words to the screen in the reverse order, but separated by a comma plus a space. (4 marks)
Example input:
Expected output:
Use theandmethods in your program
Write a program that prints the following table using the System.out.printf() method. (4 marks)
Item Unit Price Quantity Total Price
Laptop 345.00 10 3450.00
Printer 113.00 2 226.00
Use the following string for the format for the 1st line:
Use the following string for the format for the 2nd and 3rd lines:
Explanation / Answer
Question 1:
Answer:
public class Test6 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println("Please Enter the String : ");
String s = in.nextLine();
String s1[] = s.split("\s+");
System.out.println(s1[1]+", "+s1[0]);
}
}
Output:
Please Enter the String :
Sachin Tendulkar
Tendulkar, Sachin
Question 2:
Answer
StringFormat.java
public class StringFormat {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String heading1 = "Item";
String heading2 = "Unit Price";
String heading3 = "Quantity";
String heading4 = "Total Price";
System.out.printf( "%-9s %11s %11s %16s %n", heading1, heading2, heading3, heading4);
System.out.printf( "%-9s %11.2f %11d %16.2f %n", "Laptop", 345.00, 10, 3450.00);
System.out.printf( "%-9s %11.2f %11d %16.2f %n", "Printer", 113.00, 2, 226.00);
}
}
Output:
Item Unit Price Quantity Total Price
Laptop 345.00 10 3450.00
Printer 113.00 2 226.00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.