Solved

method writing

Posted on 2004-04-10
6
186 Views
Last Modified: 2010-03-31
Who can help me to think of how to write a method, given an int to it and return a string:

i got a class called sym:

public class sym
 {
        
       public static final int tCOMMA                        = 6;            // ,
       public static final int tMINUS                        = 7;            // -
       public static final int tDOTS                        = 8;            // ..
       public static final int tSLASH                        = 9;            // /
}

at another class, i need to build a method, given an int says get_symbol(6), then the method would go to the class above to return the variable whose value is 6 in String.??? This sounds pretty familiar and easy..to me but i just somehow stuck~?

0
Comment
Question by:jtcy
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:OBCT
ID: 10796866
public String getStringValue(int val)
{
    return Integer.toString(val);
}

Is this what you mean?
0
 

Author Comment

by:jtcy
ID: 10796878
not that simple.

the method should go to the sym class and then search for the right String and return that string.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 10796925
You just need to mirror the int constants with an array:

public static final String[] SYMBOLS = { "", "", "", "", "", "", ",", "-", "...", "/" };

String symbol = sym.SYMBOLS[sym.tMINUS]; // 'symbol' contains "-"
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:jtcy
ID: 10796993
CEHJ, that sounds cool but is there any other way to do it? cuz i got longgggg list to add to the array if using this option.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10797024
You could read them into an ArrayList from a file:

You could then do:

StringList symbols = new StringList("symbols.txt");

String symbol = (String)symbols.get(sym.tMINUS); // 'symbol' contains "-"

SNIP=======================================

import java.io.*;
import java.util.ArrayList;

/**
 *  A utility class the allows a text file to be
 *  treated as a collection of Strings
 *
 * @author     CEHJ
 * @created    29 February 2004
 */
  public class StringList extends ArrayList {

  public StringList() {
    super();
  }


  public StringList(String fileName) {
    this();
    String line = null;
    BufferedReader in = null;
    try {
      in = new BufferedReader(new FileReader(fileName));
      while ((line = in.readLine()) != null) {
        add(line);
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      try {
        in.close();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10797151
8-)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now