• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I pass a String from one class to another String in a seperate Class?  RSS feed

 
Papi Escobar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code below their are two seperate classes. I would like to pass the String path located in Class Fildemo to the String lineslocated in the HtmlDataTable Class. The code for both classes is listed below.




 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally what you'd do is to put a method into the second class which requires a String parameter, and then call that method from the first class, passing the appropriate String.

But I don't have a suggestion as to what that method should do, or even what it should be called, because I can't tell why you need the HtmlDataTable class to have that String in the first place. I can't even make a guess because the two classes seem to be unrelated. Could you clarify a bit, by explaining your design?
 
Papi Escobar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Normally what you'd do is to put a method into the second class which requires a String parameter, and then call that method from the first class, passing the appropriate String.

But I don't have a suggestion as to what that method should do, or even what it should be called, because I can't tell why you need the HtmlDataTable class to have that String in the first place. I can't even make a guess because the two classes seem to be unrelated. Could you clarify a bit, by explaining your design?


The program checks for existing files in a directory compares them and writes the differences in a table. There are two other classes which were not included because they dont directly relate to the issue I'm having. The String path contains a set of data that I would like to write to the table. The String lines is the code which writes the data to the table.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Papi Escobar wrote:The program checks for existing files in a directory compares them and writes the differences in a table.


I'm still just as confused because none of the code you posted does that, as far as I can see.

The String path contains a set of data that I would like to write to the table.


I'm confused about which String path that means, or whether you meant to write "paths" instead. And I'm confused because your "Fildemo" class only has a readLibDir method which doesn't return anything, but your other class refers to a "FileDemo" class which must have a getPaths() method which returns an array of Strings. So what's the relationship between those two classes?
 
Papi Escobar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Papi Escobar wrote:The program checks for existing files in a directory compares them and writes the differences in a table.


I'm still just as confused because none of the code you posted does that, as far as I can see.


Please keep in mind several classes were omitted because they I dont believe they directly relate to the issue, and to minimize the amount of code shown here. If needed I can post the code from the other classes however it needs to be noted that it contains a lot of code from 3 different classes

The String path contains a set of data that I would like to write to the table.


I'm confused about which String path that means, or whether you meant to write "paths" instead. And I'm confused because your "Fildemo" class only has a readLibDir method which doesn't return anything, but your other class refers to a "FileDemo" class which must have a getPaths() method which returns an array of Strings. So what's the relationship between those two classes?


I did mean paths, excuse the typo. The HtmlDataTable Class creates and writes data to the table. The data located in the String Paths is part of the data set needed to populate my table.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still in the dark about this FileDemo class and its getPaths() method. Or should I assume that "Fildemo" was a typo for "FileDemo"? If so, where's its getPaths() method?
 
Papi Escobar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I'm still in the dark about this FileDemo class and its getPaths() method. Or should I assume that "Fildemo" was a typo for "FileDemo"? If so, where's its getPaths() method?

Yes Filedemo was a typo as well. I was trying to create a method getPaths() to pass the string and I left that code in the HtmlDataTable Class in error(String[] paths = demo.getPaths();) please ignore that line I can no longer edit it out. The code was incorrect.

I need help creating a method where I can pass the value of Paths to the String lines
 
Campbell Ritchie
Marshal
Posts: 56586
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Papi Escobar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program that parses an XML document. After parsing the data it compares the data and writes the data to a HTML table. I would like to write the value of the [String fullname=artifact +"-" +version;] to my HTML Table. The code which writes data to the table is located in the class HtmlDataTable [String lines="<tr> <td>"+ fullname + "</td> <td>"+ jarfilename + " </td> <td>"+ jarfilename + "</td> </tr>";] I would like to create a method which passes the value of the String fullname to the String lines which writes the data to the table. The code for both classes is located below. There are other classes within the program that are not listed. I've only included the classes that I felt are relevant to the issue. If you feel theres a need for me to include the additional code please just ask.




 
Marcus Biel
Ranch Hand
Posts: 51
IntelliJ IDE Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am really not sure if I understand you correctly, you have a rather short description, and a rather lengthy code snippet.
However I have the feeling you fundamentally have not understood how Java works.
In short, String is an object, but it is also an immutable. Whenever you change a String, a new String Object will be created - you cannot change a String in one class
and expect that this will have the side effect of changing another String in another class (FORTUNATELY!!!)
What you need to do is create a String, and when you need this String in another class, you will have to create a method that returns this String to the other method.
If you have complex String concatenations going on like I can see in your case, have a look at the Java class "StringBuilder, which might do that more performant
and / or in a better readable way.

I have lately done two Java video tutorials which also talk about immutable Strings and StringBuilder:

Episode 19 - Object clone method -
Minute 30-35 - What is an Immutable / what does it mean for String to be immutable?
Object Clone Method

Episode 20 - Object toString method -
Minute 32-37 explaining class StringBuilder
Object toString method

 
Campbell Ritchie
Marshal
Posts: 56586
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have already been told not to create new copies of the question. I shall see if I can merge this question with your existing threads.
 
Papi Escobar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcus Biel wrote: . . .
I have lately done two Java video tutorials which also talk about immutable Strings and StringBuilder:
. . .


Thank you I will watch the videos and respond after Ive watched them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!