[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Read File That Is String -> Integer (Assigning Value In Code)

Posted on 2004-11-20
1
Medium Priority
?
237 Views
Last Modified: 2010-03-31
Hello Team,
I have an issue with reading a string from a "file" called file.txt and turning it into a integer value.

Of you look below - i'm reading data from a file called file.txt and storing the data into an arraylist.

The first line in that text file says
priority  = 5;
It must say this on the first line. (it is a configuration file basically)

What i need to do in the code is make priority *the variable* equal to 5, but it come in as a string.
The data is in an arraylist  - so if try to print sl.get(0); I see that it's captured "priority = 5" as a string, but what i don't need
is the "priority =" part and the semi colon at the end. Just the 5, how do I get ?

See code below.
Thanks kindly



============================================================
class MyThreadGroupFactory {
//Thread Group Object which will set the maximum priority for all even threads.

    private ThreadGroup threadGroup = null;
    private static MyThreadGroupFactory instance = new MyThreadGroupFactory();
    int priority = 1;


    private MyThreadGroupFactory()
    {
          
    threadGroup = new ThreadGroup("Event Group");
       
    StringList sl = new StringList();
      sl.read("file.txt");
      
      //System.out.println(sl.get(0));


    threadGroup.setMaxPriority(priority); //<============================ "SETTING PRIORITY HERE"
    }
   
    public static ThreadGroup getThreadGroup()
    {
        return instance.threadGroup;
    }
}

class StringList extends ArrayList {


//Constructor for the StringList object


  public StringList() {
    super();
  }

  public void read(InputStream in) {
    read(new InputStreamReader(in));
  }


  public void read(Reader r) {
    String line = null;
    BufferedReader in = null;
    try {
      in = new BufferedReader(r);
      while ((line = in.readLine()) != null) {
        add(line);
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      try {
        in.close();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }

  }

  public void read(String fileName) {
    try {
      read(new FileReader(fileName));
    }
    catch(IOException e) {
      e.printStackTrace();
    }
  }

}
============================================================

0
Comment
Question by:pauldhadialla
1 Comment
 
LVL 13

Accepted Solution

by:
petmagdy earned 2000 total points
ID: 12633544
easy, for example if ur file is a single line as u described contains only the line u mentioned then just try this:

 int periority = 0;
String line = sl.get(0);
String[] splited = line.split("[/s=;]");
if(splited.length > 1)
   periority = Integer.parseInt( splited[1].trim() );
0

Featured Post

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.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

872 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