Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Read and write to a text file and calculate values

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi, I am a complete beginner to java programming,(root planner) I am trying to write a program which request a time from the user then compares it with the data in the text file. text file contains the time scale for a bus root:
    first
    1
    100 40
    200 70
    second
    0
    300 60
    third
    2
    300 80
    200 50
    400 60

    the program needs to read the text file after user time input in the following format:
    <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).

    and if the user time input matches the result in the text file after calculation store, program needs to store it in a separate text file.

    I need some hints

    any help and hint would be appreciated
    Last edited by copeg; November 28th, 2012 at 08:56 AM. Reason: Placed code back


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,031
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. : )

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,031
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: Read and write to a text file and calculate values

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,031
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,031
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: Read and write to a text file and calculate values

    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

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,031
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: Read and write to a text file and calculate values

    calculate the time that takes for each journey
    What are the formulas to do that? What steps does the program have to take?

    i dont know how to read the data from the text file in the following format:
    That's strange. Earlier you said:
    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.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,031
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: Read and write to a text file and calculate values

    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?

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. : )

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,031
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: Read and write to a text file and calculate values

    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.

    what to use to set a specific value in the text file
    Are you asking how to write data to a text file?
    I have no problem with reading and writing the file line by line
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read and write to a text file and calculate values

    Quote Originally Posted by Norm View Post
    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.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,031
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,031
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 !

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,031
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,031
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read and write to a text file and calculate values

    Quote Originally Posted by Norm View Post
    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 View Post
    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!

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,031
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: Read and write to a text file and calculate values

    Part of the file's contents:
    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)
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,334
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default 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.

  20. #20
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default 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 : )

Similar Threads

  1. help with GUI .. read file and write file
    By kernal in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 14th, 2012, 10:38 AM
  2. Read and write file in Java
    By waiheng1986 in forum File Input/Output Tutorials
    Replies: 1
    Last Post: March 18th, 2012, 11:54 AM
  3. Replies: 0
    Last Post: October 29th, 2011, 02:37 AM
  4. Java I/O File code; how to read/write file
    By ryu2le in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 18th, 2011, 05:51 PM
  5. write text to a file help
    By wolfgar in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: November 24th, 2009, 07:36 AM

Tags for this Thread