#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Exclamation Help with loops...


    So my main goal is to collect a list of names.. then at my own will check the list of names.. i'm stuck because the way "n" variable is acting doesn't make sense to me.. I'm only printing it to see what's going on with it.. Please help me.
















    import javax.swing.*;
    public class Loopinaloop {

    public static void main(String[] args) {

    String name[] = new String[2500];
    for(int i=0;i<++i;i++){
    String start = JOptionPane.showInputDialog("Please select the number that you wish to do you wish to do\n1.Give name\n2.Check the list");
    if(start.equals("1")){
    String names = JOptionPane.showInputDialog("Good! What is your name?");
    name[i] = names;

    }
    else if(start.equals("2")){
    for(int n = 0; n<i; n++){

    System.out.println(n);


    }
    }
    }
    }

    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    the way "n" variable is acting doesn't make sense
    Please post the program's output and explain what the problem is.

    Please edit you post and wrap the code in code tags and remove the extra spaces.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Location
    London, ON
    Posts
    322
    Rep Power
    214
    Well, you have an infinite loop determined by i, which is incremented by 2 on every iteration (i<++i, and i++). In your inner loop, n is defined at 0 then each iteration increments it by one (n++), with the loop ending at n==i
    <Tetrad> the program I just wrote 1) compiled the first time without any errors and 2) worked like it was supposed to
    <Tetrad> I don't know whether to be proud or scared to death
    Originally Posted by DaWei_M
    That covers a multitude of your sins, right there.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    68
    Rep Power
    4
    You would be best off using a while loop in my opinion. A for loop is really inappropriate for what you're doing. Also, you only need to increment i when the user enters 1 (assuming you're using it to store the amount of names entered).

    Try structuring your code like this ----
    Code:
    public static void main(String[] args) {
        String name[] = new String[2500];
        int i = 0;
        while (true) {
            String start = JOptionPane.showInputDialog("Please select the number that you wish to do you wish to do\n1.Give name\n2.Check the list");
            if(start.equals("1")){
                String names = JOptionPane.showInputDialog("Good! What is your name?");
                name[i] = names;
                i++;
            }
            else if(start.equals("2")){
                for(int n = 0; n<i; n++){
                    System.out.println(n);
                }
            }
        }
    }
    You might want to also check out ArrayLists. You could technically have a buffer overflow if the user enters more than 2500 names. Not solving issues such as this can lead to runtime errors (which are a pain to solve) and are also exploitable by hackers
    Last edited by JonthnC; January 29th, 2013 at 12:50 AM.

IMN logo majestic logo threadwatch logo seochat tools logo