Solved

method writing

Posted on 2004-04-10
6
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

726 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