Question

Can I please have Java code that uses standard J2SE packages to format and parse strings...

Can I please have Java code that uses standard J2SE packages to format and parse strings and streams.

Can the answer be typewritten please.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java has a rich set of API's for parsing the strings and streams. Parsing a string means that converting a string into other class types. Example: String to date, string to long,etc.

package stringparse;

import java.text.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.regex.Pattern;
import java.util.*;

public class StringParse{

public static void main(String[] args) throws ParseException {
//Parsing Strings
System.out.println("Parsing and formating String");
System.out.println("----------------------------");
String parseString = "12/04/2012";
System.out.println("Input String: " + parseString);
//string formated into dd/MM/yy
SimpleDateFormat oldDateFormat = new SimpleDateFormat("dd/MM/yy");
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date d = oldDateFormat.parse(parseString);
String output = dateFormat.format(d);
System.out.println("Parsed and formated string: " + output);
//converting string into a stream
Pattern pattern = Pattern.compile("-");
// split date by '/' by converting into stream
System.out.println("Parsing and formating Stream");
System.out.println("----------------------------");
Stream<String> dateStream = pattern.splitAsStream(output);
dateStream.forEach(System.out::println);
}
}


Add a comment
Know the answer?
Add Answer to:
Can I please have Java code that uses standard J2SE packages to format and parse strings...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please could you please amend the java code below to serialize and deserilize an object using...

    Please could you please amend the java code below to serialize and deserilize an object using FileOutputStrean and FileInputStream. Please could I have a typewritten answer.

  • Convert this java code to C++. Also i have given the file format below for reference....

    Convert this java code to C++. Also i have given the file format below for reference. The file has a name and marks separated by a white space. Want to get the name and parse numbers (marks) into a vector given as: vector<int> marks Paul 24 56 73 80 90 public StudentEntry(String line) { int nameEnd = line.indexOf(‘ ‘); name = line.substring(0, nameEnd); line=line.substring(nameEnd +1); String[ ] splitLine =line.split(“\\s”); for (int i=0; i<size; i++) { marks[i] = Integer.parseInt(splitLine[i]); } }

  • Write in Java Implement the parse method and test it by calling with three different strings...

    Write in Java Implement the parse method and test it by calling with three different strings and by printing the results. The Scanner method can be used to read values from strings, files, or System.in. We need to invoke the useDelimiter method to define what symbols can separate, or terminate, the digits of a Fraction. public static Fraction parse(String input) t Scanner s new Scanner(input) useDelimitercTVMitln"); int num s.nextlnt() int denom s.nextlnt); s.close): return new Fraction(num, denom) class Codechef static...

  • Can I have a quick sort program -java code- , I need really good one please...

    Can I have a quick sort program -java code- , I need really good one please ??

  • How do I Write a java code where it records the user inputs of strings ....

    How do I Write a java code where it records the user inputs of strings . <orderString>

  • I need help with the following question please in JAVA code. Hash code is used to...

    I need help with the following question please in JAVA code. Hash code is used to “encode” general keys into integers. One approach of creating a hash code is to use Java's hashCode() method. The hashCode() method is implemented in the Object class and therefore each class in Java inherits it. The hash code provides a numeric representation of an object (this is somewhat similar to the toString method that gives a text representation of an object). Write a program...

  • I have written java program that requires the reading of another written java code to run...

    I have written java program that requires the reading of another written java code to run in Netbeans but I'm not sure how. Can someone please direct me? Screenshots are welcomed

  • A College wants to build their Student Information Systems (SIS). Whenever a new Student is added...

    A College wants to build their Student Information Systems (SIS). Whenever a new Student is added to the college, the details need to be recorded in a text file. Student information officer should be able to use the SIS to retrieve information from the text file and display it on the screen. The system also needs to be able to format or parse strings retrieved from the text file. I am using Eclipse IDE to write java code. Can I...

  • java find and replace code pls help me We write code that can find and replace...

    java find and replace code pls help me We write code that can find and replace in a given text file. The code you write should take the parameters as command line arguments: java FindReplace -i <input file> -f "<find-string>" -r "<replace-string> -o <output file> *question mark(?) can be used instead of any character. "?al" string an be sal ,kal,val *In addition, a certain set of characters can be given in brackets.( "kng[a,b,f,d,s]ne" string an be kngane,hngbne,kangfne,kangdne,kangsne So, all you...

  • JAVA Project Please have the code written in JAVA This is a game I am trying...

    JAVA Project Please have the code written in JAVA This is a game I am trying to develop. Please let me know if its possible and what the base code is. Prompt User “Hello! Welcome to the game.” Prompt User “To begin the game, please enter a letter” Random word generator, will set the String to a new word After the user inputs a letter, the computer will enter a loop Once in the loop the computer will test each...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT