-4

I'm trying to multiply numbers from a text file using JAVA but have no idea how.

4
12 4
16 8
14 1
12 8

The first number on the first line represents how many people there are within the document (line 2 "12 4" being the first person "12 8" being the last.)

4 (number of employees)
32 (hours employees have worked) 8 (wage per hour)
38 6
38 6
16 7

I'm trying to find how Java can skip line 1, read line two and multiply the two numbers and then do the same for the other lines.

Please can anyone explain how I could do this?

Cheers!

rgettman
  • 176,041
  • 30
  • 275
  • 357
user3071724
  • 1
  • 1
  • 4
  • 6
    What about show us some effort. What have you tried? – Jorge Campos Dec 05 '13 at 19:41
  • Even a novice can put in a little work. – Keppil Dec 05 '13 at 19:43
  • google is there for you – dev2d Dec 05 '13 at 19:43
  • How about using a List to store the data read from the file. Also retrieval by using the index can help you to get the element at the nth position if it is a fixed length flat file. Just a suggestion. – tarares Dec 05 '13 at 19:45
  • I first read the document with a buffered reader and printed it to the screen. is there a way of saving the specific numbers to a string so that I can use them in multiplication? I openly admit I'm a novice, but if anyone can provide a tutorial link etc, I'd be grateful – user3071724 Dec 05 '13 at 19:46
  • 2
    @user3071724 Try to go through [Java input file loop](http://stackoverflow.com/questions/20227937/java-input-file-loop/20253209#20253209). Here something similar has been done. – Smit Dec 05 '13 at 19:47

7 Answers7

3

Here are all the things you need:

  1. To read a file: Java: How to read a text file
  2. To cast to integer: How to convert a String to an int in Java?
  3. To multiply: use * operator
Community
  • 1
  • 1
noMAD
  • 7,744
  • 19
  • 56
  • 94
1

The assumption is that you have been given this as an assignment for homework and either you have not paid attention in class. The key to learning programming, is breaking the task down into manageable tasks.

In your case, write a program to read the input file and echo the output. Once you have that, you are part of the way there.

Next, put the numbers into integer variables. This will involve casting.

Then perform that math and spit out the output.

By performing small tasks, the problem becomes much easier.

Pete B.
  • 3,188
  • 6
  • 25
  • 38
1

I don't really understand your criteria unfortunately, but you seem to be able to understand the formula, and that's all you need. I recommend BufferedReader and FileReader to get the text from the file. From there, it's a good idea to start parsing your Strings around the spaces (there's a method for that), and you can then convert them to integers. Hopefully this puts you on the right track.

Kevin Busch
  • 712
  • 1
  • 6
  • 8
1

You definately need to read upon programming as a whole at first, because this should be one of the basic questions you should be able to answer.

I can give you a rough sketch on how to do it:

  1. Construct a File object out of the place where the 'document' resides.
  2. Read the input, definetely for novices, I would recommend using the Scanner class.
  3. Read the first Integer or Line, save this number.
  4. Read the following pair of Integers or Lines.
  5. Multiply the read numbers.
  6. Output the multiplication result.

You also might want to check some other concerns:

  • Should your program break on wrong input?
  • Should your program continue on wrong input?

You should take those things in consideration when reading either the Integers or the Lines out of the document.

skiwi
  • 66,971
  • 31
  • 131
  • 216
1

I see you are having problems with BufferedReader. So to avoid the overhead of type conversion, you may consider using Scanner. In your case,

Scanner scanner = new Scanner ("Path to your file with backslash characters escaped");
int myNumber = scanner.nextInt();

It will directly assign the number from the file to your variable.

KodingKid
  • 971
  • 9
  • 14
1
  1. Create a scanner to read the file
  2. Skip the first line
  3. Iterate over all the numbers
  4. Multiply number pairs
  5. Do something with those numbers
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;

    public class Test {
        public static void main(String[] args) throws FileNotFoundException {
            // 1. Create a scanner to read the file
                Scanner file = new Scanner(new File("textfile.txt"));
            // 2. Skip the first line
                file.nextLine();
            // 3. Iterate over all the numbers
                while(file.hasNextInt()) {
                    int hours = file.nextInt();
                    int wagePerHour = file.nextInt();
            // 4. Multiply number pairs
                    int totalWage = hours * wagePerHour;
            // 5. Do something with those numbers
                    <do something with the numbers>
                }
        }
    }
quantumkayos
  • 115
  • 6
  • Skipping the first line does not seem to be what the OP wants. Imagine that the application will need to do something else than outputting the data in the future. Then a whole array of errors can pop up if the program would be allowed to parse non-specified (beyond the number_of_employee_count inputs) inputs. – skiwi Dec 05 '13 at 20:37
  • "I'm trying to find how Java can skip line 1, read line two and multiply the two numbers and then do the same for the other lines." I assume these numbers will be in their own file, particularly because this looks a lot like an educational assignment. – quantumkayos Dec 05 '13 at 20:38
  • Sorry, my bad, I haven't read it. While the answer is now what the OP wants, I still think it is a bad practice to do so. – skiwi Dec 05 '13 at 20:39
1

This will skip the first line and print out the multiplied values for every following line. This is assuming that you are passing a file as a command-line argument.

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

public class Multiply{
    public static void main(String[] args) throws FileNotFoundException{
        File file = new File(args[0]);

        Scanner sc = new Scanner(file);

        // skip the first line
        sc.nextLine();

        int num_people;
        int hours;
        int total;

        while(sc.hasNext()){
            num_people = sc.nextInt();
            hours = sc.nextInt();

            total = num_people * hours;
            System.out.println(total); 
        }
    }
}
user2121620
  • 678
  • 12
  • 28
  • Skipping the first line is not a good approach. The number is there for a reason, see my comment on @quantumkayos for more information. – skiwi Dec 05 '13 at 20:37