Solved

method writing

Posted on 2004-04-10
6
191 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

828 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