September 22nd, 2013, 05:58 PM
The header is printed out
Originally Posted by NormR
Product Name Price ($) Quantity Total Value ($)
The file as a next line, so true
I don't understand what oyu mean
Ok, just added %n
September 22nd, 2013, 06:02 PM
The newline character in a String is "\n"
"this is a string that ends with a newline character\n"
September 23rd, 2013, 09:48 AM
I don't understand your third point
Really, isn't it always % in printf?
September 23rd, 2013, 10:10 AM
Not at all. The String can contain anything. The %X items are what is to be replaced by the printf method. The rest of the String is left alone.
The String you are using has space characters in it.
"%10s %5d %5i %10d"
Did you try adding different items to the String to see what happens?
Your computer won't break if you try different things in a program.
September 23rd, 2013, 06:57 PM
Sorry for jumping around, but quick question: why are numbers with more decimals stored more accurate than those with less?
September 23rd, 2013, 07:00 PM
September 28th, 2013, 09:50 PM
Ok got them fixed up
The problem with inventory is my class was corrupted and the printf was a bit off
In triples, there was some checking of the same thing, and the method used to generate the starting number was wrong, I used triple and compared it to that divided by 3, I started with 33333, but there were numbers less than 11111 that worked
I have another problem now, this code checks for words in a file, and see if they're in another file
The problem is in the first or second method, here are the files
public class FileBinaryCompare
* Finds and returns a list of all of the words in the given file in lower
* case and in alphabetical order. If a word appears more than once in the file,
* it will appear only once in the List (i.e. The List will have no duplicates)
* @param fileName
* the name of the file
* @return a List of all of the words in this file
* Postcondition: The returned list should contain only lowercase words
* and it should be in alphabetical order
* @throws FileNotFoundException
* if the given file doesn't exist
public static ArrayList < String > wordsInFile (String fileName)
ArrayList <String> wordsInFile=new ArrayList <String>();
Scanner inFile=new Scanner (new File (fileName));
* Find the words that are in the first List but not in the second List
* @param first the first List
* @param second the second List
* Precondition: Both of the given Lists will be in alphabetical order
* @return a List of words that are in first but not in second
* Postcondition: The returned List of words should be in alphabetical order
public static ArrayList < String > inFirstOnly (ArrayList < String > first,
ArrayList < String > second)
ArrayList<String> inFirstOnly=new ArrayList<String>();
for (String next:first)
int index=Collections.binarySearch(second, next);
public static void main (String args) throws FileNotFoundException
// Set up a keyboard for file name input
Scanner keyboard = new Scanner (System.in);
// Input the names of the two files
System.out.println ("Comparing Words in Two Files");
System.out.print ("Enter the name of the first file: ");
String firstFileName = keyboard.next ();
System.out.print ("Enter the name of the second file: ");
String secondFileName = keyboard.next ();
// Read in the files and find a list of all of the words in
// the first file that are not in the second file
long start = System.nanoTime ();
// Create word lists for each file using the method above
ArrayList <String> wordsInFirst = wordsInFile (firstFileName);
ArrayList <String> wordsInSecond = wordsInFile (secondFileName);
// Create a list of all of the words that are in first but not second
ArrayList <String> wordsToFind = inFirstOnly (wordsInFirst, wordsInSecond);
long stop = System.nanoTime ();
"There are %d words in \"%s\" that are not in \"%s\"%n",
wordsToFind.size (), firstFileName, secondFileName);
int elementsToDisplay = Math.min (wordsToFind.size (), 20);
System.out.println ("The words are: " +
wordsToFind.subList (0, elementsToDisplay));
System.out.printf ("Run time: %.1f ms%n", (stop - start) / 1000000.0);
System.out.println ("End of Program");
Your task is to find all of the words in this file that are not in the second file. For example, this file contains "giraffe" and "elephant" and the other file does not. There may be words in the second file that are not in the first file, but we donít care about those words.
Your task is to find all of the words in the first file that are not in the this file. There may be words in this file (example: "monkey"), that are not in the first file but we donít care about these words. We only want the words from the first file that are not in the second file.
September 28th, 2013, 09:53 PM
What is the problem? Can you describe what the program does and what is wrong with it?
Copy the program's output, paste it here and add some comments to it saying what the problem is.
September 29th, 2013, 09:12 AM
The program sees which words are in the one file, but not in the other (using a binary search)
At the start of the program, it asks for the two file names, but then it crashes after that
September 29th, 2013, 09:16 AM
First you need to fix the problem causing the crash. Copy the full text of the error message and paste it here.
September 29th, 2013, 07:19 PM
ArrayList<E>.rangeCheckForAdd(int) line: not available
Also says source not found, whatever that means
September 29th, 2013, 07:23 PM
Can you copy the FULL text of the error message and post it here? Not just one line.
September 29th, 2013, 08:27 PM
That is the full text, it comes up after I run in the debug tab
September 29th, 2013, 08:30 PM
Ok there's actually 3 more
ArrayList<E>.add(int, E) line: not available
FileBinaryCompare.inFirstOnly(ArrayList<String>, ArrayList<String>) line: 67
FileBinaryCompare.main(String) line: 94
September 29th, 2013, 08:31 PM
The compiler can not find an add() method for ArrayList that takes the args used on line 67.
Without a good error message I'm only guessing where the error is and what the error is.
Last edited by NormR; September 29th, 2013 at 08:43 PM.