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 3 of 3

Thread: read and modify a text file

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question read and modify a text file

    Good evening \ morning ppl

    i have homework that requires

    "Write a Java program to perform the following:

    Read 5 floating point number and 5 integers from Keyboard.

    Write them to a file with name “dataFile1.txt”

    Read and display them.

    Read and multiply by 2 and write them back to the another file “dataFile2.txt”.

    Read the file “dataFile2.txt” and display the values"

    I did the first part but I have difficulties in doning the second part which is "Read and multiply by 2 and write them back to the another file “dataFile2.txt”."

    Can any one help me


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: read and modify a text file

    What are you having problems with?
    Please explain.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: read and modify a text file

    You should probably use a Scanner, or something to read the file.

    It has a nextInt() function already in it.

    However, you should probably keep track of which type is a int and which is a float and where they are. For instance, you could do something like this

    Scanner s;
    try
    {
    s = new Scanner(new File("dataFile2.txt"));
    }

    catch(java.io.FileNotFoundException fnfe)
    {
    System.out.println("File not found.");
    System.exit(1);
    }

    Did you try that already?

    Also, you could try this

    int x;
    float y;

    x = s.nextInt();
    y = s.nextFloat();

Similar Threads

  1. What is the best way to read from a text file?
    By Kerr in forum File I/O & Other I/O Streams
    Replies: 20
    Last Post: January 4th, 2012, 05:41 PM
  2. Read text file
    By cardamis in forum Java IDEs
    Replies: 2
    Last Post: November 4th, 2011, 11:59 PM
  3. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  4. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM
  5. File I/O Modify Text Problem
    By Fordy252 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 26th, 2010, 05:30 AM

Tags for this Thread