-1

I have a Java class Text.java which just contains a severly long string (it's text from a website but stored in u006e values and such) I am creating a data mining class and was just wondering how to reference the string so that I can test things like the length and count refrences to numbers and stuff like that? the actual class is Text.java and the actual code is:

public class Text
{
/**
 * A very long String, containing text from a web site.
 */
public static final String TEXT;

static
{
TEXT = //this is where the extremely long string is composed of u006c/u006e and so on

I have tried

text.length()
Text.length()
TEXT.length()

I am fairly new to Java so I might not be going about it correctly but here is just the start of my datamining class

public class DataMiner 
{
    /**
     * Main Method, or main point of entry in the Java application.
     * 
     * @param args not used
     */
    public static void main(String[] args)
    {        
        System.out.printf("The total length of input lines are: ", Text.length());
    }
}

Here I was just looking to tell me how ling the lines of input are within the other Text.java class.

Kara
  • 6,115
  • 16
  • 50
  • 57
Blankets_McGee
  • 17
  • 1
  • 1
  • 6
  • You need to setup an accessor method. Because you cant access primitive data types. – Munib Nov 10 '16 at 02:20
  • create a method on `Text` called `length` that returns `TEXT.length()` or simply change to `Text.TEXT.length()` - Personally I would use the first choice and have TEXT as a private field – Scary Wombat Nov 10 '16 at 02:21
  • @return0 *Because you cant access primitive data types.* - **what do you mean?** – Scary Wombat Nov 10 '16 at 02:23
  • using Text.TEXT.length() i was able to compile but no value was output only my line of text "The total length of input lines are: " – Blankets_McGee Nov 10 '16 at 02:28
  • @ScaryWombat Sorry, I meant to say that a getter method should be set. – Munib Nov 10 '16 at 03:36

3 Answers3

0

For a quick fix try this

System.out.printf("The total length of input lines are: %d \n", Text.TEXT.length());

However better would be to create a method on Text called length that returns TEXT.length() or simply change to Text.TEXT.length() - Personally I would use the first choice and have TEXT as a private field

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • using %d \n worked like a charm! thanks so much I realize a simpiler and more preferred method would to be to change the actual Text.java class but I was just trying to create an entirely new class that does all that without having to edit the existing code within the Text.java class. Thanks for the help! – Blankets_McGee Nov 10 '16 at 03:17
0

For a proper solution change your TEXT variable (for example) to

private static final String TEXT = "u006e";

Make this a class variable and set whatever you want the text to be. If you want to be able to change the string, you can't make TEXT final.

Inside the Text class you'll need to create a getter method to be able to access this private string. Create the method as follows

public static String getText() { 
return TEXT;
)

Reference to: Why use getters and setters?

This is an explanation on why you should be using getters and setters rather than having public variables that you access directly from your Text class.

Now in your main method you can write

System.out.printf("The total length of input lines are: %d", getText().length());

This will return the length of 5 given that TEXT is u006e.

Community
  • 1
  • 1
FireWolf
  • 21
  • 6
-1

its not a complicated question ,you should wirte like this :

System.out.printf("The total length of input lines are: ", Text.TEXT .length());

hope that's helpful .

Kiplening
  • 1
  • 2
  • I tried this and was able to compile but the output was simply "The total length of input lines are: BUILD SUCCESSFUL (total time: 0 seconds) – Blankets_McGee Nov 10 '16 at 02:33