1

I have to create a file named Lab13.txt. In the file I have 10 numbers. I 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. Here is what I have:

import java.io.*;

import java.util.Scanner;
public class lab13 {

    public static void main(String[] args) throws IOException{
        File temp = new File("Lab13.txt");
        Scanner file= new Scanner(temp);


        PrintWriter writer = new PrintWriter("Lab13_scale.txt", "UTF-8");
        writer.println("");
        writer.close();

    }

}

How do I multiply the numbers by 10 and export it to the new file?

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
lawtonhself
  • 41
  • 2
  • 2
  • 6
  • where did you stuck? – Madhawa Priyashantha Apr 17 '15 at 03:58
  • 1
    i mean i don't see anything relative to multiple numbers in lab13.txt.it's a process with lot of steps.what is your problem ?taking numers from lab13?or multiplying ?or write to lab13_scale? – Madhawa Priyashantha Apr 17 '15 at 04:03
  • I have the file with 10 numbers and it brings the numbers in, but where and how do I get it to multiply the nymbers by 10 and then how do I save it to the new file – lawtonhself Apr 17 '15 at 04:06
  • Here is a related topic that reads integers from a file into a list. http://stackoverflow.com/questions/3806062/how-to-open-a-txt-file-and-read-numbers-in-java – Justin L Apr 17 '15 at 04:08

3 Answers3

1

This code is simple as this:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;


public class Lab13 {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner scan = new Scanner(new File("Lab13.txt"));
        PrintWriter print = new PrintWriter(new File("Lab13_scale.txt"));

        while(scan.hasNext()){
            print.write(10 * scan.nextInt()+"\n");
        }
        print.close();
        scan.close();
    }

}
Prashant Ghimire
  • 4,890
  • 3
  • 35
  • 46
0

I'll give you a different approach. I have wrote this from memory, let me know if you have any errors. I assumed the numbers are one on each line.

public static void main(String[] args)
{

  String toWrite = "";
  try{
   String line;
   BufferedReader reader = new BufferedReader(new FileReader("Lab13.txt"));
   while((line = reader.readLine())!=null){
       int x = Integer.parseInt(line);
       toWrite += (x*10) + "\n";
   }
   File output = new File("lab13_scale.txt");
   if(!output.exists()) output.createNewFile();
   FileWriter writer = new FileWriter(output.getAbsoluteFile());
   BufferedWriter bWriter= new BufferedWriter(writer);
   bWriter.write(toWrite);
   bWriter.close();
 }catch(Exception e){}
}
Slow Trout
  • 492
  • 3
  • 13
  • I use the generic Exception type. I assume this is your lab work so I recommend you go and find what kind of exception this can throw. – Slow Trout Apr 17 '15 at 04:07
  • This is really advanced, I am in first year java. I am not trying to sound dumb, but could you simplify it or explain how it works or how to do it? – lawtonhself Apr 17 '15 at 04:09
  • The BufferedReader, FileReader are basically reading the file content. (It dumps it into a buffer). Then I go line by line, and transform the string content of a line into an integer. Then I multiply the integer by 10, add it to the toWrite string (our new output) and add a new line "\n". Then I simply create a new file and use the File Writer/Buffered Writer to write to the new created file. – Slow Trout Apr 17 '15 at 04:12
  • ok thanks. I still get an error under bWriter in "bWriter.write(toWrite);" and in "bWriter.close();" – lawtonhself Apr 17 '15 at 04:14
  • can you paste the error. I have eddited a line -> new FileWriter(output.getAbsoluteFile()). Let me know if it works. – Slow Trout Apr 17 '15 at 04:16
  • yes: Exception in thread "main" java.lang.Error: Unresolved compilation problems: after cannot be resolved to a type Syntax error on token ".", ; expected The method println(String) is undefined for the type lab13 Duplicate local variable writer at lab13.main(lab13.java:14) – lawtonhself Apr 17 '15 at 04:18
  • wait here:Exception in thread "main" java.lang.Error: Unresolved compilation problems: bWriter cannot be resolved bWriter cannot be resolved at lab13.main(lab13.java:20) – lawtonhself Apr 17 '15 at 04:19
  • BufferedWriter bWriter= new BufferedWriter(writer); I added, by mistake an extra t to the variable name. Try now. – Slow Trout Apr 17 '15 at 04:21
  • Spacing? Like you do not want a new line or? – Slow Trout Apr 17 '15 at 04:25
  • a new line please, for each number. And thank you for helping – lawtonhself Apr 17 '15 at 04:25
  • + "\n"; This should add a new line. try +"\n" + "\n", if you want a line between the numbers. – Slow Trout Apr 17 '15 at 04:27
  • you're welcome. don;t forget to set the question as solved :D – Slow Trout Apr 17 '15 at 04:28
0

If the numbers are separated by spaces, use

file.nextInt();

Full Code:

int[] nums = new int[10];
for(int i = 0; i < 10; i++){
    nums[i] = file.nextInt();
    nums[i] *= 10;
}

after writer.println("");

for(int i = 0; i < 10; i++){
    writer.println(nums[i]);
}
Dan12-16
  • 351
  • 1
  • 8