I trying to learn how to write formatted strings in java for console apps. but, no one really explained this subject very well so it gets a bit messy.
I need to know where to start, i can't learn it from java doc (The doc explains this topic really terribly).
So, i surfed a lot in Wikipedia values like:
Template Languages,
scanf blala,
Data models.
I just can't understand even what this subject is?, why do we need it? and just why...
and what "fomat" / "formatted" means? design?
By the way, printf and scanf are string formats?
Related
I have been going through the Java interview questions asked by my company and came across one that I can't seem to find the solution.
Here is the question:
Please write a method (function) accepting as single parameter a
string and reversing the order of the words in this string.
The " " is the word separator and any other char is considered as being part of a word. In order to simplify, please consider that there is always one space between the words.
Important - You are NOT allowed to use other strings or arrays or other data structures containing several elements - just plain atomic variables such as integers, chars etc.
Also, it is not allowed to use any other language specific string function other than the function giving you the length of the string.
Expected result:
"hello my beautiful world" -> "world beautiful my hello"
So, I can't use: chars[], str.split(), str.charAt(), str.substring(), StringBuilder, another declaration of String.
Should I use recursion to do it?
Since, String is Immutable and uses encapsulation,
There is no solution to your problem. You can't update the values directly, no setters are available and without the access to the getters (since you can only use .length), you can't read the value.
So, I would suggest to respond that Immutability and encapsulation prevent you from doing so.
In real life as a software engineer, you'll sometimes be asked to do things that are technically impossible or even nonsensical. Sometimes the person asking will be someone important like your boss or a big customer.
If someone actually asks you this interview question, then you're in one of those situations. That makes this question pretty interesting, and you might want to figure out what the best way to answer really is.
If someone asked me, this is how I would answer, and as an interviewer, this is the kind of answer I would award the most points for:
1) Explain how it's technically impossible to meet the requirements, but do it without making me feel stupid. This shows diplomacy.
2) Figure out what I really want. In this case, the interviewer probably wants to see if you know how to reverse the words in a string using low-level operations. This is a perfectly reasonable C language question, for example. Figuring out what the interviewer really wants shows experience and judgement.
3) Provide an answer that gives me what I want. Write this method in Java, but take a StringBuilder instead of a string, and call only length(), charAt(), and setCharAt(). This shows the expertise that the interviewer wants to see.
Suppose I tell my Java program to find the subject of the sentence:
I enjoy spending time with my family.
My program should output:
Tell me more about your family.
How would it go about doing this?
EDIT
I could do this by having an array of String and have that filled with every noun in the English dictionary, but is there a simpler way?
This is way too open-ended a question. But a good place to start would be to learn about Natural Language Processing concepts and then look at using a framework like CoreNLP. It breaks down sentences into a parse tree and you can use this to identify parts of speech and things like the subject of a sentence. This is probably your best bet if you want a reasonably-reliable method.
I am quite confused here. Today I came across this concept of AttributedString. But I am not able to find the exact difference for String and AttributedString. And more ever I am not able to convert this attributed String to String using the toString(). Can anyone help me with this?
EDIT 1
Actually I want to provide Kerning attrribute for my text. But I don't know how to do it. Can anyone help me?
A String is a holder for some consecutive list of characters, usually known as "text". "Hello World" is a String. It's a general-purpose data type that's used a lot all throughout Java.
An AttributedString holds text and some attributes. Attributes can be pretty much anything. Usually it's used for taggin some parts of the text with some language or even providing information about layout arguments (bold, italics, ...). It's a very specific data type that's used for some specific areas only.
Unless you have a specific reason to use AttributedString, you can pretty much forget that it exists.
if(word.equals(" ")){
}
I have this IF statment, what i would like it to also do is the following..
if(word.equals(" ") OR word.equals(".") ){
}
I know the above code will not work, but im looking how to implement something like that? Anyone know how to do an if or, or do i have to use else if?
Android uses Java, and the boolean OR operator in Java (and in pretty much every single language with a C-inspired syntax) is ||.
if (word.equals(" ") || word.equals("."))
Here is a document describing the Java operators. You may also want to read it from the start since it covers the basics of Java.
There are thousands of tutorials on Google explaining the basics of the Java language that you could also use.
Probably, the reason the question was asked is that you must do this test BEFORE trying to do a .parseDouble on 'word' or else the app will crash
At the server end (GAE), I've got a java Hashtable.
At the client end (iPhone), I'm trying to create an NSDictionary.
myHashTable.toString() gets me something that looks darned-close-to-but-not-quite-the-same-as [myDictionary description]. If they were the same, I could write the string to a file and do:
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:tmpFile];
I could write a little parser in obj-C to deal with myHashtable.toString(), but I'm sort-of hoping that there's a shortcut already built into something, somewhere -- I just can't seem to find it.
(So, being a geek, I'll spend far longer searching the web for a shortcut than it would take me to write & debug the parser... ;)
Anyway -- hints?
Thanks!
I would convert the Hashtable into something JSON-like and take it on the iPhone side.
Hashtable.toString() is not ideal, it will have problem with spaces, comma and quotation marks.
For JSON-to-NSDictionary, you can find the json-framework tools under http://www.json.org/
As j-16 SDiZ mentioned, you need to serialize your hashtable. It can be to json, xml or some other format. Once serialized, you need to deserialize them into an NSDictionary. JSON is probably the easiest format to do this with plenty of libraries for both Objective-C and Java. http://json.org has a list of libraries.