Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-03-2007, 01:09 PM
Member
 
Join Date: Jul 2007
Posts: 4
HeavyD is on a distinguished road
Loop Help
Hi,
I need help. Im new to java and am trying to figure something out.

I want to create a pattern loop where the user enters the number of rows to output and also what to output.

e.g

How many rows: 5
Character to be printed: X

X
XX
XXX
XXXX
XXXXX

Would anyone have any clue where i start or show me the coding to help me understand better??

Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-03-2007, 02:04 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Lets say you keep number of rows in numRows variable and character to be printed in charToPrint variable. Then the code will be like this:

Code:
for (int i=1; i<=numRows; i++) { for (int j=1; j<=i; j++) System.out.print(charToPrint); System.out.println(); }
I guess you already know how to get a character and integer from user. If not try google. If something is unclear let me know.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-08-2007, 09:59 AM
Member
 
Join Date: Jul 2007
Posts: 4
HeavyD is on a distinguished road
I have the genereal idea but not sure how to do it. Any other help would be greatly appreciated. I'll keep at it.
Thanks
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-08-2007, 03:02 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
This code shows how you can get a String from user:

Java Tips - Reading Text from Standard Input

After you get String you can convert it to integer or character. For converting it to character, you can call charAt(0) method of that string. And you can see the following tip to convert it to integer value:

Java Tips - Conversion from String to integer

Now, you just need to join these pieces together.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-09-2007, 05:15 AM
Member
 
Join Date: Jul 2007
Posts: 4
HeavyD is on a distinguished road
I've worked out how to let the user input a number and character but still having a problem. Here is the code below. Can anyone tell me what im doing wrong? This is the whole program i created, some which is not complete but its just the FIRST section, pattern section that i need help with.

Code:
import java.util.Scanner; public class assignment1 { public static void main (String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println(); System.out.println("Print Pattern and Reverse String Menu"); System.out.println(); System.out.println("A. Print a pattern"); System.out.println("B. Reverse a string of text"); System.out.println("X. Exit the programme"); System.out.println(); System.out.print("Select an option from the menu (A, B or X): "); char option = keyboard.next().charAt(0); if (option == 'A' || option == 'a') { System.out.println(); System.out.print("Enter the number of rows in the pattern to be printed: "); int rows = keyboard.nextInt(); System.out.print("Enter character to be printed: "); char character = keyboard.next().charAt(0); for (int i = 1; i<=rows; i++) { for (char j=1; j<=i; j++) System.out.print(character + " "); System.out.println(); } } else if (option == 'B' || option == 'b') { System.out.println(); System.out.println("Enter a string of text to be reversed: "); String text = keyboard.nextLine(); int last = text.length(); String reverse = text.substring(last,0); System.out.println("The reverse of the string of text you entered is: " + reverse); System.out.println(); } else if (option == 'X' || option == 'x') { System.out.println("Exiting the programme..."); System.out.println(); System.exit(0); } else { System.out.println("Error! You did not select a valid option from the menu"); } } }

Last edited by JavaBean : 07-09-2007 at 09:10 AM. Reason: Code placed inside [code] tag.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-10-2007, 03:26 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
As far as i understand your problem, that loop should be:

Code:
for (int i = 1; i<=rows; i++) { for (char j=1; j<=i; j++) System.out.print(character); System.out.println(); }
This should work, what is wrong with this?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
do...while loop eva New To Java 16 01-31-2008 08:44 AM
while loop michcio New To Java 5 01-27-2008 02:56 AM
can you help me with this for loop? java_fun2007 New To Java 6 12-22-2007 12:20 PM
A loop that doesn't loop MichYer New To Java 2 07-30-2007 10:44 AM
While loop leebee New To Java 1 07-18-2007 05:11 PM


All times are GMT +3. The time now is 10:15 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org