String 6 « string « Java Data Type Q&A





1. object --> string ---> object ????    coderanch.com

I need to take a object convert it to a string and later take this string and convert this to an object again..... I cant use XML or other nice things ! I can do it, by taking every class member that contains string, intengers, and arraylist, and put it into a komma delimitered string, and then when i read it ...

2. String - the proto-auto-boxer!    coderanch.com

3. String Object query    coderanch.com

Hi, I have a doubt in the following question: Given the following, 13. String x = new String("xyz"); 14. y = "abc"; 15. x = x + y; how many String objects have been created? A. 2 B. 3 C. 4 D. 5 According to me, Three String objects are created - 1 - abc(referneced by x) , 2- abc(refernced by ...

4. Making an object from string    coderanch.com

The answer is yes you certainly can do that, and much more, with Java. The part of the language/API for doing this is called "Reflection". Google "Java Reflection" for more. In the first instance (ha!), look at Class.forName(), Class.newInstance() and ClassLoader.loadClass(). After reading about Reflection, look at things like Class.getConstructor() and Constructor.newInstance().

5. Some string questions    coderanch.com

1) According to the JavaDoc, "if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned". A potential use is to avoid having to check for whether you have a null object before the call. 2) StringBuffer extends Object so String.valueOf(Object) already takes care of that case. The other signatures are for non-objects (primitives ...

6. Hot to know if a string is been encoded?    coderanch.com

I've already solved, I had a method which updateds the user object and it was in charge to encode the password, in case he set a new one, but that was not a good idea, because if he only changed the address, for example, the password would be then encoded two times. SO I decided to create a task dedicated only ...

7. Perform calculation on String    coderanch.com

8. String to object typ    coderanch.com

More of a beginner's question. Casting objects (reference types) is totally different from casting primitives. In the case of primitives you can alter the storage type and value of a variableint i; . . . i = (int)123.45;. . . not only changes the storage of the double value 123.45 from 64 bits to 32 bits, but also changes the value ...

9. String problems....    coderanch.com





10. String compression    coderanch.com

I have the exact same assignment. We're not allowed to use loops though, and the extent of what we ARE allowed to do is really just if statements and switch cases. Basically, we need to input something like =6-=10+=9j=24$ where = is the delimiter. The output should be: ------++++++++++jjjjjjjjj$$$$$$$$$$$$$$$$$$$$$$$$ Edit: I guess I just need to know where to start. I ...

11. Doubt on String?    coderanch.com

12. Computing a string 'calculations'    coderanch.com

I am currently adding such capabilities to an open source project of mine. I am using antlr to parse the expressions. The learning curve for antlr is a little steep (There is a book by the creator Terrence Parr that is good though), though pretty easy to work with once you get the hang of it. It would be nice if ...

13. String recognition questions...    coderanch.com

-------------------------------------------------------------------------------- i want to build a function that inputs a string and i want to recognize a complex polynomial in that string and if for example if the user inputs the folloing string (1+i)x^3 +(5i)x^2 +(3+5i)x +5 (each cell in the complex number array consists of a real number and emaginary) the function will put in cell 3 the value 1 ...

15. Using the String    coderanch.com

Strings are peculiar in Java. They are immutable, and they have another strange bit of behaviour. You have actually created two String objects in that bit of code. A String literal is a String object too. String s = new String("abcdef");This has a String whose value is "abcdef". This is a String object, and because you have written out its value ...

16. String Class    coderanch.com

You probably can't mimic all the behaviour of the String class, like using the same object for two literals. And you can't create a new member class for the java.lang package. Why are you doing this? I presume as an exercise? It seems pointless otherwise. Get your Strings divided into a char[] array and put them into a new char[] array. ...





17. String class    coderanch.com

Welcome to the Ranch. Have you read the API documentation for String? That is so you can use the same String object in several different places. If you have a String like "Arpit" you can pass it around your application in different classes and different threads secure in the knowledge that it will still say "Arpit" when you have finished. If ...

18. Information on string object internals    coderanch.com

I don't have a problem, but I am very curious to understand this aspect of Java internals that I encountered recently. It may be a newb question, but I wasn't able to find an appropriate Google keyword to get an answer. In the debugger I observed that the string variable "name" is equal to "/css/cssimages/circlexx.png". When executed, the debugger shows the ...

19. I need help with Strings    coderanch.com

Welcome to Javaranch. Well meaning folks will redirect you to the forum's naming policy. Regarding your question, the behaviour is different because of sheer dumb luck. Consider yourself lucky, because it got you to ask this question and seek clarification. The answer may or may not be easy to explain, but let me just say that you should use the 'equals' ...

20. String Objects    coderanch.com

I suggest you take the approach that Java is a more Object Oriented approach to most problems - see if you can get the Java program to do what you want and do not try to make stack/heap distinction. My understanding is that if you decalare a String as static, there is only one string and all instances see the same ...

21. Strings    coderanch.com

Hi All, Can a string object with no reference, residing in a string pool be got back? That is, i understand that each time a new literal is creatd, JVM searches for a same string in the string pool anf if it finds one it refers to that one. So if there is string which has no existing references, can it ...

23. Getting the Last token from a String    coderanch.com

Hi Ranchers, I have a string from which I need to get the last token and the token changes everytime based on the string value.I am planning to use String tokenizer with hasMoreTokens method. Haven't come with the code as of it. Is there any other ways of getting the last token of a string variable. Please let me know. Thanks ...

24. A simple question regarding String class    coderanch.com

public class Rajat { public static void main(String args[]) { String a=null; String b="rajat"; System.out.println(a+b); System.out.println(a); System.out.println(a.toString()); } } output of 1st system.oout.println statement comes to be nullrajat. output of 2nd system.out.println statement comes to be null. 3rd system.out.println statement throws null pointer exception. can you tell me why? can you tell me why?

25. String tokenizing- Advise needed    coderanch.com

Hello Java smarts, I have code string arrray that has names(including first and last name and Mr, Mrs, Jr & etc) in it. Here is an example. String[] names = {"Bobby Bounds", "Dawn & Gary McMullin", "Mr. & Mrs. Rick Spears", "Mr John E. & Gary R. Scott"}; What is the best way to parse the first and last names out ...

26. a question about string and eqalization..    coderanch.com

== determines that the two object references point to the exact same stack/heap variable, aka their memory addresses. The most primitive version of .equals() also does the exact same thing. The difference is that equals can be overridden by any class and is quite often overridden. equals() is intended to ask if the objects are 'equivalent', not necessarily the same object ...

27. Strings    coderanch.com

HashCode is a method used in computer science to give an arbitrary numeric that always returns the same number for a String. How the String is stored in memory of the computer will affect how ( == ) operates. String class equals methods will compare two strings and decide if they are equal no matter where they are in memory.

28. Dynamic Property Strings    coderanch.com

29. Immutability behavior of a String object    coderanch.com

Hello, I am reading HeadFirst Java and there's one section about String immutability that I'm a little bit confused. There's one example: String s = "0"; for ( int x = 1; x< 10; x++ ) { s = s + x; } The book says that it's creating ten String objects ("0", "01", "012", "0123", through "0123456789"). In the end ...

30. Need a string helper function    coderanch.com

String s ="tHis is A TEST String" String modify(String s){ String modifiedString =s ; //do some modification and return. return modifiedString } output Should be : This Is A Test String That is what ever I give it to the modify function it should return me the string formated with all leters caps for each first word. Can anybody please help ...

32. calling a metohd name usingn string    coderanch.com

Hi Friends, I want to call a java method dynamically. That method name comes from the data base. So I would know method name at run time,will get that into a string variable. with that string variable want to call the method. ex: java method: public void callDisplay(){} I get from db: Display..I store it in String method="callDisplay()"; using method string ...

33. String    coderanch.com

Howcome you can create a String object like: String a = "Hello,World"; What mechanism does it use to automatically create a new String object and store "Hello,World" inside it? I would like create a class like that so I could create new objects just like String does it. Imagine you have something like this: class foo { int value; } Now ...

34. simple String assignment    coderanch.com

OK, I see now this general rule also applies to other Objects and primitives as well besides the String class. In a way the rule makes some sense since we don't want Object assignments going on willy-nilly anywhere do we ? (except for initialization). Thanks for THE ANSWER, I can sleep now tonight. I thought knew Strings cold...

35. Confused with STRING references.    coderanch.com

Hi, I've a program like..... class ex1 { public static void main(String args[]) { String s1="hellothisisourworld "; String s2 = s1; s1=s1.substring(2,5); System.out.println(s1==s2); } } Here, s1 and s2 are 2 string objects. My understanding is that s1 points to an address which hold the string "hellothisisourworld". Later s2 is assigned s1. So s2 also points to the same ADDRESS as ...

36. String A-String B?    coderanch.com

that sounds frightfully complex. from what you say, it does seem quite complex. the data are regular so what you say would work but please clarify, do you mean to say that the fact that i have other strings which contain the zip code , city and state does not help me in any way. there is no way to 'subtract' ...

37. strings    coderanch.com

What is the output of the following code fragment: int[] z = new int[9]; z[0] = 7; z[1] = 3; z[2] = 4; System.out.println( z[0] + z[1] + " " + z[5] ); a. 10 0 b. 7 3 0 c. The program is defective and will not compile. d. 7 3 4 what i think is that "b" should be ...

38. Execute the contents of a String    coderanch.com

While this is possible using the Java Reflection API, it's not easy, and usually not a good idea. The usual way to do this is to actually store an object instead of a string with each of your nodes. This object can be anything as long as they all implement the same interface, for example: interface Doer { public String doit(); ...

39. Strings    coderanch.com

Am I just blind or is there no remove() function in a string? Say I want to remove the 2nd character of the string, the only way I've found is to build a new string of the two substrings (all characters before the one I want to remove and all the ones behind). I know it's not posible to change a ...

40. Disecting a String    coderanch.com

41. disecting a string    coderanch.com

42. Numerics to a String    coderanch.com

Anyone, I know that there are ways/methods that convert a "string" to a "numeric/double/int/etc.", however is there a way to convert a numberic to a string? The reason that I ask is because I am wanting to "truncate" the value of a double variable. Unlike C/C++, I cannot use "precision" with some type of conversion specifier to do this. I was ...

43. object to string    coderanch.com

Hi, every class you create is derived from the class Object, the class Object has a method toString(). So therefore once you create an instance of that class you can call the toString() method. Here's an example, hope this helps. /* small program that converts an object to a string */ import java.util.*; class ObjectToString { public static void main( String ...

44. String argument in main method()    coderanch.com

In main mehod there is a one paramenter is given as string *** public static void main(String[] args) *** string args is given to pass comandline argument but if we don't pass any argument then it runs without any error but as a function or method rule if we define a function or method having an argument then must be passed ...

45. define String class    coderanch.com

46. Asking about "string" data type of JAVA    coderanch.com

hi sir, hope u r doin' fine .i m having some problems in the String data type of JAVA so plz help me out.The problem is given below: Write a program to display a random choice from a set of six choice of (i.e breakfastApple,Orange,Grappes,Pineapple,Strawbery&Bannana) i hope sir taht u will get me this answer asap. i really appriaciate your concern ...

47. asking about STRING ASSOCIATIVITY    coderanch.com

i have a question regarding string precedence given below: string x="my name is jitender kumar" sir i want to ask that due to associativity ,the (=)sign starts functioning from RIGHT SIDE but in the string ,it starts executing from left. it is really confusing,either to use index method in string ,even then,it starts counting from left rather than from right.for me ...

48. String / string =>mutable?    coderanch.com

All Strings are immutable. Once created they can NOT be modified in any way. This is to allow for some cute little "optimizations" in the JVM. Specifically the re-use of defined Strings. If you define the String "String yes = "Y"; in lots of places, the JVM only makes ONE object for it and points all the various uses at the ...

49. asking about string as a object in java    coderanch.com

Hi there u had a good Questuion There ... and i think u will not be convinced by above replies ... i will try to sort u out from there ... in java String is a class that has particular specifictaions ... and more Strings as n C++/c are char arrays ... which are ccreated by combining different chars ... for ...

50. cutting string into peaces    coderanch.com

Hi MacTosh, This is an interesting exercise. I was thinking about how I'd do it if I was going from scratch and roughed out (possibly incomplete) ideas. You might be able to incorporate some of the following. ~~~~~ I'd look at running a loop on the string, based on it's length. Within each iteration I'd do something like... test each character ...

51. Strings and Garbage Collection    coderanch.com

Dale As far as I know (and someone may correct me if I'm wrong), the two Strings you create in your examples are just Strings stored in the String pool and pool Strings are not subject to Garbage Collection because you haven't actually made an Object. If you had used new to create myString then it would be subject to garbage ...

52. asking about string    coderanch.com

53. strings    coderanch.com

Use the "indexOf()" method to find the position of a character in the String. int slashPos = s.indexOf( '\' ); If it is not found, the position will equal -1. Then use the "substring()" method to break it out. if ( slashPos != -1 ) { firstPart = s.substring( 0 , slashPos ); secondPart = s.substring( slashPos + 1 ); } ...

54. Strings    coderanch.com

Kevin sounds like a school assignment so most people probably will not just blindly give the answer - the whole point of going to school is to learn... What do you have so far? any code written? or even a rough outline of what steps you need - show us what you have and we'll point you in the right direction. ...

55. strings    coderanch.com

Hello all, How can check to see if something anything but null? But the catch is that I don't know what the user will enter. So I can't setup a static variable for this information. I know you use the .equals() for comparing strings, but what would I do for not equals? Thanks.

56. how to know the no of occurance in a string    coderanch.com

public static void main() throws IOException { String a = "aab ca d a e"; InputStreamReader std = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(std); while (a.length() != 0) { StringTokenizer st1 = new StringTokenizer(a, "a", true); StringTokenizer st2 = new StringTokenizer(a, "a"); System.out.println(a + "\n" + st1.countTokens()); System.out.println(a + "\n" + st2.countTokens()); System.out.println("Total:" + (st1.countTokens() - st2.countTokens())); System.out.println("************"); System.out.print("enter a:"); ...

57. Strings    coderanch.com

58. String Compression    coderanch.com

I need to compress a string into a ZIP archive, and then send this string to a remote location( either through socket or to a browser window). I built a function that recieves a String, and using the Defalte function in util.zip. When I use the ".setInput(byte[])", i get a StackOverflow...(blablabla). in the java.sun site, its a known bug. can you ...

59. Change \ into \\ in a string    coderanch.com

60. Strings    coderanch.com

Originally posted by Dan Lund: How much is it possible to store in a string object? I tried to store a not so large text in one string object and I got a lot of errors. Is it any other way that is better to store texts that are as big as book reviews for example? As far as I know, ...

61. String problem    coderanch.com

Does the text you're pasting in have either a quote or a slash in it? Show me the line after you've pasted text into it. I have a feeling either there is a quote in that text or the last character may be a slash which would be "escaping" the close quote. Post the line after you've pasted the text and ...

62. String references/values    coderanch.com

63. combination of strings    coderanch.com

64. String handling    coderanch.com

Hi, I have a doubt. String letter= "o"; String hi = "hello"; String concatHi = "hell"+letter; the contents of hi and concatHi are same. Means if i use equals method ,it gives me true. Also, if i use hi==concatHi, it returns false, as two are not having same reference. I printed hashcodes for hi and concatHi. As two are different objects,it ...

65. String method    coderanch.com

1. (To Max) Well, maybe. But at this point, it's been asked in a bunch of other forums as well, and others have already chosen to redirect some of the other forums here, so I'd choosing to answer it here for consistency. (To Quasi) If the value of hibyte used was 0, then the simplest solution is to use the String(byte[], ...

66. String token    coderanch.com

public String replaceStringWithString(String srcStr, String oldStr, String newStr) { while (srcStr.indexOf(oldStr) > -1) { if (!(srcStr.equals(newStr))) { int i = srcStr.indexOf(oldStr); String firstStr = null; String restStr = null; if (i != -1) { firstStr = srcStr.substring(0, i); int index = srcStr.indexOf(oldStr) + oldStr.length(); if (index <= srcStr.length()) { restStr = srcStr.substring(index); } StringBuffer sb = new StringBuffer(); sb.append(firstStr).append(newStr).append(restStr); srcStr = ...

67. Strings    coderanch.com

Hi, I am traversing through a string, character by character, how would I know the end of the string? Example code: ... ... String testStr = "Java is awesome!"; String testStr2 = "e"; int index = 0; while (index != str.length()){ char ch = str.charAt(index); if (ch == 'e'){ char ch1 = str.charAt(index+1); if (ch1 == '!'){ doSomething(); } } } ...

68. Strings and Hashtables    coderanch.com

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - ...

69. Strings    coderanch.com

No, actually what happens is that the changes you made are foisted on a new String which is created. See the sample code below: public class Test40 { public static void main(String [] args) { String s1 = new String(" Java "); String s2 = s1.trim(); System.out.println("s1 and s2 point to the same String? " + (s1 == s2)); System.out.println(s1); System.out.println(s2); ...

70. Question Regarding Strings    coderanch.com

No. s2.toUpperCase() returns a brand new string that is an upper case'ed copy of s2. s2 is unchanged. Try this: class A { public static void main(String[] args) { String s1="HELLO WORLD"; String s2="hello world"; String s3 = s2.toUpperCase(); if(s1.equals(s3) == true) System.out.println("true"); System.out.println( s2 ); System.out.println( s3 ); } } Even if you do s2 = s2.toUpperCase(); You still get ...

71. Strings are immunable - What does this mean?    coderanch.com

Strings are immunable What does this mean? When I change a String with a string operation like replace or StringBuffer then the changed String is a new string object / reference In this case s2 has the same content as s1 after a string Operation. But s2 is not pointing to the same reference (memory place) than s1 as s2==s3 ...

72. return 2 strings    coderanch.com

73. method that reads a string for vowels?    coderanch.com

I am to create a program that asks the user to enter a string in any combination. Once the string is entered the number of each vowel in the string is outputed as well as the remaining nonvowel characters (including spaces). I pseudocoded and planned out how I am going to do the program. My only question though, how do I ...

74. Garbage Collection of Strings and Objects    coderanch.com

I heard that when you set an object to null, it is eligable for g.c. But when you set a String to null it is not eligable for g.c. as Strings are never garbage collected. Is that true? When is e eligable for g.c. (Garbage Collection) in Test program? public class Test { public static void main (String args []) { ...

75. String question    coderanch.com

77. Why String class is final    coderanch.com

78. Strings    coderanch.com

79. obj instanceof (String [] )    coderanch.com

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - ...

80. strings    coderanch.com

Tony This sounds like a school assignmnet... What have you got so far? Do you have the user entered string and you need to know what to do next? A simple process would be: Get the user String Split it into the individual words Reverse the order of the words Print the words back to the screen in the new order. ...

81. String class    coderanch.com

I am bit confused when to use new with String and when to not use as String is a class to make an object we had to write String s = new Srtring("Hello"); But h ow does the following code works where we are not making an object public class MyClass { public static void main(String args[]) { String s = ...

82. unescaping a string    coderanch.com

83. Yet ANOTHER string problem....    coderanch.com

OK, OK, i admit that over the past 3 weeks or so, I have put up quite a few queries regarding strings, and nothing else. I've neglected the javaranch assignments, etc..., just to read this colussus chapter on strings in Ivor Horton's epic "Beginning Java 2". Well, I have yet another query regardiong strings..... Below is a program that reads a ...

85. Verify if a String is numeric    coderanch.com

Hello: I find that this is a pparently silly doubt (so I am writing in beginner's forum): I never found a method in the API of the Java to verify if a String is numerical or not, either Integer or floating-point. A way - that I don't like - would be to make something thus: public boolean isInt(String str) { boolean ...

86. Problem with strings    coderanch.com

I know this should be a simple solution but I think I've confused myself with the different string methods. My program should create a new string by inserting the second string into the first. The second string should be inserted into the first at the position number given by the user. A push in right direction would be appreciated. import cs1.Keyboard; ...

87. Using strings, etc.    coderanch.com

I want to print out ALL links present in a page (the page is in HTML). Can someone tell me how to do this...what's wrong with this code...anything! Thanks. public class ReturnURL { public int Beg(String stringX) { int b=stringX.indexOf("a href="); return b; } public int End(String stringY) { int e=stringY.indexOf("\""); return e; } public void ParsePage(){ String strFText=new String(); try{ ...

88. string to object    coderanch.com

i have a text game im creating that has a 3x3 dimensioned space. i.e. 9 rooms in a 3x3 pattern. each room contains a jewel. i have a commandWords class that contains methods etc to read info from user. i also have a command class that determines first word and second word entries from the user i.e take jewel. my problem ...

89. regarding String    coderanch.com

90. Need help with String method    coderanch.com

You need to do a better job of explaining what you want to do. Are you trying to write a method that will return each and every string contained in an array of strings? If so, you need to realize that a method only returns once. If it is a method that returns a string you only get one string.

91. I need help,a question about String objects!    coderanch.com

Equals() This compares an object passed as an argument with the current object, and returns true if they are the same object (not just equal - they must be one and the same object). Or returns false if they are different objects, even if the objects have identical values for their data members. Page 239, Beginning Java2 1.3 Edition, Horton, Wrox ...

92. String to Clipboard    coderanch.com

93. Saving A Clean String    coderanch.com

94. canonical string representation    coderanch.com

95. how do you chop the last 7 digits off of a String in JAVA?    coderanch.com

Just be careful - the parameters for the substring method were definitely designed by compiler guys . The first parameter is cool - it's the index of the first character. But the send parameter is NOT the number of characters. It is also not the index of the last character in the substring. It is the index of the last character ...

96. Why does getBytes() truncate the string?    coderanch.com

Hi all, I want to convert a string into bytes. I've read there are two alternatives: a) String s; byte[] buf = new byte[ s.length() + 1 ]; s.getBytes( 0, s.length(), buf, 0 ); or b) String s; byte[] buf = s.getBytes(); whereby (a) is deprecated, but the preferred way to do it, namely (b), truncates the string. Why does the ...

97. string replac    coderanch.com

98. Plz help! string class question    coderanch.com

Terms like "binary string" are always a bit ambiguous. You did show a correct string representation of 32 1s and 0s which sounds like what was asked for. Are you comfortable converting integer to binary and back? IP addresses are usually given as 4 numbers, each between 0 and 255, like 11.96.8.175. You can make each number an 8 bit binary ...

99. String question!    coderanch.com

100. string break/flush right    coderanch.com

See if I got this right. The menu looks like: Jump Through Hoops ........1 Light My Fire .............2 Be All You Can Be .........3 Is that what you mean by right justified out to column 80? If not, ignore the rest. I knew REXX before I knew Java, and REXX has some wonderful string functions that I duplicate in every new ...