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

Thread: Amending whole words to a/an char[][]

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Amending whole words to a/an char[][]

    So my goal is to build a word search here. The road block I have run into is how to save words, input from user, into an array. Then once I figure that out I need to try and arrange them in such a way so that they will fit into my array without screwing with one another. It does not have to be working exceedingly well I mean really it can be assumed that the users will not try to screw the program up. Ie one or two words and a fairly large table will be all the input. Here is the program as of now:

    import java.util.Random;
    import java.util.Scanner;
    public class BuildWS
    {
        int r;
        int c;
        char[][] array;
        String query = " ";
     
        public BuildWS()
        {  
        }
     
        public void build()
        {
            collectInfo();
            print();
        }
     
        public void collectInfo()
        {
            System.out.println("How many rows?");
            Scanner in = new Scanner(System.in);
            r = in.nextInt();
            System.out.println("How many columns?");
            Scanner in1 = new Scanner(System.in);
            c = in.nextInt();
            array = new char[r][c];
     
            Random ran = new Random();
            for (int i = 0; i < r; i++)
            {
                for (int j = 0; j < c; j++)
                {
                    array[i][j] = (char)(ran.nextInt(26) + 'a');
                }
            }
     
            Scanner in2 = new Scanner(System.in);
            while (!query.equals("end"))
            {
     
                System.out.println("Add a word to your search. Type end to quit");
                query = in2.nextLine();
                if (!query.equals("end"))
                {
                    paste(query);
                }
     
            }
        }
     
        public void print()
        {
     
            for (int i = 0; i < array.length; i++)
            {
                for (int j = 0; j < array[i].length; j++)
                {
                    System.out.print(array[i][j] + " ");
                }
                System.out.println();
            }
     
        }
     
        public void paste(String inString)
        {
            System.out.println(inString);// Here is where I want to operate on the words. How do I save inString into the array as a string?
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,485
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: Amending whole words to a/an char[][]

    how to save words, input from user, into an array
    String has a method for that. Please review the String API.
    I need to try and arrange them in such a way so that they will fit into my array without screwing with one another
    Not sure what that means. I think you have a picture of your program's function in your head, but you haven't given us the whole picture. It would be helpful (to you and us) if you'd write down what the program does from start to finish and share that with us. Then ask specific questions about those program functions that you're not sure how to code. Making an attempt at the code before asking the question is always encouraged, because usually the question goes away, becomes clearer, or changes completely once the problem is better understood.

Similar Threads

  1. Replies: 2
    Last Post: February 10th, 2014, 05:24 AM
  2. Creating a number char by char
    By Herah in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 23rd, 2013, 03:59 AM
  3. Replies: 12
    Last Post: March 17th, 2013, 01:38 AM
  4. Get int value from char, char pulled from String
    By Andrew Red in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2013, 09:04 AM
  5. How do I compare between the array of char and array of words !!?
    By bady2050 in forum Collections and Generics
    Replies: 1
    Last Post: May 5th, 2012, 05:36 PM