Java-Read pairs of large numbers from file and represent them with linked list, get the sum and product of each pair -


data file looks below

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

result supposes looks below

12234123  123452622  sum: 125686745  product:1510334562...  1000010  20000  sum: 1020010  product: 20000200000 

can not use utility library such java.math.bigdecimal javautil.linkedlist etc.

  1. i dont know how display each number on 1 row

my result like

12  234  123 

but need display on 1 row as

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

shown on below:

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

i dont know how modify it.

for method of sum , method of product. hope can give me clues.

here 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);         }     } } 

well, i'll address 2 points, should read on i/o here.

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

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

since you're consuming input line line, you'll read whole number in already. string.replace(",", "") method take care of commas. sum , product, think adder, using string representations addition , multiplication same way hand.

2) there empty line between each pair of numbers. can first pair of numbers since while statement works when line not null.

the null check eof, or end of file. according javadocs bufferedreader, blank line equivalent "" + "\n", you'll read blank line. check if line.equals("") , nothing.

edit: addressing updated post

  1. you need avoid resource leaks , create file pass filereader. take @ appropriate constructors.
  2. i've refactored deal fact have multiple test cases
  3. you shouldn't split on comma delimiter, because want single line of output, correct?
  4. finally, logic, research on bignums, how biginteger implemented, , try create own simple implementations.

here's fixes i/o implemented. should read more on topic, free focus on bignum algorithms, suspect point of 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 input 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 } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -