0

data file looks like below

12,234,123
123,452,622
(here is a empty line)
1,000,010
20,000

result supposes to looks like below

12234123

123452622

sum: 125686745

product:1510334562...

1000010

20000

sum: 1020010

product: 20000200000

can not use any utility library such as java.math.BigDecimal javautil.LinkedList etc.

  1. I dont know how to display each number on one row

my result is like

12

234

123

but I need to display it on one row as

12234123
  1. the problem of the space, there is a empty line between each pair of numbers I only can get first pair of numbers since my while statement works only when line is not null.

Shown on below:

while((Line = File.readLine())!= null)

I dont know how to modify it.

for the method of sum and the method of product. hope someone can give me some clues.

here is the code. (blank line problem solved)

public static void main (String[] args){
    if (args.length == 0)
        System.out.println("No file specified.");
    else{
        FileReader theFile;
        BufferedReader inFile;
        String num1,num2;
        try{
            theFile = new FileReader(args[0]);
            inFile = new BufferedReader(theFile);
            while((num1 = inFile.readLine())!= null){
                if(num1.length()==0){   
                }
                else{
                num2 = inFile.readLine();
                num1.replace(",", "");      
                num2.replace(",", "");
                System.out.println(num1);
                System.out.println(num2);
                }
            }
        }
            catch (Exception e) {
            System.out.println(e);
        }
    }
}
dunnopro
  • 47
  • 1
  • 7
  • You are asking people to help you fix your code. You should post your code in that case. – Erwin Bolwidt Jul 14 '15 at 03:02
  • While this is much better, a style suggestion would be to clean up the levels of indentation, especially with if-else. Also, I/O object declarations can be nested, since you never refer to the inner objects by name. Finally, either use try-with-resources or close your resources in a finally block. – Aaron Zou Jul 14 '15 at 04:55
  • thank you for help. @Erwin Bolwidt I already post the code. if you dont mind. please take a look at it. – dunnopro Jul 14 '15 at 05:00
  • Okay, everything's refactored. If you're satisfied with my answer, do you mind accepting it (checkmark)? Let me know if you need any other pointers, but I think you should be well on your way! – Aaron Zou Jul 14 '15 at 05:21

1 Answers1

2

Well, I'll address your two points, but you should read up on I/O here.

1) I don't know how to display each number on one row.

while ((line = file.readLine()) != null)

Since you're consuming your input line by line, you'll read the whole number in already. The String.replace(",", "") method will take care of your commas. As for your sum and product, think like an adder, using the String representations to do the addition and multiplication the same way you do by hand.

2) There is a empty line between each pair of numbers. I only can get the first pair of numbers since my while statement works only when line is not null.

The null check is for EOF, or end of file. According to the Javadocs for BufferedReader, your blank line is really equivalent to "" + "\n", so you'll actually read a blank line. So just check if line.equals("") and do nothing.

EDIT: Addressing Updated Post

  1. You need avoid resource leaks and create a File to pass to the FileReader. Take another look at the appropriate constructors.
  2. I've refactored to deal with the fact you have basically multiple test cases
  3. You shouldn't split on the comma delimiter, because you want a single line of output, correct?
  4. Finally, for your logic, do some research on bignums, how BigInteger is implemented, and try to create your own simple implementations.

Here's all the fixes for your I/O implemented. You should read up more on the topic, but this will free you to focus on the bignum algorithms, which I suspect is the point of this exercise.

private static String add(String num1, String num2) {
    // implement
}

private static String multiply(String num1, String num2) {
    // implement
}

try (
    BufferedReader reader = new BufferedReader(new FileReader(new File(args[0])));
) {
    String num1, num2;
    while ((num1 = reader.readLine()) != null) {
        num2 = reader.readLine();       // assumes your input is formatted correctly
        num1.replaceAll(",", "");       // "delete" commas
        num2.replaceAll(",", "");
        System.out.printf("%s\n", num1);
        System.out.printf("%s\n", num2);
        System.out.printf("sum: %s\n",     add(num1, num2));
        System.out.printf("product: %s\n", multiply(num1, num2)); 
        reader.readLine();              // consume blank line
    }
} catch (IOException e) {
    System.err.println(e.getMessage()); // error output
}
Aaron Zou
  • 414
  • 3
  • 9
  • `line.equals("")` you mean! (or `line.trim().length() == 0`). Never compare strings using `==` ! – Erwin Bolwidt Jul 14 '15 at 03:14
  • You're completely right. I've been juggling different languages recently and this was a quick post. Checking that the references are equal is definitely the wrong behavior, thanks. – Aaron Zou Jul 14 '15 at 03:15
  • thank you for help. @AaronZou but since the type of my instance variable is string[] not string. I can not use the method "String.replace"? I use "split" since I only know it and "StringTokenizer" to deal with such problem. – dunnopro Jul 14 '15 at 05:08
  • Why do you need a String[ ] ? It doesn't make sense to treat "123,456" as ["123", "456"]. I guess you can print them together, but you'll have to join them back together when you're doing your calculations anyway? – Aaron Zou Jul 14 '15 at 05:09
  • @AaronZou it because I need to store integer between 0-999 in each node. e.g. 15,123,012 needs to be separated as 3-node list. I use the method of replace, but it didnt work. I updated the new code. – dunnopro Jul 15 '15 at 04:38
  • Oops, should be replaceAll() rather than replace(). If you're just trying to print out the number, why does it need to be a list with three elements? – Aaron Zou Jul 15 '15 at 04:44
  • @AaronZou actually I need to store numbers as a linkedlist or two probably, this is requirement, but I just want to print out the number first. and I did try replaceAll(",", "") but it didnt work either. the "," still exist – dunnopro Jul 15 '15 at 04:49