Re: Read and write to a text file and calculate values
The Scanner class has some easy to use methods for reading a text file.
The PrintWriter class could be used for writing to a text file.
Re: Read and write to a text file and calculate values
Hi, thanks for your reply, I have no problem with reading and writing the file, i am struggling with calculating the time taken for each journey and compare it with user input. : )
Re: Read and write to a text file and calculate values
Quote:
calculating the time taken for each journey and compare it with user input.
What is the data the program has available for making the computations?
What are the formulas for doing the computations?
Re: Read and write to a text file and calculate values
Can you explain in English what data the program has to work with
and what the formulas are for doing the computations?
For example:
Start time + duration = arrival time
Re: Read and write to a text file and calculate values
Quote:
data to work with are the distance, the speed and number of changes in journey,
Do you know how to get the data from the file, if not please explain where the problems are.
Where do you get:
distance,
the speed
number of changes
Quote:
formulas are
the distance devided by speed
Distance/speed = duration????
For example: 100M/(50M/sec) = 2 sec?
Once you have read those three data values then
Do you know how to use that data in the formulas needed for the computations?
If not please explain.
Re: Read and write to a text file and calculate values
Quote:
calculate the time that takes for each journey
What are the formulas to do that? What steps does the program have to take?
Quote:
i dont know how to read the data from the text file in the following format:
That's strange. Earlier you said:
Quote:
I have no problem with reading and writing the file,
Can you explain?
You show that the data has three lines. Read each line, parse its contents and save it in variables.
Re: Read and write to a text file and calculate values
Quote:
how to assign data in the text file to variables for later calculations.
What do you normally do with data that you read in from a file? How is this different?
Quote:
how to compare the result of calculation with user input!
I assume the two values to be compared are numeric.
To compare two numbers, use the <, >, ==, etc operators.
Re: Read and write to a text file and calculate values
I don't know what to use to set a specific value in the text file, for instant how do I assign the third line of the text file into a variable.
Thanks I think to compare the result an if statement does the work. : )
Re: Read and write to a text file and calculate values
Quote:
how do I assign the third line of the text file into a variable.
Can you post the code for the reading the first two lines?
Explain what the problem is with reading the third line.
Quote:
what to use to set a specific value in the text file
Are you asking how to write data to a text file?
Quote:
I have no problem with reading and writing the file line by line
Re: Read and write to a text file and calculate values
Quote:
Originally Posted by
Norm
Can you post the code for the reading the first two lines?
Explain what the problem is with reading the third line.
it reads the text file line by line:
File infile = new File("data.txt");
try {
Scanner scanner = new Scanner(infile);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Are you asking how to write data to a text file?
I am trying to calculate the time for each journey but to do that i need to call the values in the text file. I dont actually know how to add my calculation to the above code. :)
Re: Read and write to a text file and calculate values
Why are you using an array named args? How does that get the data from the file?
The code in post #15 looks like it will read lines from a file. What happens when you use the Scanner methods from that code?
Do this in steps.
First make a program that reads the 3+ lines from the file and displays what was read with a label describing what the data is. For example:
Name: first
Number of changes: 1
etc
Re: Read and write to a text file and calculate values
Make a complete program, compile it and execute it.
Add some println statements to print out the values as I suggested in post #16
Re: Read and write to a text file and calculate values
is it possible to use a for loop to read through the text file and calculate the time. Or a for loop within a for loop !
Re: Read and write to a text file and calculate values
Do one thing at a time. Read the first three+ lines and print them.
When that works, move on to doing the calculations.
Re: Read and write to a text file and calculate values
Looks like you are making progress.
Does that code compile and execute and print out the correct values?
The next thing you need to do is use the value read into the number of changes to read in that number of lines worth of data (distance and speed).
Its allowed and recommended to use longer variable names that describe the data they hold.
nbrChanges vs ch
It makes the code easier to read and understand.
Please wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: Read and write to a text file and calculate values
Quote:
Originally Posted by
Norm
Does that code compile and execute and print out the correct values?
It does print the first three line of the txt file and displays the result.
Quote:
Originally Posted by
Norm
The next thing you need to do is use the value read into the number of changes to read in that number of lines worth of data (distance and speed).
Could you expand a little bit more on this please!
Re: Read and write to a text file and calculate values
Part of the file's contents:
Quote:
2
300 80
200 50
400 60
The 2 (this is 1 less than the number of following lines) is a count of the number of pairs of numbers that must be read in. You will need a loop to read the lines that follow the 2 (nbr of changes)
Re: Read and write to a text file and calculate values
@s.sariyan, Please do not remove your posts - this defeats the purpose of this being a public forum. I have added your code back to your first post.
Re: Read and write to a text file and calculate values
posts deleted by s.sariyan:
post 1:
i wrote the text file values at the top; first, second and third journey and parts of the formula is at the top too:
text file contains:
first
1
100 40
200 70
second
0
300 60
third
2
300 80
200 50
400 60
and format plus formulas:
<name>
<N> // N is the number of changes
<FB><DB> // for each leg B, FB is the distance and DB is the speed. Note B=(N+1)
post 2:
the data to work with are the distance, the speed and number of changes in journey, the formulas are
the distance devided by speed in which :
program needs to read data in textfile from the following format:
<name> // First, Second, Third
<N> // N is the number of changes
<FB><DB> // for each leg B, FB is the distance and DB is the speed. Note B=(N+1).
--- Update ---
sorry for my english language : (
post 3:
the text file contains:
first //Name
1 //Number of changes
100 40 //distans and 40 the speed
200 70 //distans and 70 the speed
second //Second journey
0 //number of change
300 60 // 300 distance and 60 speed
third //third journey
2 // number of change
300 80 // the distance and the speed
200 50 // the distance and the speed
400 60 // the distance and the speed
i dont know how to read the data from the text file in the following format:
<name>
<number of changes>
<distance> and <speed>
in terms of calculations, the program should calculate the time that takes for each journey then the program should compare the time with requested time from the user. if the calculated time matches the user input then the program should display the output.
post 4:
I have no problem with reading and writing the file line by line,, I am struggling with reading the file in this specific format. I dont know how to assign data in the text file to variables for later calculations.
The formula is: time = distance(number of change+1) devided by speed(number of changes +1) i am also not sure about the formula. how to compare the result of calculation with user input!
the steps:
1.Request time period from the user
2.read the text file in following format(mentioned above)
3.calculate the time that takes for each journey in the text file
4.compare the result with user input
5.display the output based on the comparison(if the journeys are appropriate or not)
6.if the journey is appropriate and matches the user input, save it to a different text file
I hope i was able to explain it and make it more understandable : ) Thank you
post 5:
user= JOptionPane.showInputDialog ( "Request Time:" );
Scanner inFile = new Scanner(new FileReader ("data.txt"));
String user;
String name;
int change;
int distance;
int speed;
int sum;
int ch = Integer.parseInt(args[0]);
int di = Integer.parseInt(args[1]);
int sp = Integer.parseInt(args[2]);
JOptionPane.showMessageDialog(null, "journey name: "+name+" \n Number of Changes: "+ch+" \n Distance and speed: "+di, "Journey Planner",JOptionPane.INFORMATION_MESSAGE);
}
}
i think that I have done this completely wrong, as i mentioned i am a complete beginner sorry : )