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 25 of 25

Thread: New assignments for JAVA beginners

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default New assignments for JAVA beginners

    Hello all. Sorry for not being too active, I have had 3 tests last week and 2 papers in the past 2 weeks, along with a knee injury this last weekend and have not had time to browse all of my forums.

    But I have this new assignment due this Friday that's quite tough, just starting on it right now and will hopefully post some things tonight to see what you guys think and can help with.

    The specifications for it are quite technical, but I'll post the link here for the assignment instructions:

    Assignment 7: CS 160 Foundations in Computing

    I'll hopefully have some code later tonight to put up.

    -Peter
    Last edited by Peetah05; April 24th, 2009 at 11:35 AM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: New Assignment that's tough

    Hello Peter,

    What file do you wish to spell check? Or type quit when done. LibertyQuote.txt
    Words not in dictionary from file: LibertyQuote.txt
    homeles
    tempst
    tosed
    lammp
    What file do you wish to spell check? Or type quit when done. Gettysburg.txt
    Words not in dictionary from file: Gettysburg.txt
    grond
    dedicatd
    birt
    What file do you wish to spell check? Or type quit when done. quit
    You can use the Scanner class to do this. Please take a look at these tutorials:

    http://www.javaprogrammingforums.com...ner-class.html

    http://www.javaprogrammingforums.com...ner-class.html

    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New Assignment that's tough

    This is what I have so far. I'm kinda stuck and some of this code might be wrong. If some of you could start me off and get the first parts done that would be great and then I could go from there.

    The format I see that it has to be is this:

    Access the dictionary file, initialize the dictionary array, then create a new int with the number of words in the dictionary file.

    Then create a while loop to build the dictionary array. Then grab the word from the file, store it into [i] then do the next word and so on...

    Then make another while loop to continue prompting for a file until someone types quit.

    Then make the spell checking happen by checking to see if each word stored into the array is in the file.

    And then I'm not sure.

    But here's the code I have tried to create so far:

    import java.util.Scanner;
    import java.io.FileReader;
    import java.util.Arrays;
     
    public class SpellCheck {
     
    	public static void main(String[] args) {
     
    		int array[] = new int[95877];
    		String[] words;
    		String userChoice = "";
    		Scanner in = new Scanner(System.in);
     
    		System.out.println("What file do you wish to spell check? Or type quit when done.");
    		String dataFile = in.next();
            FileReader fr = new FileReader(dataFile);
            Scanner in1 = new Scanner(fr);
     
            words = in1.nextLine().split("/n");
            in1.close();
     
            while (userChoice.equals("no")) {
            	int i = 0;
     
            }
    	}
    }
    Last edited by Peetah05; April 22nd, 2009 at 03:57 PM.

  4. #4
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New Assignment that's tough

    Anyone? I want to get some more progress on this tonight if possible

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Red face Re: New Assignment that's tough

    Quote Originally Posted by Peetah05 View Post
    Anyone? I want to get some more progress on this tonight if possible
    Hey Peter,

    I didn't have chance to look at this last night before I went to bed.

    I'm looking into it now...
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: New Assignment that's tough

    This is untested but I imagine something like this would be how you would load the dictionary file...did it in a few secs so probably errors.
    package SpellCheck;
     
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.Scanner;
     
    public class SpellCheck {
    	public static void main(String[] args){
    		int dictSize =0;
    		Scanner dictFile = null;
    		String[] dict;
     
    		if(args.length < 2){
    			System.out.println("Usage: java SpellCheck <dict file>");
    			return;
    		}
     
    		try{
    			dictFile = new Scanner(new FileReader(args[1]));
    		}catch(FileNotFoundException fnfe){ System.err.println(fnfe.getMessage()); }
     
    		try{
    			dictSize = Integer.parseInt(dictFile.next());
    		}catch(NumberFormatException nfe){ System.err.println("Invalid Number in file"); }
     
    		dict = new String[dictSize];
    		for(int i = 0; dictFile.hasNext(); i++)
    			dict[i] = dictFile.next();
     
    	}
    }

    Chris

  7. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: New Assignment that's tough

    Hey Chris, long time no see!

    I'm not far off completing this example for you Peter.. Ill post what I have asap.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: New Assignment that's tough

    Hello Peter,

    This is what I have so far. I've completed all steps except for Part D.

    Don't forget you need to send dictionary.txt as a command line argument when running this program.

    The current word is the String line and the dictionary words are stored in the dictLine[] array.

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
    public class SpellCheck {
     
        public String[] dictLine = new String[95878];
     
        public static int index;
        public static String dictFile;
        public static String myFile;
        public static boolean correct;
     
        public void dictToArray() {
     
            File file = new File(dictFile);
     
            try {
                Scanner scanner = new Scanner(file);
     
                // Add each dictionary word to Array
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    dictLine[index] = line;
                    index++;
                }
     
                scanner.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
     
        public void spellChecker() {
     
            File file = new File(myFile);
     
            try {
                Scanner scanner = new Scanner(file);
                scanner.useDelimiter("[\\s\\n]");
     
                while (scanner.hasNext()) {
                    String line = scanner.next().toLowerCase().trim(); 
     
     
                    [B]// ** Loop to check spelling goes here **[/B] [B]Part D[/B]
     
                }
     
                scanner.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.exit(1);
            }
     
        }
     
        public static void main(String[] args) {
     
            // Error handling for no command line arguments
            if (args.length > 0) {
                dictFile = args[0];
            } else {
                System.out
                        .println("You need to specify the dictionary file in the command line!");
                System.exit(0);
            }
     
            SpellCheck spell = new SpellCheck();
            // Add dictionary file to array
            spell.dictToArray();
     
            // Start program
            System.out
                    .println("What file do you wish to spell check? Or type quit when done.");
     
            try {
                Scanner scanner = new Scanner(System.in);
                while (scanner.hasNext()) {
     
                    myFile = scanner.next();
     
                    if (myFile.equals("quit")) {
                        System.out.println("Bye!");
                        System.exit(1);
                    }
     
                    System.out.println("Spell check file: " + myFile);
     
                    spell.spellChecker();
     
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
     
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  9. The Following User Says Thank You to JavaPF For This Useful Post:

    Peetah05 (May 1st, 2009)

  10. #9
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New Assignment that's tough

    I put together a little loop, not sure if it's right...but I'm not quite sure how I can store these words so that I can later print them out as the ones that are not in the dictionary. This is what I figured should be done in the loop, not sure where to go from here though.

    boolean found = false;
    int i = 0;
    while (scanner.hasNext()) {
                    String line = scanner.next().toLowerCase().trim(); 
                    if (line[i].equals(myFile))
                           found = true;
                    else
                          i ++;

    Again I'm not sure if this is correct, I'm just not as familiar with loops dealing with strings, not ints.
    And I tried to run the program the way it was posted above and it said that it could not find the main class. Not sure why that happened.
    Last edited by Peetah05; April 23rd, 2009 at 06:26 PM.

  11. #10
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New Assignment that's tough

    I guess I'm also a bit confused how the whole code is separated in two parts and the main method is at the bottom area and how it can't find the main class. If you could explain this concept b/c I'm quite curious, never seen this type of code before. And if you could solve this part D part because I'm quite clueless to go from here. I just need to get this thing running with those sample files given on the assignment page.

  12. #11
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New Assignment that's tough

    Bump, I really need to get this done. I've tried all I could with this, can't get that loop to work and need this done by 11:00am tomorrow. Anyone?

  13. #12
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New Assignment that's tough

    Crap...I tried doing an if statment saying if(line.equals(dictLine) but that didn't work....

    JavaPF if you complete this code in the morning I would really appreciate it. It's due at 11am MT and I'll be up around 8am to try and work on it again but I doubt I'll be able to do it.

  14. #13
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: New Assignment that's tough

    If you copied the code exaclty, then you need to name the file SpellCheck.java then it will locate the main class without a problem. Note: if you have it set up in a package you will also need to tell it what package it is part of!

    You are after something like this.
    while (scanner.hasNext()) {
                    String line = scanner.next().toLowerCase().trim(); 
     
                    for(String word:dictLine){
                    	if(line.equals(word)){
                    		// found the word
                    	}
                    }
                }

    of course I've left some out, but you get the idea. may I just add that you do need to try because it is your course. Also if you are after extra merit, you could do a binary search or something to bring the time taken to find if the word is a match down from O(N) (in this case O(95878)) to a meer O(log n) which is approx 4.9 time units. Which is 19500 times faster....per word. i know which way i would go xD since the dict file is already sorted alphabetically you wouldn't need to do that

    Chris
    Last edited by Freaky Chris; April 24th, 2009 at 03:15 AM.

  15. #14
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: New Assignment that's tough

    Warning: This is a bigger and more complex assignment than any of the previous assignments! START EARLY.
    This is another assignment you have left late to finish! I can't be responsible for completing all your assignment work at the last minute lol

    I guess I'm also a bit confused how the whole code is separated in two parts and the main method is at the bottom area and how it can't find the main class. If you could explain this concept b/c I'm quite curious, never seen this type of code before. And if you could solve this part D part because I'm quite clueless to go from here. I just need to get this thing running with those sample files given on the assignment page.
    This is what object oriented programming is all about. I have split the code into several methods so it is easy to work on each method rather than having a bunch of code all muddled up in the main method. The main method is still there.. The main method contains the error handling for the command line arguments, it calls the dictToArray(); method which puts the dictionary file into the array. And it also handles the main loop which takes the user input.

    I work full time during the day and am extremely busy. I try to help as much as possible on the forums in the time that I have spare.. Yesterday was dedicated to that first part of your assignment.

    I will do my best to complete this for you if possible but I can't guarantee anything.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  16. #15
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: New Assignment that's tough

    This is almost working how you would want it to.. I've included a quick example of the loop for part D but i've had no real time to test it.

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
    public class SpellCheck {
     
        public String[] dictLine = new String[95878];
     
        public static int index;
        public static String dictFile;
        public static String myFile;
        public static boolean correct;
     
        public void dictToArray() {
     
            File file = new File(dictFile);
     
            try {
                Scanner scanner = new Scanner(file);
     
                // Add each dictionary word to Array
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    dictLine[index] = line;
                    index++;
                }
     
                scanner.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
     
        public void spellChecker() {
     
            correct = false;
     
            File file = new File(myFile);
     
            try {
                Scanner scanner = new Scanner(file);
                scanner.useDelimiter("[\\s\\n]");
     
                while (scanner.hasNext()) {
                    String line = scanner.next().toLowerCase().trim();
     
                    // ** Loop to check spelling goes here **
                    // Loop to check spelling
                    for (int a = 1; a < dictLine.length; a++) {
     
                        if (line.equals(dictLine[a]) & correct == false) {
                            correct = true;
                            break;
                        }
     
                        correct = false;
     
                    }
     
                    if (correct == false) {
                        System.out.println(line);
                    }
     
                }
     
                scanner.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.exit(1);
            }
     
        }
     
        public static void main(String[] args) {
     
            // Error handling for no command line arguments
            if (args.length > 0) {
                dictFile = args[0];
            } else {
                System.out
                        .println("You need to specify the dictionary file in the command line!");
                System.exit(0);
            }
     
            SpellCheck spell = new SpellCheck();
            // Add dictionary file to array
            spell.dictToArray();
     
            // Start program
            System.out
                    .println("What file do you wish to spell check? Or type quit when done.");
     
            try {
                Scanner scanner = new Scanner(System.in);
                while (scanner.hasNext()) {
     
                    myFile = scanner.next();
     
                    if (myFile.equals("quit")) {
                        System.out.println("Bye!");
                        System.exit(1);
                    }
     
                    System.out.println("Spell check file: " + myFile);
     
                    spell.spellChecker();
     
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
     
        }
    }
    The output for the Gettysburg.txt file is:



    a


    a

    a
    a


    a

    grond





    dedicatd




    a
    birt
    You need to figure out how to get rid of the white space etc to clean up the results.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  17. #16
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New Assignment that's tough

    Thanks for the help, I'm sorry it won't happen again.

    For some reason it still can't find the main class. And when I try to run it through the configurations, it doesn't give me the option to select the run button, it is just faded. I'm not sure what's going on here.

  18. #17
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Thumbs up Re: New Assignment that's tough

    Quote Originally Posted by Peetah05 View Post
    Thanks for the help, I'm sorry it won't happen again.

    For some reason it still can't find the main class. And when I try to run it through the configurations, it doesn't give me the option to select the run button, it is just faded. I'm not sure what's going on here.
    I don't mind helping, just please don't leave things until the last minute. If you give us plenty of time before your deadline then this saves everyone a lot of stress.

    What IDE are you using? Please make sure the name of your class is SpellCheck.

    If possible, attach screenshots showing the error and the workspace. It might help me see whats going on.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  19. #18
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New Assignment that's tough

    Here's a screenshot of the error:


  20. #19
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: New Assignment that's tough

    OK looking at the errors in the console, this could be because you are not sending dictonary.txt as a command line argument which is what you have to do in your assignment.

    After working on your project I made this post:

    How to send command line arguments to Eclipse

    Please follow those instructions.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  21. #20
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New Assignment that's tough

    I did that exact same thing and it gave me the same error. Hmm...I'm not sure what's going on here. The command line arguments are setup right. Did it work for you with the same code and command line arguments?

  22. #21
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: New Assignment that's tough

    Quote Originally Posted by Peetah05 View Post
    I did that exact same thing and it gave me the same error. Hmm...I'm not sure what's going on here. The command line arguments are setup right. Did it work for you with the same code and command line arguments?
    Yes it worked fine for me. Are you sure you have copied all of the code?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  23. #22
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New Assignment that's tough

    Yes I am. I'll try it again but I have all of the code that you posted in post #15 in my eclipse window so it should be working...

  24. #23
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: New Assignment that's tough

    Try right clicking the class file and going to Run as > Java Application.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  25. #24
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New Assignment that's tough

    Ahh, that worked haha. I guess I can't run the configs on the java file. It has to be on the class file.

    That's weird though, the output didn't give me the other words that I should have as it shows in the example output on the assignments page.

  26. #25
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: New Assignment that's tough

    Glad we got it to run eventually...

    I didn't have chance to fully test the output but when I checked it was pretty close. The main code is there.. everything you need to complete it and get it working.

    I can't go any further with this today as i'm about to leave work and im away for the weekend.

    Good luck!!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Replies: 1
    Last Post: May 8th, 2009, 08:55 AM
  2. Java program for to implement supermarket menu
    By 5723 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 14th, 2009, 03:14 AM
  3. How to use for loop for movement of points in a picture display?
    By Dman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2009, 09:19 AM
  4. [SOLVED] Problem with Grading calculator in java program
    By Peetah05 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: March 28th, 2009, 04:25 PM