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: 2D Array with Strings

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas.
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 2D Array with Strings

    The lab I got from my teacher is supposed to be a 2D array. We're supposed to be using it to where it reads the strings from the file, and then it prints them out in an hour glass form like this:

    HELLO
    E L
    L
    E L
    HELLO

    This is my code so far, I just don't know how in the heck to get it to read string the strings from a file, then convert it to a char matrix, or any of that. If anyone could please explain and help me with my code, I'd highly appreciate it.

    import java.io.*;
    import java.util.Scanner;
     
    public class Pattern
    {
    	public static void main(String args[]) throws FileNotFoundException
    	{
     
    	System.out.println("Enter the path of the file:");
    	Scanner kb = new Scanner(System.in);
     
    	String path = kb.next();
     
        Scanner file = new Scanner(new File(path));
     
        String s = file.next();
     
    	int start = 0;
    	int q = 1;
     
     
     
    	for(int r = 1; r <= s.length()-2; r++){
    		for(int c = 0; c < s.length()-start; c++){
    			if(c < start)
    				System.out.print(" ");
    			else
    				System.out.print(((c - start) % 2 == 0) ? " " : s.charAt(c));
    }
            System.out.println();
            start += q;
            if(start==s.length()/2){
        	    start -= q;
        	    start--;
        	    q *= -1;
            }
    	}
     
    	System.out.println(s);
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,140
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: 2D Array with Strings

    how in the heck to get it to read string the strings from a file,
    Your code uses next() to read one word from the file. Are all the words on the same line or is each word on a separate line? If on separate lines you should use the nextLine() method to read them.
    convert it to a char matrix
    Can you explain what you mean by a char matrix? Given the String: "HELLO" what would a char matrix look like for that word?

    Look at the pattern that you printed out that the program is supposed to generate. Replace the letters in the word with numbers so you can easily see what is printed on each line. The double Ls might confuse you. Using numbers should make it very clear what goes on each line.

    What happens when you execute your code?

Similar Threads

  1. Comparing a character to an array of strings
    By Hashmeer169 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 4th, 2011, 06:55 PM
  2. How do I combine two strings in an array?
    By SkyAphid in forum Collections and Generics
    Replies: 3
    Last Post: September 15th, 2011, 06:20 PM
  3. Adding strings to an array dynamically, how can i do it?
    By emkoirl in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2011, 02:07 PM
  4. filling an array with strings
    By exose in forum Collections and Generics
    Replies: 3
    Last Post: February 18th, 2011, 08:44 AM
  5. creating an array of strings?
    By Jason in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 19th, 2010, 08:28 AM