Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

method writing

Posted on 2004-04-10
6
Medium Priority
?
196 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 1000 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
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.

 

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

618 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