Results 1 to 2 of 2
  1. #1
    BenHart1121 is offline Member
    Join Date
    Apr 2015
    Rep Power

    Default reading and writing files into java

    have to create a file named Lab13.txt. In the file I have 10 random numbers. I have to import the 10 numbers and have to Multiply all the numbers from Lab13.txt by 10 and save all the new numbers a new file named Lab13_scale.txt. so if the number 10 is in lab13.txt it prints 100 to Lab13_scale.txt. how do i get it to Multiply Here is what I have:

    Java Code:
    import java.util.Scanner;
    public class Lab13 {
        public static void main(String ar[]) {
            int x=0;
            try {
                File file=new File("Lab13.txt");
                FileWriter filewriter=new FileWriter(file);
                BufferedWriter writer=new BufferedWriter(filewriter);
                for(int i=0;i<10;i++) {
                    writer.write(" "+x);
            catch(IOException e) {
            try {
                File file1=new File("Lab13.txt");
                FileReader filereader=new FileReader(file1);
                BufferedReader reader=new BufferedReader(filereader);
                String y;
                while((y=reader.readLine())!=null) {
            catch(Exception e) {

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: reading and writing files into java

    Since you are writing the information to the file as a String so you need to read it back the same way. There are a variety of ways of doing this. I recommend you look at the BufferedReader class. And when you read in the String, you need to convert the number to the appropriate primitive value (int or double) so you can then do the multiplication. There are methods in String, Integer, and Double which could help with the conversion. As always, I recommend you practice them in small program snippets so you get used to working with them.


    Java Code:
    String number = "20.1";
    double val = Double.parseDouble(number);
    // now you can do math operations on val.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. reading and writing txt. files into a database
    By Dan C in forum New To Java
    Replies: 1
    Last Post: 05-30-2013, 08:51 AM
  2. Reading / Writing files
    By Learning Java in forum New To Java
    Replies: 6
    Last Post: 08-08-2010, 09:21 PM
  3. Reading and Writing Text Files
    By kandt in forum New To Java
    Replies: 1
    Last Post: 11-12-2008, 03:15 AM
  4. Reading/Writing files through Applet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-15-2007, 08:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts