Page 1 of 2 12 LastLast
Results 1 to 20 of 33
  1. #1
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default ugh need some serious methods help

    need to write a method for my main that takes the number of lines, number of characters per line and character you want to use and prints it out.


    What I have so far.

    Java Code:
    import java.util.Scanner;
    public class Lab9
    {
    public static void main(String[] args)
      	 {
       		Scanner keyboard= new Scanner(System.in);
    String ans;
       	do
          		{
          		System.out.print("How many lines you want?  ");
         		 int noLines=keyboard.nextInt();
    
        		  System.out.print("How many characters per line?  ");
          		int charPerLine=keyboard.nextInt();
    
        		  System.out.print("What character?    ");
    
         		 char theChar=keyboard.next().charAt(0);
         		 int countx=printLines(noLines,charPerLine,theChar);
          		System.out.println("main reports "+countx+"  characters printed");
          		System.out.println("Do another?(y/n)  ");
          		ans=keyboard.next();
      		    }while(ans.equals("y"));
    
    System.out.println("GoodBye!!  ");
    }
    		
    		//your printLines method should be coded here
    
    
    
    }
    Last edited by jjth39347; 03-30-2011 at 07:49 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What have you tried for the method call?

    Ill give you some good starting hints.

    Method declaration should be
    Java Code:
    public static int printLines(int numberOfLines, int charsPerLine, char symbol)
    It should have at least one loop, which runs X x Y times, where x is number of lines, and y is chars per line.

    Try thinking of these tips and see what you come out with.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by Wesley wora View Post
    Read the letters and numbers in image file ....

    Thx in advance
    I don't know if this is supposed to be a question or what. If it is meant to be a question, we are here to help you figure it out, not simply answer your vague questions. Do not hijack someone elses thread. Make your own. When/if you do, please answer a smart question.

  4. #4
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    alright I gave it a try and this is what I've come up with, I would love to test it but I keep getting an error,
    The error is
    Java Code:
    C:\Users\Jason\Desktop\cpt-236\Lab9.java:41: illegal character: \29
    			}
    			^
    1 error
    
    Tool completed with exit code 1
    the code is:
    Java Code:
    
    
    import java.util.Scanner;
    public class Lab9
    {
    	public static void main(String[] args)
      	 {
       		Scanner keyboard= new Scanner(System.in);
    		String ans;
       		do
          		{
          			System.out.print("How many lines you want?  ");
         		 	int noLines=keyboard.nextInt();
    
        		 	 System.out.print("How many characters per line?  ");
          			int charPerLine=keyboard.nextInt();
    
        		 	 System.out.print("What character?    ");
    
         		 	char theChar=keyboard.next().charAt(0);
         		 	int countx=printLines(noLines,charPerLine,theChar);
          			System.out.println("main reports "+countx+"  characters printed");
          			System.out.println("Do another?(y/n)  ");
          			ans=keyboard.next();
      		   	}
      			while(ans.equals("y"));
    
    			System.out.println("GoodBye!!  ");
    		}
    
    		public static int printLines(int noLines, int charsPerLine, char theChar)
    		{
    			for(noLines=1;noLines<=5;noLines++)
    			{
    				for(charsPerLine=1;charsPerLine<=noLines;charsPerLine++)
    				{
    					System.out.print(""+theChar);
    				}
    
    				System.out.println();
    			}
    		}
    }

  5. #5
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    bump still need some help :/

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your loops in the static method are bad, the idea of a loop is to do something x amount of times where x is unknown until runtime(this isn't always true, but most of the time it's safe to say)

    You should not be changing the inputs, they should stay the same at all times. Try thinking about this and give the method another attempt.

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

    Default

    You really do not understand what "illegal character" means? It points directly at the offending character.

  8. #8
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    I've gotten it to print the right number of lines and number of chars per line and the right character but its not giving me the right count for each char such as,

    Java Code:
    $$$$
    $$$$
    $$$$
    main reports 12 characters printed
    instead I get some random number.
    Here is my method
    Java Code:
    	public static int printLines(int noLines, int charPerLine, char theChar)
    	{
    	    int i;                        // index variable in "for" loop
    		int j;
    	    for (i = 0; i < noLines; i ++)
    	    {
    			for (j = 0; j < charPerLine; j++)
    			System.out.print(""+theChar);
    			System.out.println("");      // end output line
    
    		}
    	    return theChar;
    	}
    
    }
    Last edited by jjth39347; 03-30-2011 at 07:36 AM.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Good you did a smart thing by breaking into small loops. If one loop creates x items how many items will 2 loops create?

    As for the return of this method, you want to return the total number of symbols, how would you do this? How do you find the area of a rectangle?

  10. #10
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    You really do not understand what "illegal character" means? It points directly at the offending character.
    and it was giving me the error because I forgot to return the method lol

  11. #11
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Good you did a smart thing by breaking into small loops. If one loop creates x items how many items will 2 loops create?

    As for the return of this method, you want to return the total number of symbols, how would you do this? How do you find the area of a rectangle?
    I love you, you don't just give me the answer when most people would have been bored by now.

    And I don't know why I didn't think of trying to multiplying the two ints in the return. How about you take my teachers job :D

    Method is now done.

    Java Code:
    public static int printLines(int noLines, int charPerLine, char theChar)
    	{
    	    int i;                        // index variable in "for" loop
    		int j;
    	    for (i = 0; i < noLines; i ++)
    	    {
    			for (j = 0; j < charPerLine; j++)
    			System.out.print(""+theChar);
    			System.out.println("");      // end output line
    
    		}
    	    return noLines * charPerLine;
    	}

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad to have helped. Two tips I have for you are the following:
    1) you can declare the ints i and j in the loop if they aren't going to be used again like this
    Java Code:
    for(int i = 0; condition; i++)
    2) be careful about not wrapping loop statements in a code block or you may run into errors later. It may be a good habit to always wrap the loop statements in a code block so you don't forget(this is what I do)
    Java Code:
    for(int j = 0; j < 10; j++){
      //all statements in block of this loop
    }
    A common error I see is somethig like this

    Java Code:
    int x = 2;
    for(int j = 0; j < 10; j++)
      x += j;
      x -= (j - 1);
    In this code you will get an error because j only exists for the first statement after the loop. If you define j before the loop the second statement only happens once, after the loop, with the final value of j.

  13. #13
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    I was messing around with my code for some practice but I can't seem to get it to work with a while loop instead of a for, any ideas?

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Declare the variables outside of both loops increment the variable in re correct loop and the while loops should look the same as the for loops condition.

  15. #15
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    Here is the method but it just prints one vertical line but not the vertical ones

    Java Code:
    	public static int printLines(int noLines, int charPerLine, char theChar)  // my method.
    	{
    	    int i=0;
    	    int j=0;// variables for loops
    
    	    while (i < noLines) // loop for number of lines
    	    {
    
    			while (j < charPerLine) // loop for characters per line
    			{
    
    		System.out.print(""+theChar);
    			System.out.println("");
    
                            i++;
    			j++;
    
    
    			}
    
    
    			// end output line
    
    		}
    	    return noLines * charPerLine; // show your method my methods results.
    	}

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Think about what the inner loop does each time through it's loop. You have some lines out of place.

  17. #17
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    I'm not quite sure >.> I'm pretty sure the increment lines are out of place though.

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    There are 2 out of place lines.

    Try commenting out different lines in the inner loop and see how the output changes. This will certainly help you spot the problem.

  19. #19
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    I've tried every single combo I could think of for moving lines and I also try commenting out lines and figured out which lines do the vertical lines and which ones does horizontal ones but I still couldn't make it print the correct number of characters on each line.

  20. #20
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Java Code:
    public static int printLines(int noLines, int charPerLine, char theChar)  // my method.
    {
        int i=0;
        int j=0;// variables for loops
        while (i < noLines) // loop for number of lines
        {
            while (j < charPerLine) // loop for characters per line
    	{
    	    System.out.print(""+theChar);
    	    System.out.println("");
                i++;
                j++;
    	 }
    // end output line
           }
        return noLines * charPerLine; // show your method my methods results.
    }
    I re formatted it a bit, does this help you at all?

    Each iteration through the outer loop should produces a full loop of the inner loop. You are going want to separate each time through the inner loop.

    You should check out your for loop version, the print line and print go in the same places as the old one, the only difference is whn to increment the counter for the loop.

    Does it seem logical to increment the outer loops counter each pass through the inner loop?
    Last edited by sunde887; 04-01-2011 at 02:26 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. is it possible to do this to methods?
    By alacn in forum New To Java
    Replies: 8
    Last Post: 08-14-2010, 04:01 PM
  3. methods
    By blueduiker in forum New To Java
    Replies: 12
    Last Post: 02-09-2010, 04:50 PM
  4. Replies: 4
    Last Post: 04-03-2009, 07:36 AM
  5. Get and Set Methods
    By kian_hong2000 in forum New To Java
    Replies: 10
    Last Post: 08-27-2008, 03:06 PM

Posting Permissions

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