#16
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0
    Originally Posted by NormR
    What is printed out by the program?

    What is the value of inFile.hasNextLine() just before the while() loop starts executing?

    One problem I see is that there are many calls to methods that will read data from the file inside of the while() loop without the hasNext test to see if there is data.

    Another problem is that the printf() statement does not have a newline ("\n") character at the end so all its output will be on one line.
    The header is printed out
    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
  2. #17
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    The newline character in a String is "\n"
    For example:
    "this is a string that ends with a newline character\n"
  4. #18
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0
    I don't understand your third point
    Really, isn't it always % in printf?
  6. #19
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    isn't it always % in printf?
    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.
    Code:
    "%10s %5d %5i %10d"
    The String you are using has space characters in it.

    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.
  8. #20
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0
    Sorry for jumping around, but quick question: why are numbers with more decimals stored more accurate than those with less?
  10. #21
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    No idea. Perhaps there are some useful concepts here:
    http://docs.oracle.com/cd/E19957-01/..._goldberg.html
  12. #22
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Fixed them


    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
    Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Scanner;
    
    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)
            throws FileNotFoundException
        {
        	String word;
        	int index;
            ArrayList <String> wordsInFile=new ArrayList <String>();
            Scanner inFile=new Scanner (new File (fileName));
            inFile.useDelimiter("[^\\p{Alpha}]+");
            while (inFile.hasNext())
            {
            	word=inFile.next();
            	index=Collections.binarySearch(wordsInFile, word);
                if (index<0)
                	wordsInFile.add(-index-1,word);
            }
            inFile.close();
            return wordsInFile;
        }
    
    
        /**
          * 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);
            	if (index<0)
            		inFirstOnly.add(-index-1,next);
            }
            return inFirstOnly;
        }
    
    
        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 ();
    
            System.out.printf (
                    "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");
            keyboard.close();
        }
    }
    The problem is in the first or second method, here are the files
    Code:
    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.
    Code:
    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.
  14. #23
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    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.
  16. #24
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0
    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
  18. #25
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    it crashes after that
    First you need to fix the problem causing the crash. Copy the full text of the error message and paste it here.
  20. #26
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0
    ArrayList<E>.rangeCheckForAdd(int) line: not available
    Also says source not found, whatever that means
  22. #27
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Can you copy the FULL text of the error message and post it here? Not just one line.
  24. #28
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0
    That is the full text, it comes up after I run in the debug tab
  26. #29
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0
    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
  28. #30
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    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 07:43 PM.

IMN logo majestic logo threadwatch logo seochat tools logo