Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to add two numbers in the same row of a file

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to add two numbers in the same row of a file

    Hello i need to add two integers in the same row of a file, separated by tab

    my file abc.txt has the following entry
    12 123
    15 456

    my program needs to add 12 with 123 and 15 with 456

    i am being able to split the two entries in a row and convert them to integer but i dont know how to treat them as separate numbers and add them

    for example if i try to add then 12 adds with 12 and 123 adds with 123. wheres it should be 12+123

    here is my program

    import java.io.*;
    public class test {
    public static void main(String[] args) {
    String s = "";
    FileInputStream finp = null;
    InputStreamReader inpr = null;
    BufferedReader br = null;
    try {
    finp = new FileInputStream(args[0]);
    inpr = new InputStreamReader(finp);
    br = new BufferedReader(inpr);

    } catch (FileNotFoundException e) {

    e.printStackTrace();
    }
    try{
    while(true){
    s = br.readLine();
    if(s==null)
    break;
    for(int i =0;i<2;i++){

    String [] addrs = s.split("\t");
    int a = Integer.parseInt(addrs[i]);
    System.out.println(a+a);}
    }
    }catch(IOException e){

    e.printStackTrace();

    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,404
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: How to add two numbers in the same row of a file

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    how to treat them as separate numbers and add them
    Put the values of each number into a separate int variable and add the two variables together.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: How to add two numbers in the same row of a file

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

Similar Threads

  1. Need a different way to add Cubed numbers 1-10
    By Dtank123456 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2014, 02:02 PM
  2. Replies: 2
    Last Post: December 12th, 2013, 11:01 PM
  3. [SOLVED] Find the the longest decreasing row of numbers in a vector
    By einar123 in forum What's Wrong With My Code?
    Replies: 27
    Last Post: September 24th, 2013, 09:55 AM
  4. Beginniner - Add Row to JTable
    By steme in forum Java Theory & Questions
    Replies: 0
    Last Post: June 8th, 2013, 02:59 PM
  5. Add or remove a row on a jtable with Netbeans 6.0
    By Pieter in forum Java Theory & Questions
    Replies: 1
    Last Post: July 8th, 2010, 02:40 PM