12 Replies - 1055 Views - Last Post: 13 May 2012 - 11:21 AM Rate Topic: -----

#1 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

2d arrays

Posted 13 May 2012 - 02:52 AM

Hello

Im stuck on how to do a 2d array in a while loop, i found a few examples online on 2d arrays on for loops but
nothing on the while loop.

I did try change the for code to while but in this instance i can seem to get it right!

Can anyone give me an example of how this is done?

Thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: 2d arrays

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: 2d arrays

Posted 13 May 2012 - 02:56 AM

What are you trying to do with the 2D array in the while loop? Is the use of a while loop mandated? If all you're doing is iterating the array, you know the number of rows and columns in the array, so a for loop would typically be more appropriate. I can give a better answer when I understand what you're trying to do.
Was This Post Helpful? 0
  • +
  • -

#3 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Re: 2d arrays

Posted 13 May 2012 - 03:19 AM

Display Unicode characters between 65 and 124, starting with A.
I need to start by declaring a char and setting it equal to A.
The output should be in 12 rows and 5 columns.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: 2d arrays

Posted 13 May 2012 - 03:27 AM

We've already danced this dance. You have the unicode characters in an array, and now you just want to print them in 5 columns. Your last post said you were going to show your attempt at doing that.

You currently have a while loop that prints:

A
B
C
D
E
etc.

Post your code as it currently exists. Include your attempt at printing in 5 columns, but if you haven't made an attempt, that's okay. We'll work through it.
Was This Post Helpful? 1
  • +
  • -

#5 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Re: 2d arrays

Posted 13 May 2012 - 03:46 AM

heres my program at the moment:
public class WhileDemo
{
    public static void main(String[] args)
    {

	int a = 1;
	int sum = 0;
	while(a <= 100)
	{

		sum = sum + a;
		++a;

	} 

	System.out.println("sum = " + sum);
    {
	int i = 1; 
        int j = 1;
	while(i<=20)
	{
	
	j = i * i;
 
        System.out.println(j + " ");
	
	++i;
}

        char c = 65;
        while(c <= 144)
        {
	
	System.out.print(c);

	++c;
    		}
    System.out.println();
	
	}
    }
}


At the moment it prints this way: ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~?????????????????
just like that
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: 2d arrays

Posted 13 May 2012 - 03:59 AM

You've combined the squares and characters into a single loop. That's okay, but it would be helpful to know where you're going with both of those.

While you're thinking about that, to break your line of characters into columns, you just have to print a linefeed every 5 characters, then do some formatting to make it look prettier. One way to do that would be to add either a counter or a check on c to see if it has incremented the desired number of times.

c starts at 65, so you want to print c = 65, 66, 67, 68, and 69, then add a return or linefeed. You could do something like:

// add a linefeed
if ( c % 5 == 0 )
{
   System.out.println();
}

after c has been incremented in the character while loop.

This post has been edited by GregBrannon: 13 May 2012 - 04:02 AM

Was This Post Helpful? 1
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: 2d arrays

Posted 13 May 2012 - 04:14 AM

You're getting some unprintable unicode characters at the end. I don't remember your original ending point, but the unprintable characters weren't there before. Your updated character while loop could look something like:
char c = 65;

while(c <= 126)
{
    System.out.print( c++ + "  " );

    if ( c % 5 == 0 )
    {
        System.out.println();
    }
}

Was This Post Helpful? 1
  • +
  • -

#8 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Re: 2d arrays

Posted 13 May 2012 - 04:18 AM

Thanks.
I was unaware i could use the if satement in a loop.
Your code works great in doind what im trying do.
The ?????????? at the end of the Unicode, is that because it does not have a return?
Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: 2d arrays

Posted 13 May 2012 - 04:26 AM

Quote

The ?????????? at the end of the Unicode, is that because it does not have a return?

Those are caused by the unprintable unicode characters I mentioned in my last post. If you're actually seeing question marks, that's your system telling you that it doesn't know what to print for those characters. I see something different on my system. I suggest you decrease c to the last printable character, the '~' at c = 126.
Was This Post Helpful? 0
  • +
  • -

#10 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Re: 2d arrays

Posted 13 May 2012 - 04:34 AM

I just noticed the problem

The code was
char c = 65;
                   while(c <= 144)


Which was wrong because i was trying to display the characters from 65 to 124 not 65 to 144 so code should have been:
char c = 65;
            while(c <= 124);


All prints like it should now! :)

Thanks a lot!!
Was This Post Helpful? 0
  • +
  • -

#11 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Re: 2d arrays

Posted 13 May 2012 - 04:50 AM

Here is my completed program:

public class WhileDemo
{
    public static void main(String[] args)
    {

	int a = 1;
	int sum = 0;
	while(a <= 100)
	{

		sum = sum + a;
		++a;

	} 

	System.out.println("sum = " + sum);
        System.out.println();// added this to make out print look neater
    {
	int i = 1; 
        int j = 1;
	while(i<=20)
	{
	
	j = i * i;
 
        System.out.println(j + " ");
        System.out.println();// added this for same reason as above
	
	++i;
}

        char c = 65;
        while(c <= 124)
        {
	
	System.out.print(c + " ");
	
		++c;
	   
		if ( c % 5 == 0 )
		{
	System.out.println();
	}
    }
}
    }
}

Was This Post Helpful? 0
  • +
  • -

#12 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: 2d arrays

Posted 13 May 2012 - 05:38 AM

You should also get in the habit of properly indenting your code, eliminating extra lines, and including comments that improve code readability and understanding. It's okay to have additional lines to separate code blocks or to improve readability, but opinions will vary on what's more readable or understandable. Here's a suggestion to improve the readability of your code (to me):
public class WhileDemo
{
    public static void main(String[] args)
    {
        int a = 1;
        int sum = 0;
        while(a <= 100)
        {
            sum += a;  // note shorthand += operator
            ++a;       // this line could be eliminated by
                       // incrementing a in the previous\
                       // line, e.g.: sum += a++;
        } 

        System.out.println("sum = " + sum);
        System.out.println();// added this to make out print look neater

        int i = 1; 
        int j = 1;
        while(i<=20)
        {
            j = i * i;

            System.out.println(j + " ");
            System.out.println();// added this for same reason as above (same reason as above? - be specific)

            ++i;   // this line could be eliminated by
                   // incrementing i in the previous line
                   // j = i * i++;

                   // you could shorten the whole loop to:
                   // while( i <= 20 )
                   // {
                   //    System.out.println( ( i * i++ ) + "\n");
                   // }
        }

        char c = 65;
        while(c <= 124)
        {
            System.out.print(c + " ");

            ++c;      // this line could be eliminated by
                      // incrementing c in the previous
                      // line: System.out.print(c++ + " ");

            if ( c % 5 == 0 )
            {
                System.out.println();
            }
        }
    }
}

The code above with the changes I suggested:
public class WhileDemo
{
    public static void main(String[] args)
    {
        int a = 1;
        int sum = 0;
        while( a <= 100 )
        {
            sum += a++;
        } 

        System.out.println("Sum of first 100 integers = " + sum + "\n" );

        System.out.println( "Squares of the first 20 integers:" );
        int i = 1; 
        int j = 1;
        while( i <= 20 )
        {
            System.out.println( ( i * i++ ) + "\n");
        }

        System.out.println( "Unicode characters 65 through 124:" );
        char c = 65;
        while(c <= 124)
        {
            System.out.print( c++ + " " );

            if ( c % 5 == 0 )
            {
                System.out.println();
            }
        }
    }
}

Was This Post Helpful? 1
  • +
  • -

#13 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Re: 2d arrays

Posted 13 May 2012 - 11:21 AM

Thanks for all the tips. Im only 6 weeks into studying Java so anything to help me improove my programming is great!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1