-
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.
-
Simply do something like this.
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++;
}
It's better to refer some documents about loops. It's not specific to Java, in every language you have.
-
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.
-
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.
-
System.out.print not added any values into the arrays. Just run and see what happened.
-
thank you both. that helps alot.