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

Thread: User inputs a number of characters as a string, program then lists all the words from array that can be made

  1. #1
    Junior Member
    Join Date
    Sep 2019
    Posts
    7
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default User inputs a number of characters as a string, program then lists all the words from array that can be made

    Hello, I tried to solve these problem by myself but I realised that need help. I don't get annything at the output. Will be grateful for any help!

    This task have a three condition:
    1. Each letter can be used only once.
    2.The letter can be used in any order.
    3.The text input by the user must not be one of the words generated.

    import java.util.Arrays;
    import java.util.Scanner;
     
    public class TYM6 {
    	 String[] words = {"and", "love", "vole", "levo", "the"};
            // TODO: Read characters from user.
     
            // TODO: Loop throgh the words array, and see which words can be made from the user's input.
     
            // TODO: Output the words that can be made, one per line.
            // For the word love program should output: vole, levo
     
     
    public void start1()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your characters: ");
        String chars = sc.nextLine();
        String word = Arrays.toString(words);
       // if(word1.length())
        try{
     
                boolean display=true;
                if(word.length()==chars.length()){  //checking if the length of user input word and words from array are equal
                    for (int c = 0; c<chars.length(); c++)  
                    {
                        if (word.indexOf(chars.charAt(c)) == -1) {   // Getting character from the list of characters that user typed at position c an search for that character in the //current word
                            display = false;
                            System.out.println("No match");
                        }
                    }
                    if (display) {
                        System.out.println(word);
                    }
                }
     
     
        }
        catch (Exception e) {
            System.out.println("Couldn't find the list of words!");
        }
     
    }
    }
     
    public class Main {
     
    	public static void main(String[] args) {
     
    		 TYM6 tym6 = new TYM6();
     
    	        tym6.start1();
     
     
    	}
     
    }
    Last edited by p.katanic; September 25th, 2019 at 02:55 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: User inputs a number of characters as a string, program then lists all the words from array that can be made

    I don't get annything at the output.
    What are you expecting to see in the output?

    What is your input to the program? What should the output be for that input?

    How are you trying to debug the program to see what the problem is? Try adding more print statements that display the values of variables as they are changed and used.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2019
    Posts
    7
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: User inputs a number of characters as a string, program then lists all the words from array that can be made

    For example, if user input word ovle, the program must show levo, love, vole,velo. I'm using Intellij IDEA but have trouble with debugging, getting notification Disconnected from the target VM, address: '127.0.0.1:63712', transport: 'socket'. Thanks for answer!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: User inputs a number of characters as a string, program then lists all the words from array that can be made

    Sorry, I do not know anything about Intellij IDEA or about using IP addresses to debug java code.
    I do all my debugging at the command prompt window looking at the output from print statements.

    The code need a main method to be able to execute it with the java command.

    The code also needs some comments describing what steps it is taking to solve the problem.
    For example what is the purpose of this statement:
    String word = Arrays.toString(words);
    Why is the array's contents placed in a String?

    This comment makes no sense:
    checking if the length of user input word and words from array are equal
    When would the length of a single word be the same as the length of a group of words?


    I think you need to stop coding for a while and spend some time working out the logic to solve the problem. Make a list of the steps the code needs to take. When the logic looks good, then move to writing the code.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2019
    Posts
    7
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: User inputs a number of characters as a string, program then lists all the words from array that can be made

    Thanks for answer. I have main method, just forget to copy it here, sorry.
    String word = Arrays.toString(words); // I'll do that in order to use method IndexOf later in the code, but maybe it's my mistake, I'm not sure.

    //checking if the length of user input word and words from array are equal - logic here is that if the word that user input is longer or shorter than the number of character from user input it's not a good word.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: User inputs a number of characters as a string, program then lists all the words from array that can be made

    Can you make a list of the steps the program needs to take to solve the problem?

    Note: When adding comments to the code, try to make it so that none of the text goes past column 80. Split the comments to new lines as needed. Long lines require scrolling left-right and makes the code harder to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2019
    Posts
    7
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: User inputs a number of characters as a string, program then lists all the words from array that can be made

    This is starting code from the examen.

    import java.util.Scanner;

    public class TestYourMettle6 {

    String[] words = {"and", "love", "vole", "levo", "the"};

    public void start() {
    Scanner sc = new Scanner(System.in); // Do not remove for automated testing to work
    // TODO: Read characters from user.

    // TODO: Loop throgh the words array, and see which words can be made from the user's input.

    // TODO: Output the words that can be made, one per line.
    }

    public static void main(String[] args) {
    TestYourMettle6 tym6 = new TestYourMettle6();
    tym6.start();
    }
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: User inputs a number of characters as a string, program then lists all the words from array that can be made

    This is starting code
    Hold off writing any more code until you have a design or a list of the steps for the program to solve the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2019
    Posts
    7
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: User inputs a number of characters as a string, program then lists all the words from array that can be made

    Any suggestion what steps to make? Should I use StringBuilder?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: User inputs a number of characters as a string, program then lists all the words from array that can be made

    Should I use StringBuilder?
    Don't think about coding yet. Work on the logic details.

    An example of the steps to take when tying shoe laces:
    after foot is in shoe
    pull up tongue of shoe
    pick up left lace in left hand
    pick up right lace in right hand
    cross lace from one hand to the other and underneath the other lace
    form a loop with the laces in both hands
    etc

    Work on a list of steps at the detail level to solve the problem.
    How would you do it with paper and pencil?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Sep 2019
    Posts
    7
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: User inputs a number of characters as a string, program then lists all the words from array that can be made

    Hello, I solved my problem. Issue was a logical one. I have declared an array of words. The user then inputs some characters, and the program is supposed to tell the user which words can be made with the characters input. However, I never actually looping through the 'words' array - instead I was compared the characters the user has input, with the characters the user has input ( chars and word are the same thing).
    public class TYM6 {
        String[] words = {"and", "love", "love", "love", "vole", "levo", "the"};
        public void start() {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter your characters: ");
            String chars = sc.nextLine();
     
            // Loop through the words array
            for (String word : words) {
                boolean display = true;
                if (word.length() == chars.length()) {
                    // Loop through characters from user input, make sure the current
                    // word has all characters from user input
                    for (int c = 0; c < chars.length(); c++) {
                        if (word.indexOf(chars.charAt(c)) == -1) {
                            // One of the characters was not found, so do not display
                            display = false;
                        }
                    }
                    if (display) {
                        // All characters found, so display
                        System.out.println(word);
                    }
                }
            }
        }
     
        public static void main(String[] args) {
            TYM6 tym6 = new TYM6();
            tym6.start();
        }
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: User inputs a number of characters as a string, program then lists all the words from array that can be made

    Glad you got it to work.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Delimiter function and number of words in string
    By Java girl in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 22nd, 2014, 05:08 PM
  2. How to replace characters in Array with user input Characters
    By gdoggson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 4th, 2013, 05:53 AM
  3. Splitting a String by number of characters
    By torrie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 24th, 2013, 10:58 PM
  4. User inputs string to get calculated output
    By freky8ss in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 2nd, 2013, 10:08 AM
  5. [SOLVED] implementing a dictionary of words using an array of linked lists
    By McTown in forum Java Theory & Questions
    Replies: 3
    Last Post: April 27th, 2011, 02:19 PM

Tags for this Thread