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

    Default Scanner and Switch problem

    Cant fix two things here, first is to have 2 scanners one for integers and one for strings.
    And i cant get method called "show" to work properly.

    I supply and Word class code because problem is maybe there.

    Java Code:
    public class Menu
    {
        public Scanner mScanner = new Scanner(System.in);
        private static TestGenerator mGenerator = new TestGenerator();
        
        /**
         * Constructor for objects of class Menu
         */
        public Menu()
        {
          
        }
    
        /**
         * Method to create welcome menu for user.
         * 
         */
        public void show() 
        {
            startWelcome();
            
            while(true) 
            {
                int number = mScanner.nextInt();
                switch(number)
                {
                    case 1: 
                        Test test = mGenerator.generateNewTest();
                        test.start(); 
                        
                     break;
                     case 2:
                        Word word = new Word(mScanner.nextLine());  // This line gives me headache !
                       
                        
                    break;
                    case 3:
                
                        return; 
                default:
                    System.out.println("");
                break;
                }
            }
        }
        
    
        /**
         * Privat method for welcome screen.
         * To make show method easier to read.
         */
        private void startWelcome()
        {
            System.out.println("Welcome");
            System.out.println();
            System.out.println("1.Start test");
            System.out.println("2.Register word");
            System.out.println("3.Fisnish test.");
            System.out.println();
            System.out.println("What you wanna do?........");
        }
    
    }
    I also have to have menu under menu for example "what is your name" before menu test start but i am not sure how to do that.


    Word class which might be problem for my show-method in previous code.
    Java Code:
    public class Word
    {
        private String mWord;
        private String mAnswer;
        private int mCounter;
        
        public static Scanner mScanner = new Scanner(System.in);
        /**
         * Constructor for elements of class Word.
         * Creating copy of the word.
         * 
         * @para - class Word with name word.
         */
        public Word(Word word)
        {
            mWord = word.mWord;
            mAnswer = word.mAnswer;
            mCounter = word.mCounter;
        }
        
        /**
         * Constructor for objects of class Word.
         * 
         * @para - String word.
         * @para - String answer.
         */
        public Word(String word,String answer)
        {
           mWord = word.toLowerCase().trim(); 
           mAnswer = answer.toLowerCase().trim();
           mCounter = 3; 
        }
    
        /**
         * Method to check is answer correct or not.
         * 
         * @return - boolean false or true.
         */
        public boolean answer()
        {
            
            while(mCounter > 0) 
            {
                System.out.println("What is " + mWord + "?");
                String answer = mScanner.nextLine();
                mCounter--;
                
                if(mAnswer.equals(answer.toLowerCase().trim()))
                {
                    System.out.println("You guessed");
                    return true;
                }
                else
                {
                    System.out.println("You have missed");
                }
            }
            System.out.println("No more tryes");
            return false;

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Scanner and Switch problem

    Quote Originally Posted by Aenohe View Post
    Cant fix two things here, first is to have 2 scanners one for integers and one for strings.
    Can't fix what? Do you get errors? Then post the full messages. Do you get incorrect behaviour? Then describe what is happening and what you want to happen instead.
    And i cant get method called "show" to work properly.
    How are we supposed to know what "properly" is? Provide better details, ask a specific question and get a specific answer.

    By the way, why do you want to have 2 Scanner objects? Using one should be sufficient unless input is coming from different sources.
    Last edited by Junky; 11-28-2011 at 11:25 PM.

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

    Default Re: Scanner and Switch problem

    Line 33 of the Menu class is problem i cant compile it ("cannot find symbol - constructor Word(java.lang.String") it should allow me to add new words to the WordDatabase class which and increase initial size of the WordDatabase class.

    I add this class to help you get the picture what my problem is.

    Java Code:
    public class WordDatabase
    {
        private ArrayList<Word> mDatabase;
    
        /**
         * Constructor for objects of class WordDatabase.
         * 
         * 
         */
        public WordDatabase()
        {
            mDatabase = new ArrayList<Word>();
            fillDatabase();
         }
    
        /**
         * Method which only stores data translated swedish words.
         * 
         */
        private void fillDatabase()
        {
            mDatabase.add(new Word("bil","car"));
            mDatabase.add(new Word("båt","boat"));
            mDatabase.add(new Word("hus","house"));
            mDatabase.add(new Word("katt","cat"));
            mDatabase.add(new Word("hund","dog"));
            mDatabase.add(new Word("bord","table"));
            mDatabase.add(new Word("stol","chair"));
            mDatabase.add(new Word("cykel","bicycle"));
            mDatabase.add(new Word("säng","bed"));
            mDatabase.add(new Word("fru","wife"));
            mDatabase.add(new Word("son","son"));
            mDatabase.add(new Word("spindel","spider"));
            mDatabase.add(new Word("skola","school"));
            mDatabase.add(new Word("far","father"));
            mDatabase.add(new Word("tangentbord","keyboard"));
        }
        
        /**
         * Method to rerurn size of word database.
         * 
         * return - int number of total words in database.
         */
        public int size() 
        {
            return mDatabase.size();
        }
        
        /**
         * Method to add word in current database.
         * 
         * para - class Word with name word.
         */
        public void add(Word word) 
        {
            mDatabase.add(word);
        }
       
        /**
         * Method to get word under specific number.
         * 
         * para - int number.
         * return - String word under chosen number.
         */
        public Word get(int i)
        {
           return new Word(mDatabase.get(i));
        }
    }

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Scanner and Switch problem

    Quote Originally Posted by Aenohe View Post
    Line 33 of the Menu class is problem i cant compile it ("cannot find symbol - constructor Word(java.lang.String")
    The Word class has two constructors, neither of them take a single String parameter.

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

    Default Re: Scanner and Switch problem

    I am aware of that and i need to use second Constructor if i am right.
    But i just cant write code properly tried everything i knew still cant get it to work.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Scanner and Switch problem

    If you are going to use the String String constructor then you will need to have a second String parameter. What should that String be? I have no idea. That is your problem. You are designing the program not me.

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

    Default Re: Scanner and Switch problem

    Quote Originally Posted by Junky View Post
    If you are going to use the String String constructor then you will need to have a second String parameter. What should that String be? I have no idea. That is your problem. You are designing the program not me.
    That second String should be String answer. And I am still not able to make it work.

    Can you or someone else write proper code for line 33(class Menu) using second constructor(String word, String answer) from class Word.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Scanner and Switch problem

    Quote Originally Posted by Aenohe View Post
    Can you or someone else write proper code for line 33
    That is not how it works. It is your program and your responsibility to write the code. If the second parameter to the constructor will be the "answer" then where does that answer come from? This is a design issue that you have to make. Perhaps you need to step away from the computer and sketch things out on paper until you have a clear idea of what the program should be doing. Then you should be able to convert that to code.
    Last edited by Junky; 11-29-2011 at 11:26 PM.

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

    Default Re: Scanner and Switch problem

    @Junky

    I managed to fix that question i was asking you before, but now i have new problem. Can you please tell me why last line in method wordMenu().
    Doesn't actually add word to mDatabase. I cant understand it specially because if you just want to print out list SWITCH 3 you can see new word added.
    But when you run SWITCH 1 again after adding new words they never appear in test (i tried zillion times).

    If you need some other class please say so so i can post. Cant find solution for this few days already.



    Java Code:
    import java.util.Scanner;
    
    /**
     * 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;
            }
       }
    }

Similar Threads

  1. Problem with the Scanner
    By Maretaga in forum New To Java
    Replies: 6
    Last Post: 07-14-2011, 09:14 AM
  2. Scanner problem
    By mkay in forum New To Java
    Replies: 2
    Last Post: 10-28-2010, 12:09 AM
  3. Replies: 9
    Last Post: 09-21-2010, 04:15 PM
  4. Problem with scanner
    By Kangaroo128 in forum New To Java
    Replies: 11
    Last Post: 09-01-2009, 08:07 AM
  5. Problem with a switch statement in Java
    By baltimore in forum New To Java
    Replies: 2
    Last Post: 08-02-2007, 04:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •