[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

method writing

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
jtcy
Asked:
jtcy
  • 3
  • 2
1 Solution
 
OBCTCommented:
public String getStringValue(int val)
{
    return Integer.toString(val);
}

Is this what you mean?
0
 
jtcyAuthor Commented:
not that simple.

the method should go to the sym class and then search for the right String and return that string.
0
 
CEHJCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jtcyAuthor Commented:
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
 
CEHJCommented:
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
 
CEHJCommented:
8-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now