Java Programming Help please? Two codes are posted below: Question : Write code
ID: 3680126 • Letter: J
Question
Java Programming Help please? Two codes are posted below:
Question: Write code that uses the input string stream inSS to read input data from string userInput, and updates variables userMonth, userDate, and userYear. Sample output if userinput is "Jan 12 1992":
Code provided with question:
import java.util.Scanner;
public class StringInputStream {
public static void main (String [] args) {
Scanner inSS = null;
String userInput = "Jan 12 1992";
inSS = new Scanner(userInput);
String userMonth = "";
int userDate = 0;
int userYear = 0;
/* Your solution goes here */
System.out.println("Month: " + userMonth);
System.out.println("Date: " + userDate);
System.out.println("Year: " + userYear);
return;
}
}
Question Number Two)
Write code that inserts userItems into the output string stream itemsOSS until the user enters "Exit". Each item should be followed by a space. Sample output if user input is "red purple yellow Exit":
Code provided with question:
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.StringWriter;
public class StringStreamOutput {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
String userItem = "";
StringWriter itemCharStream = new StringWriter();
PrintWriter itemsOSS = new PrintWriter(itemCharStream);
System.out.println("Enter items (type Exit to quit):");
userItem = scnr.next();
while (!userItem.equals("Exit")) {
/* Your solution goes here */
userItem = scnr.next();
}
userItem = itemCharStream.toString();
System.out.println(userItem);
return;
}
}
Explanation / Answer
Hi, I have updated code where it requires. Please find the highlighted lines.
Question 1:
import java.util.Scanner;
public class StringInputStream {
public static void main (String [] args) {
Scanner inSS = null;
String userInput = "Jan 12 1992";
inSS = new Scanner(userInput);
String userMonth = "";
int userDate = 0;
int userYear = 0;
/* Your solution goes here */
String str = inSS.nextLine();
userMonth = str.substring(0,3);
userDate = Integer.parseInt(str.substring(4,6));
userYear = Integer.parseInt(str.substring(7));
System.out.println("Month: " + userMonth);
System.out.println("Date: " + userDate);
System.out.println("Year: " + userYear);
return;
}
}
Output:
Month: Jan
Date: 12
Year: 1992
Question 2:
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.StringWriter;
public class StringStreamOutput {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
String userItem = "";
StringWriter itemCharStream = new StringWriter();
PrintWriter itemsOSS = new PrintWriter(itemCharStream);
System.out.println("Enter items (type Exit to quit):");
userItem = scnr.next();
while (!userItem.equals("Exit")) {
/* Your solution goes here */
itemsOSS.print(userItem+" ");
userItem = scnr.next();
}
userItem = itemCharStream.toString();
System.out.println(userItem);
return;
}
}
Output:
Enter items (type Exit to quit):
Red
Yellow
Blue
Green
Purple
Exit
Red Yellow Blue Green Purple
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.