Results 1 to 6 of 6
Thread: Simple Adding Programme
- 04-04-2012, 05:49 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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
- 04-04-2012, 05:55 AM #2
Member
- Join Date
- Feb 2012
- Posts
- 28
- Rep Power
- 0
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.
- 04-04-2012, 06:33 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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:
I think this is an improvement, however it still has a lot of problems.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(); } }
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 12:10 AM. Reason: Added code tags. [code]YOUR CODE HERE[/CODE]
- 04-04-2012, 11:53 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 28
- Rep Power
- 0
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
}
- 04-05-2012, 06:37 AM #5
- 04-13-2012, 05:11 AM #6
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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:
And the output: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(); } } }
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
-
Need help with first Java programme.. Very new!
By xxdave in forum New To JavaReplies: 3Last Post: 11-22-2011, 04:46 PM -
Help with BinarytoDecimal programme
By Thompson in forum New To JavaReplies: 3Last Post: 02-23-2011, 10:43 PM -
this programme dont work as I want Help me!!
By pinar in forum New To JavaReplies: 2Last Post: 10-26-2010, 11:27 PM -
cannot compile the programme
By Roshini in forum New To JavaReplies: 3Last Post: 09-06-2010, 11:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks