StringBuffer , StringBuidler - stringbuffer

StringBuidler sb="ram"
StringBuffer sf = "ram"
Both throws comile time error .
Please explain Why??????

There's no promotion from String to StringBuilder/Buffer. Try
StringBuilder = new StringBuilder("ram")

Because StringBuilder and StringBuffer isn't strings.
You need to write
sb = new StringBuilder("ram")

Perhaps because it is "StringBuilder" not "StringBuidler"?
Or maybe because you need a semi-colon between the two lines?
Or that "ram" is a string and not a StringBuilder or StringBuffer?

Related

SimpleDateFormat in J2ME

I Make a J2ME Application and I just want to print the system date and time, and when I search I found this class that used for that purpose, but I didn't find it in J2ME to use it.
How can I do that?
Is this enough for what you need?
Calendar cal = Calendar.getInstance();
StringBuffer date = new StringBuffer();
StringBuffer time = new StringBuffer();
date.append(cal.get(Calendar.DATE)).append('/');
date.append(cal.get(Calendar.MONTH) +1).append('/');
date.append(cal.get(Calendar.YEAR));
time.append(cal.get(Calendar.HOUR_OF_DAY)).append(':');
time.append(cal.get(Calendar.MINUTE)).append(':');
time.append(cal.get(Calendar.SECOND));

How do you convert a string into an InputStream

I want to use the SaxParser to parse a string that contains XML code I got using the HTTPUtils.
Can you tell me how to convert the string I got from the HTTPUtils into an InputStream?
I tried to do this but it won't let me compile it:
Sub JobDone (Job As String)
Dim strStringFromWebSite As String
Dim in As InputStream
If HttpUtils.IsSuccess(strUrlToCall) Then
strStringFromWebSite = HttpUtils.GetInputStream(strUrlToCall)
in = strStringFromWebSite
XmlParser.Parse(in, "Parser")
in.Close
Else
ToastMessageShow("There was a problem getting a response from the web site.", False)
End If
End Sub
I get the error on this line of code:
in = strStringFromWebSite
Thanks.
HttpUtils.GetInputStream returns an InputStream (as it's name indicates). Why are you trying to convert it to a string and then back to an InputStream? It seems to me you can go direct and cut out the middleman. :)
in = HttpUtils.GetInputStream(strUrlToCall)
XMLParser.Parse(in, "Parser")

String codification to Twitter

I'm developing a program that sends tweets.
I have this piece of code:
StringBuilder sb = new StringBuilder("Recomendo ");
sb.append(lblName.getText());
sb.append(" no canal "+lblCanal.getText());
sb.append(" no dia "+date[2]+"/"+date[1]+"/"+date[0]);
sb.append(" às "+time[0]+"h"+time[1]);
byte[] defaultStrBytes = sb.toString().getBytes("ISO-8859-1");
String encodedString = new String(defaultStrBytes, "UTF-8");
But When I send it to tweet I get the "?" symbol or other strage characters because of the accents like "à" . I've also tried with only
String encodedString = new String(sb.toString().getBytes(), "UTF-8"); //also tried with ISO-8859-1
but the problem remains...
You are trying to read Latin-1 as UTF-8. That's why you are getting question marks.
Try to send your string as is,
String encodedString = sb.toString();
The charset should be taking care when you send the message to Tweet. If URL encoding is required, you would do something like
String msg = URLEncoder.encode(encodedString, "UTF-8");

How to use MongoRegex (MongoDB C# Driver)

Has anyone have any idea how to use MongoRegex for the document search?
I attempted this, but returns nothing back:
var spec = new Document();
spec.Add("Name", new MongoRegex("/" + searchKey + "*/", "i"));
collection.Find(spec)
Wondering why it doesn't work, I tried to execute following command from the console:
db.things.find({"Name":/john*/i}) /* WORKS */
db.things.find({"Name":"/john*/i"}) /* DOESN'T WORK */
Is that possible that the driver applies double quotation to the regex?
Thanks..
you just want a simple prefix query. Your regex is then ^ + searchKey. Also, this form will allow mongodb to use an index on Name.
var spec = new Document("Name", new MongoRegex(string.Format("^{0}",searchKey), "i"));
collection.Find(spec)
I think you need to not include the "/"s in C#, i.e.,
spec.Add("Name", new MongoRegex(searchKey + "*", "i"));
After digging the source code, I finally found the answer :)
var spec = new Document();
spec.Add("Name", new MongoRegex(".*" + searchKey + ".*", "i"));
collection.Find(spec)

How can I get the current stack trace in Java?

How do I get the current stack trace in Java, like how in .NET you can do Environment.StackTrace?
I found Thread.dumpStack() but it is not what I want - I want to get the stack trace back, not print it out.
You can use Thread.currentThread().getStackTrace().
That returns an array of StackTraceElements that represent the current stack trace of a program.
StackTraceElement[] st = Thread.currentThread().getStackTrace();
is fine if you don't care what the first element of the stack is.
StackTraceElement[] st = new Throwable().getStackTrace();
will have a defined position for your current method, if that matters.
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
System.out.println(ste + "\n");
}
Thread.currentThread().getStackTrace();
is available since JDK1.5.
For an older version, you can redirect exception.printStackTrace() to a StringWriter() :
StringWriter sw = new StringWriter();
new Throwable("").printStackTrace(new PrintWriter(sw));
String stackTrace = sw.toString();
Tony, as a comment to the accepted answer, has given what seems to be the best answer which actually answers the OP's question:
Arrays.toString(Thread.currentThread().getStackTrace()).replace( ',', '\n' );
... the OP did NOT ask how to get a String from the stack trace from an Exception. And although I'm a huge fan of Apache Commons, when there is something as simple as the above there is no logical reason to use an outside library.
You can use Apache's commons for that:
String fullStackTrace = org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace(e);
On android a far easier way is to use this:
import android.util.Log;
String stackTrace = Log.getStackTraceString(exception);
Another solution (only 35 31 characters):
new Exception().printStackTrace();
new Error().printStackTrace();
To get the stack trace of all threads you can either use the jstack utility, JConsole or send a kill -quit signal (on a Posix operating system).
However, if you want to do this programmatically you could try using ThreadMXBean:
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
ThreadInfo[] infos = bean.dumpAllThreads(true, true);
for (ThreadInfo info : infos) {
StackTraceElement[] elems = info.getStackTrace();
// Print out elements, etc.
}
As mentioned, if you only want the stack trace of the current thread it's a lot easier - Just use Thread.currentThread().getStackTrace();
I suggest that
Thread.dumpStack()
is an easier way and has the advantage of not actually constructing an exception or throwable when there may not be a problem at all, and is considerably more to the point.
Silly me, it's Thread.currentThread().getStackTrace();
In Java 9 there is a new way:
public static void showTrace() {
List<StackFrame> frames =
StackWalker.getInstance( Option.RETAIN_CLASS_REFERENCE )
.walk( stream -> stream.collect( Collectors.toList() ) );
for ( StackFrame stackFrame : frames )
System.out.println( stackFrame );
}
Getting stacktrace:
StackTraceElement[] ste = Thread.currentThread().getStackTrace();
Printing stacktrace (JAVA 8+):
Arrays.asList(ste).forEach(System.out::println);
Printing stacktrage (JAVA 7):
StringBuilder sb = new StringBuilder();
for (StackTraceElement st : ste) {
sb.append(st.toString() + System.lineSeparator());
}
System.out.println(sb);
To string with guava:
Throwables.getStackTraceAsString(new Throwable())
I have a utility method that returns a string with the stacktrace:
static String getStackTrace(Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
t.printStackTrace(pw);
pw.flush();
sw.flush();
return sw.toString();
}
And just logit like...
...
catch (FileNotFoundException e) {
logger.config(getStackTrace(e));
}
try {
}
catch(Exception e) {
StackTraceElement[] traceElements = e.getStackTrace();
//...
}
or
Thread.currentThread().getStackTrace()
Maybe you could try this:
catch(Exception e)
{
StringWriter writer = new StringWriter();
PrintWriter pw = new PrintWriter(writer);
e.printStackTrace(pw);
String errorDetail = writer.toString();
}
The string 'errorDetail' contains the stacktrace.
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
The last element of the array represents the bottom of the stack, which is the least recent method invocation in the sequence.
A StackTraceElement has getClassName(), getFileName(), getLineNumber() and getMethodName().
loop through StackTraceElement and get your desired result.
for (StackTraceElement ste : stackTraceElements )
{
//do your stuff here...
}
You can use jstack utility if you want to check the current call stack of your process.
Usage:
jstack [-l] <pid>
(to connect to running process)
jstack -F [-m] [-l] <pid>
(to connect to a hung process)
jstack [-m] [-l] <executable> <core>
(to connect to a core file)
jstack [-m] [-l] [server_id#]<remote server IP or hostname>
(to connect to a remote debug server)
Options:
-F to force a thread dump. Use when jstack <pid> does not respond (process is hung)
-m to print both java and native frames (mixed mode)
-l long listing. Prints additional information about locks
-h or -help to print this help message
I used answers from above and added formatting
public final class DebugUtil {
private static final String SEPARATOR = "\n";
private DebugUtil() {
}
public static String formatStackTrace(StackTraceElement[] stackTrace) {
StringBuilder buffer = new StringBuilder();
for (StackTraceElement element : stackTrace) {
buffer.append(element).append(SEPARATOR);
}
return buffer.toString();
}
public static String formatCurrentStacktrace() {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
return formatStackTrace(stackTrace);
}
}
For people, who just want to get the current stacktrace to their logs, I would go with:
getLogger().debug("Message", new Throwable());
Cheers
You can also use exception to print stack instead of taking pain of putting new line char:
Exception e = new Exception();
e.setStackTrace(Thread.currentThread().getStackTrace());
loggr.info(e);
This is an old post, but here is my solution :
Thread.currentThread().dumpStack();
More info and more methods there :
http://javarevisited.blogspot.fr/2013/04/how-to-get-current-stack-trace-in-java-thread.html