Results 1 to 6 of 6

Thread: while loop

  1. #1
    Unknown1369 is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default 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.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Simply do something like this.

    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++;
        }
    It's better to refer some documents about loops. It's not specific to Java, in every language you have.

  3. #3
    Unknown1369 is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default

    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.

  4. #4
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    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

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #6
    Unknown1369 is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

Similar Threads

  1. How to use Do While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:45 PM
  2. How to use While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:44 PM
  3. do...while loop
    By eva in forum New To Java
    Replies: 16
    Last Post: 01-31-2008, 06:44 AM
  4. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 10:20 AM
  5. A loop that doesn't loop
    By MichYer in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 08:44 AM

Posting Permissions

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