Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 27th, 2013, 05:46 PM
xRynee xRynee is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 xRynee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 47 sec
Reputation 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);


}
}
}
}

}

Reply With Quote
  #2  
Old January 28th, 2013, 06:23 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,048 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 16 h 53 m 14 sec
Reputation Power: 346
Quote:
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.

Reply With Quote
  #3  
Old January 28th, 2013, 10:59 AM
Wetmelon's Avatar
Wetmelon Wetmelon is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Location: London, ON
Posts: 322 Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 3 Days 9 h 49 m 24 sec
Reputation Power: 213
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
Quote:
Originally Posted by DaWei_M
That covers a multitude of your sins, right there.

Reply With Quote
  #4  
Old January 28th, 2013, 08:49 PM
JonthnC JonthnC is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2010
Posts: 64 JonthnC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 22 h 51 m
Reputation Power: 3
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Help with loops...

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap