Results 1 to 6 of 6
  1. #1
    ratiug is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Simple Adding Programme

    Hello.

    I need help with a simple programme.

    The programme is feed two(or more) text files.

    Each text file contains names and numbers e.g:

    Bob 56
    Mark 78
    Laura 34
    Joeseph 93
    Sarah 100


    The programme needs to scan each text file and if
    the scanner finds the same name in two or more
    of the files, it needs to add the two numbers together
    and then output all of the names and numbers.

    For example, if the second text file is

    Jenny 67
    Samual 45
    Mark 11
    Sarah 34
    Gordon 89

    Then the output would be:


    Bob 56
    Mark 89
    Laura 34
    Joeseph 93
    Sarah 134
    Jenny 67
    Samual 45
    Gordon 89

    Here is my initial attempt

    import java.io.*;
    import java.util.*;

    public class Top50 {

    public static void main(String[] args) throws IOException {

    ArrayList stuRec = new ArrayList();
    File file = new File("c:\\xxx.txt");
    try {
    Scanner scanner = new Scanner(file).useDelimiter("$");

    while (scanner.hasNextLine())
    {
    String Name = scanner.next();
    int Score = scanner.nextInt();
    int TotScore = scanner.nextInt();
    System.out.println(Name + " " + Score + " " + TotScore);
    }
    }
    catch (FileNotFoundException e)
    {
    e.printStackTrace();
    }
    }

    Do I need to keep the names and numbers in arrays?

    And i think I need a boolean operator in there somewhere to compare each name

    Any help would be appreciated,
    Thankyou

  2. #2
    stevenfriz is offline Member
    Join Date
    Feb 2012
    Posts
    28
    Rep Power
    0

    Default Re: Simple Adding Programme

    Please use the HTML code tags when you post Java code.
    You do need a boolean operator to compare the two Strings. So if Jenny and Jenny appeared in two different files, a "flag" or boolean would have to be set to true.
    Also, if I'm looking correctly, you're only scanning one file. You need two scanners to scan both, or add the files to an array or array list, get that file, then scan it; optionally.

  3. #3
    ratiug is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Simple Adding Programme

    Ah so I need a scanner for each txt file?
    Thanks, this has helped me think better.

    Sorry I'm not sure how to use html code tags.
    Do you mean:

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class Top50 {
    
    public static void main(String[] args) throws IOException {
    
        File file1 = new File("c:\\xxx.txt");
        File file2 = new File("c:\\yyy.txt");
    	
        try {
            Scanner scanner1 = new Scanner(file1).useDelimiter("$");
    	Scanner scanner2 = new Scanner(file2).useDelimiter("$");
    
            while (scanner1.hasNextLine())
            {
                String Name1 = scanner1.next();
                String Name2 = scanner2.next();
    		
    	if (Name1==Name2) {
    
            int tot = scanner1.nextInt()+scanner2.nextInt();
    
    	System.out.println(Name1 + " " + tot);
    	}
    
    	
    	else {
                int score1 = scanner1.nextInt();
                int score2 = scanner2.nextInt();
              
                System.out.println(Name1 + " " + score1);
                System.out.println(Name2 + " " + score2);
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
    }
    I think this is an improvement, however it still has a lot of problems.

    First of all I don't think I have the right parameter for the while loop.

    Also, I think that this current programme is only comparing the first string
    in the first text file with the first string in the second file ( and so on)
    whereas it needs to compare every string in the first file with every string in
    the second file.

    Any help would be very much appreciated.

    Thank you.
    Last edited by sunde887; 04-05-2012 at 01:10 AM. Reason: Added code tags. [code]YOUR CODE HERE[/CODE]

  4. #4
    stevenfriz is offline Member
    Join Date
    Feb 2012
    Posts
    28
    Rep Power
    0

    Default Re: Simple Adding Programme

    By code tags, you would type "["code"]" and "["/code"]" (without quotes).
    Your next attempt is an improvement. The while loop is only testing to see if Scanner1 has a next line. Scanner2 needs to be tested somehow. When you compare Name1 and Name2 you need to use either .equals() or .compareTo(). I can't remember off the top of my head if compareTo is for strings though.

    So:
    if(Name1.equals(Name2)){
    //code
    {

    Or:
    if(Name1.compareTo(Name2)){
    //code
    }

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Simple Adding Programme

    Quote Originally Posted by ratiug View Post
    Ah so I need a scanner for each txt file?
    It depends upon what you mean. If you create 2 variables and 2 Scanners then do not do this. Simply reuse the same variable to create a new Scanner for each file.

  6. #6
    ratiug is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Simple Adding Programme

    Hello everyone.

    This is where I'm at with 'Simple Adding Programme':

    The lists I am using are:

    List1:
    Bob 56
    Mark 89
    Laura 34
    Joeseph 93
    Sarah 134
    Jenny 67
    Samual 45
    Gordon 89

    List2:
    Jenny 13
    Bob 99
    Mark 11
    Sarah 34
    Gordon 69
    Fred 6
    Jake 0
    Digby 100

    The code is:

    Java Code:
    import java.io.*;
    import java.util.*;
     
    public class Top50 {
     
    public static void main(String[] args) throws IOException {
     
        File file1 = new File("c:\\list1.txt");
        File file2 = new File("c:\\list2.txt");
         
        try {
        Scanner scanner1 = new Scanner(file1);
        Scanner scanner2 = new Scanner(file2);
    
        String Name1 = scanner1.next();
            while (scanner1.hasNext())
            {
                
                if (Name1.equals(scanner2.next())){
               System.out.println("These two strings are the same");
            int tot = scanner1.nextInt()+scanner2.nextInt();
     
        System.out.println(Name1 + " " + tot);
        }
             
       else {
                
               System.out.println("different");
            }
        }
      
    
    
    }catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
    }
    }
    And the output:

    different
    different
    These two strings are the same
    Bob 155
    different
    different
    different
    different
    different
    different
    different
    different
    different
    different
    different
    different


    The programme is starting to work. It has successfully compared 'Bob' with every token in list2 and printed out the combined values
    when it found another 'Bob'.

    But how do it get the programme to do this not just for 'Bob' but every name in list1?

    Thanks, any help is appreciated

Similar Threads

  1. Need help with first Java programme.. Very new!
    By xxdave in forum New To Java
    Replies: 3
    Last Post: 11-22-2011, 05:46 PM
  2. Help with BinarytoDecimal programme
    By Thompson in forum New To Java
    Replies: 3
    Last Post: 02-23-2011, 11:43 PM
  3. this programme dont work as I want Help me!!
    By pinar in forum New To Java
    Replies: 2
    Last Post: 10-27-2010, 12:27 AM
  4. cannot compile the programme
    By Roshini in forum New To Java
    Replies: 3
    Last Post: 09-06-2010, 12:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •