Write a new Java interface called DelimitedTextIO with the following methods: St
ID: 3673404 • Letter: W
Question
Write a new Java interface called DelimitedTextIO with the following methods:
String toText(char delimiter) - This method returns a String containing all the data of the implementing class as text and with each element separated by the provided delimiter.
void toObject(Scanner input) - This method uses the provided Scanner input to parse delimited text representing the data for the implementing class and initializes the objects instance variables with the parsed values. The delimiter to use must be specified for the Scanner input before calling this method.
Explanation / Answer
/*
chegg author
*/
/**
*
* @author CHEGG
*/
import java.util.Scanner;
interface DelimitedTextIO {
//Methods
//interface contains public abstract methods
public String toText(char delimiter);//abstract and public method
public void toObject(Scanner input);//abstract and public method
}
class DelimitedText implements DelimitedTextIO {
public String toText(char delimiter) {
String str = "vinod-25-male-30000";
String[] temp;
/* delimiter */
/* given string will be split by the argument delimiter provided. */
temp = str.split("-");
/* print substrings */
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i]);
str = temp[i];
}
System.out.println("");
return str;
}
public void toObject(Scanner input) {
Scanner scan = new Scanner("Anna Mills-Female-18");
// initialize the string delimiter
scan.useDelimiter("-");
System.out.println("The delimiter use is " + scan.delimiter());
// Printing the tokenized Strings
while (scan.hasNext()) {
System.out.print(scan.next());
}
// closing the scanner stream
scan.close();
}
}
public class DelimitedIODemo {//driver program
public static void main(String[] args) throws ClassNotFoundException {
DelimitedText D = new DelimitedText();
System.out.println("toText method");
D.toText('-');
Scanner sc = new Scanner(System.in);
System.out.println("toObject method");
D.toObject(sc);
}
}
output
toText method
vinod25male30000
toObject method
The delimiter use is -
Anna MillsFemale18
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.