Results 1 to 6 of 6
Thread: while loop
- 07-08-2008, 05:56 AM #1
Member
- Join Date
- Jul 2008
- Posts
- 10
- Rep Power
- 0
while loop
hi, i am having a bit of a problem. i'm a newb when it comes to programming, especially java. i need to know how to start this problem.
public class printContact {
public static final int MAX_ENTRIES = 10;
public static int curr_entries = 6;
public static String[] first_names = {"Stan","Fredia","James","Sandra","Jerry","Robin"} ;
public static String[] last_names = {"Marko","Zells","Johnson","Christmas","kelly","By rde"};
public static String[] street_address = {"2 Easy Street","23 King Junction","10207 W. OuttaMy Way","18 Walker Blvd","00 Memory Lane","16 Lovers Lane"};
public static String[] city = {"Ann Arbor","Ypsilanit","Belleville","Dexter","Maumee", "Toledo"};
public static String[] state = {"Michigan","Michigan","Michigan","Michigan","Ohio ","Ohio"};
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
1. On the first line, separated by space, print first name and last name.
2. On the second line the street address.
3. On the third line, separated by a comma and a space, write out the city and state.
Since the information is stored in parallel arrays use a while loop to print out the information. Use integer variables to index the arrays and also use integer(s), boolean(s) and/or constants to terminate the while loop. Do not use hard coded numeric values in the code except to initialize constants or variables.
thank you.
- 07-08-2008, 07:06 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Simply do something like this.
It's better to refer some documents about loops. It's not specific to Java, in every language you have.Java Code:int i = 0; while(i < curr_entries) { System.out.println(first_names[i] + " " + last_names[i]); System.out.println(street_address[i]); System.out.println(city[i] + ", " + state[i] + "\n"); i++; }
- 07-08-2008, 07:26 AM #3
Member
- Join Date
- Jul 2008
- Posts
- 10
- Rep Power
- 0
thank you. i sorta understand most of it. don't understand how you came up with "curr_entries" tho.
anyways, another question.
Includes a function (method), called from "main" that adds your own first name and last name and fictitious contact information to the end of the parallel arrays
i have this written up:
System.out.print("John");
System.out.print(" ");
System.out.println("Doe");
System.out.println("1 Washington Street");
System.out.print("Detroit");
System.out.print(", ");
System.out.println("Michigan");
is this correct? or is there a simpler way to do it instead of repeating "System.out.print"?
Thanks again.
- 07-08-2008, 07:43 AM #4
Code given by Eranga does the same what u r asking for. That code adds your own first name and last name and fictitious contact information. If u want to add 1 more detail add it to the array instead of doing sysout's everytime...
If u r not comfortable with while loop(easiest) u can even go for using FOR loop.To finish sooner, take your own time....
Nivedithaaaa
- 07-08-2008, 07:45 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
System.out.print not added any values into the arrays. Just run and see what happened.
- 07-08-2008, 10:15 AM #6
Member
- Join Date
- Jul 2008
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
How to use Do While loop
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:45 PM -
How to use While loop
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:44 PM -
do...while loop
By eva in forum New To JavaReplies: 16Last Post: 01-31-2008, 06:44 AM -
can you help me with this for loop?
By java_fun2007 in forum New To JavaReplies: 6Last Post: 12-22-2007, 10:20 AM -
A loop that doesn't loop
By MichYer in forum New To JavaReplies: 2Last Post: 07-30-2007, 08:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks