-3

I have written some data to a .txt file and from another Frame, I read the .txt file. Is it possible that I'm able to use the data stored in the .txt for some calculation.

Let's say the .txt only got 1 Double value. I do not want to just display it.

EDIT:

public WeightlossFat()
{
    JPanel text = new JPanel();
    text.add(bmi);
    text.add(rbmi);
    text.add(bmr);
    text.add(rbmr);
    text.add(tdee);
    text.add(rtdee);
    text.setLayout(new GridLayout(1,3));

    JPanel percent = new JPanel();
    percent.add(suggested);
    percent.add(aggressive);
    percent.add(reckless);
    percent.setLayout(new GridLayout(3,1));

    setLayout(new BorderLayout());
    add(text,BorderLayout.NORTH);
    add(percent,BorderLayout.CENTER);

    suggested.addActionListener(this);
    aggressive.addActionListener(this);
    reckless.addActionListener(this);

    try
    {

        BufferedReader bmi = new BufferedReader(new FileReader("bmi.txt"));

        while((line = bmi.readLine()) != null)
        {
            currentline = line;
        }
        lastline = currentline;
        rbmi.setText(lastline);
        bmi.close();

        BufferedReader bmr = new BufferedReader(new FileReader("bmr.txt"));

        while((line = bmr.readLine()) != null)
        {
            currentline = line;
        }
        lastline = currentline;
        rbmr.setText(lastline);
        bmr.close();

        BufferedReader tdee = new BufferedReader(new FileReader("tdee.txt"));

        while((line = tdee.readLine()) != null)
        {
            currentline = line;
        }
        lastline = currentline;
        rtdee.setText(lastline);
        tdee.close();           
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, "File does not exist", "Error", JOptionPane.ERROR_MESSAGE);
    }

}

As you can see from the above code, I'm able to display what's inside the .txt however, I wanted to use the value in the .txt to do some calculation.

public static double suggested(double lastline)
{
    //double rate = lastline * 0.15;
    //double result = lastline - rate;
    //return result;
}

Anybody able to teach me how to I use the value?

Luke
  • 85
  • 2
  • 2
  • 13
  • 2
    Your question is far too vague. Of course, it is possible to read a text file and use its content for whatever you need it. I think, you have to explain your problem in much more details. – Seelenvirtuose Feb 03 '15 at 07:52
  • Your question is unclear. Please [edit it](https://stackoverflow.com/posts/28293792/edit) and include the relevant code. –  Feb 03 '15 at 07:52
  • Your question is very broad. Maybe this helps: http://stackoverflow.com/questions/1830698/what-is-inputstream-output-stream-why-do-we-use-them-and-when-do-we-use-each – Arne Deutsch Feb 03 '15 at 07:52
  • After editing your question, it's still not very clear. I assume, you are searching for a way to convert a string into a double. If so, look at [Double.parseDouble](http://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#parseDouble-java.lang.String-) or [Double.valueOf](http://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#valueOf-java.lang.String-). – Seelenvirtuose Feb 03 '15 at 08:03
  • Why don't you simply post a few examples of the lines you have on the ".txt"? All the above code is useless. – laune Feb 03 '15 at 08:05
  • @laune on my .txt there's only 1 line. Let's say in my .txt I have "21.0" and I want to use this "21.0" for some other calculation rather than just displaying it. – Luke Feb 03 '15 at 08:18
  • Then: see my answer, it's spot on - especially if there might be more numbers in the future or if you'd like to combine the three text files into a single file. - Although Double.parseDouble(lastline) is a good enough alternative. – laune Feb 03 '15 at 08:21

3 Answers3

0

I haven't not exactly understood your question. So with Java, of course, you can read/write .txt file and take those values to make some calculation. For example you have this file: 10.2 7.4 3.4

You can read those values, store them in a vector or ArrayList and make some calculus, for example the sum. I hope this answer will be very useful for you !

I love coding
  • 1,183
  • 3
  • 18
  • 47
0

A very simple way of reading a text containing all sorts of data (words and numbers) is to use a Scanner.

Scanner scanner = new Scanner( new File( "x.txt" ) );

If you expect a double:

double dVal = scanner.nextDouble();

Or an integer:

int iVal = scanner.nextInt();

You may precede reading with a test to make sure the file is correct:

if( scanner.hasNextDouble() ){
    double dVal = scanner.nextDouble();
}

In between you may read strings, "tokens":

String tok = scanner.next();

And so on.

laune
  • 31,114
  • 3
  • 29
  • 42
0

I think using textfield is better. You can use like this..

insert value

JTextField txtBmi = new JTextFiled()
txtBmi.setText(bmi);
text.add(txtBmi);

get value

int numBmi = Integer.parseInt(txtBmi.getText());

And.. Is below thing right, you want?

    int numBmi = 0;
    int numBmr = 0;
    int numTdee = 0;

    try
    {

        BufferedReader bmi = new BufferedReader(new FileReader("bmi.txt"));

        while((line = bmi.readLine()) != null)
        {
            currentline = line;
        }
        lastline = currentline;
        rbmi.setText(lastline);
        try {
            numBmi = Integer.parseInt(lastline);
        } catch (Exception e) {
        } 
        bmi.close();

        BufferedReader bmr = new BufferedReader(new FileReader("bmr.txt"));

        while((line = bmr.readLine()) != null)
        {
            currentline = line;
        }
        lastline = currentline;
        rbmr.setText(lastline);
        try {
            numBmr = Integer.parseInt(lastline);
        } catch (Exception e) {
        } 
        bmr.close();

        BufferedReader tdee = new BufferedReader(new FileReader("tdee.txt"));

        while((line = tdee.readLine()) != null)
        {
            currentline = line;
        }
        lastline = currentline;
        rtdee.setText(lastline);
        try {
            numTdee = Integer.parseInt(lastline);
        } catch (Exception e) {
        } 
        tdee.close();           
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, "File does not exist", "Error", JOptionPane.ERROR_MESSAGE);
    }

    double calBmi = suggested(numBmi);
    double calBmr = suggested(numBmr);
    double calTdee = suggested(numTdee);
}

public static double suggested(double inputValue)
{
    double rate = inputValue * 0.15;
    double result = inputValue - rate;
    return result;
}
David Kim
  • 122
  • 2
  • 2
  • 11