Results 1 to 5 of 5
  1. #1
    Aenohe is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default How to add item on list

    Hi again

    I managed to fix previous problems i had with scanner, which is now working as intended.

    Last problem i have is with method called wordMenu() which is inside method called show().
    Problem is that last line of method wordMenu() should add newly created words to the mDatabase.

    If i use method listWords() (SWITCH 3) i see newly added words on the bottom of the list which is intended.
    But they never appear if i run the game test again (SWITCH 1). Those newly created words by user should
    be added to the mDatabase and program should be able to use them to create new random word list for test.

    Can some1 or you Junky help me understand why and how to fix it - i have no complier or any other errors.



    Java Code:
        /**
         * Class which interacts with user using scanner.
         * And runs the main method of the program.
         *
         */
        public class Menu
        {
            public Scanner mScanner;
            private static  TestGenerator mGenerator = new TestGenerator();
            private WordDatabase mDatabase;
             
           /**
            * Constructor for objects of class Menu
            *
           */
           public Menu()
           {
                mScanner = new Scanner(System.in);
                mDatabase = new WordDatabase();
           }
             
           /**
             * Main method that runs our program.
             * From welcome menu trough test or adding new words.
             * Up to the goodbye screen.
             *
             */
           public void show()
           {       
                login();
                 
                while(true)
                {
                    welcomeMenu();
                     
                    if(!mScanner.hasNextInt())
                    {
                        System.out.println(mScanner.nextLine() + " är inte giltigt input");
                        continue;
                    }
                    int option = mScanner.nextInt();
                     
                     switch(option)
                    {
                        case 1:
                            Test test = mGenerator.generateNewTest();
                            test.start();    
                        break;
                         
                        case 2:
                            wordMenu();
                        break;
                         
                        case 3:
                            listWords();
                        break;
                         
                        case 4:
                             if(goodbyeMenu()){
                                  return;
                             }
                             else{
                             }
                        break;
                                                         
                        default:
                            System.out.println("--> Förstår inte <--");
                        break;
                    }
                }
           }
            
            /**
            * Method that list all words currently in database.
            *
           */
           private void listWords()
           {
                for(int i = 0; i < mDatabase.size(); i++)
                {
                    System.out.println( mDatabase.get(i) );
                }
                System.out.println();
           }
         
           /**
             * Method that prints out welcome text.
             * Helps with keeping main method easier to understand.
             *
             */
           private void welcomeMenu()
           {
                System.out.println("Hej och välkommen till Igor's glostränare!");
                System.out.println();
                System.out.println("1.Kör glostest");
                System.out.println("2.Registera ord");
                System.out.println("3.Skriv ut alla glösor.");
                System.out.println("4.Avsluta testet.");
                System.out.println();
                System.out.print("Vad vill du göra ? --> ");
           }
             
           /**
            * Method that takes users name.
            * Helps with keeping main method easier to understand.
            *
           */
           private void login()
           {
               System.out.print("Ditt namn: ");
               mScanner.nextLine();  
               System.out.print("\f");
           }
             
           /**
             * Method that allows user to add new words into database.
             *
             */
           private void wordMenu()
           {
                   System.out.println();
                   System.out.println("Lägga till ord menyn");
                   System.out.println();
                   System.out.print("Svenska:-->");
                   String Svenska = mScanner.next(); 
                   System.out.println();
                   System.out.print("Engelska:-->");
                   String Engelska = mScanner.next();
                   System.out.println();
                   System.out.print("-->");
                             
                   System.out.println(Svenska + ":" + Engelska);
                             
                   Word word = new Word(Svenska, Engelska);
                   mDatabase.add(word);    
           }
         
            /**
             * Method that makes creates goodbye screen.
             *
             */
            private boolean goodbyeMenu()
            {  
                System.out.println("Är du säker att du ska avsluta program");
                System.out.println();
                System.out.println(" 1 - avsluta");
                System.out.println(" 2 - gå tillbaka");
                System.out.print("-->");
                 
                int input = mScanner.nextInt();
                if(input == 1){
                    return true;
                }       
                else{
                    return false;
                }
           }
        }

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: How to add item on list

    What does that Test class do?

  3. #3
    Aenohe is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: How to add item on list

    Here is the code for both Test and TestGenerator class.

    TestGenerator class generates simple test using random class to pick 10 words(swedish,english)
    and make simple test for user.

    Java Code:
    import java.util.ArrayList;
    import java.util.Iterator;
    
    /**
     * Class that runs test and counting results.
     * 
     * @author (Igor Mijatovic) 
     * @version 1.0(2011.11.06)
     */
    public class Test
    {
        private ArrayList<Word> mTestWords;
        private int mScore;
    
        /**
         * Constructor for objects of class Test.
         * 
         * @param is ArrayList wordlist from class Word.
         */
        public Test(ArrayList<Word> wordList)
        {
           mTestWords = wordList;
           mScore=0; 
        }
    
        /**
         * Method which count and print out test results.
         * 
         */
    
        public void start() 
        {
            System.out.println("Nu startar testet");
            Iterator<Word> it = mTestWords.iterator();
            while(it.hasNext())
            {
                if(it.next().answer()) 
                {
                    mScore++;
                }
            }
            System.out.println("Testet avlklarat; " + mScore + "/" + mTestWords.size());
        }
    }
    Java Code:
    import java.util.ArrayList;
    
    /**
     * This class generates tests using class RandomN and WordDatabase.
     * 
     * @author (Igor Mijatovic) 
     * @version 1.0(2011.11.06)
     */
    public class TestGenerator
    {
        private RandomN mRandom;    
        private WordDatabase mDatabase;
        
        /**
         * Constructor for objects of class TestGenerator.
         * 
         */
        public TestGenerator()
        {
            mDatabase = new WordDatabase();
            mRandom = new RandomN(mDatabase.size());
        }
        
        /**
         * Method that add new words to our wordlist.
         * 
         * @param new (word).
         */
        public void addWord(Word word)
        {
            mDatabase.add(word);
            mRandom = new RandomN(mDatabase.size()); 
        }
        
        /**
         * Method which randomly pick values from our database.
         * 
         * @return  Test randomly picked values from database list.
         */
        public Test generateNewTest()
        {
            ArrayList<Word> wordlist = new ArrayList<Word>();
            
            for(int i = 0; i < 10; i++)     
            {   
                int max = mDatabase.size();
                Word word = mDatabase.get(mRandom.getRandomValue(max));
                while(wordlist.contains(word)) 
                {
                    word = mDatabase.get(mRandom.getRandomValue(max)); 
                }
                wordlist.add(word);     
            }
            return new Test(wordlist);
            
        }
    }

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: How to add item on list

    Looks like you should be passing mDatabase to that call to generateNewTest

  5. #5
    Aenohe is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: How to add item on list

    Quote Originally Posted by al_Marshy_1981 View Post
    Looks like you should be passing mDatabase to that call to generateNewTest
    If you think on

    Java Code:
        public Test generateNewTest()
        {
            ArrayList<Word> wordlist = new ArrayList<Word>();
             
            for(int i = 0; i < 10; i++)    
            {  
                int max = mDatabase.size();
                Word word = mDatabase.get(mRandom.getRandomValue(max));
                while(wordlist.contains(word))
                {
                    word = mDatabase.get(mRandom.getRandomValue(max));
                }
                wordlist.add(word);    //mDatabase.add(word) It does compile but never fills the list i get null as value.
            }
            return new Test(wordlist);
             
        }
    I just get message from start() method last line - i guess wordlist never get values.

Similar Threads

  1. Replies: 1
    Last Post: 06-29-2011, 02:32 PM
  2. access array list item from draw
    By trishtren in forum New To Java
    Replies: 8
    Last Post: 04-19-2011, 12:34 PM
  3. Appending and item to a Select List
    By Samurai Coder in forum New To Java
    Replies: 1
    Last Post: 12-04-2009, 10:56 PM
  4. Button to remove item from list
    By dacool25 in forum Java Applets
    Replies: 6
    Last Post: 10-14-2009, 04:30 AM
  5. linked list nodes all refernce same item.
    By yllawwally in forum New To Java
    Replies: 0
    Last Post: 12-18-2007, 08:45 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
  •