Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Beginner for loop

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Beginner for loop

    Hi guys

    I'm having a bit problem with my for-loop, and would just want to hear if i'm way of, or if there is something small i'm missing.

    String[] days = {"Monday", "Tuesday", "Wednesday", "Turshday", "Friday", "Saturday", "Sunday"};
    String opgType = arbejdsOpgaver.opgTypeText.getText();
    String opgave = arbejdsOpgaver.opgText.getText();
    String dag = arbejdsOpgaver.opgDagText.getText();
    String kunde = arbejdsOpgaver.opgKundeText.getText();
     
    boolean opgCheck = false;
    			if (!opgave.equals("") && opgave.charAt(0) != ' ') {
    				if (!dag.equals("") && dag.charAt(0) != ' ') {
    					if (!kunde.equals("") && kunde.charAt(0) != ' ') {
    						opgCheck = true;					
    					}
    				}
    			}
     
    			if (opgCheck == true) {
    				try {
    					String day = null;
    					for (int i=0; i<= days.length; i++) {
    						day.equals(day + days[i]);
    							if (!day.equals(days)) {
    								opgCheck = false;
    							}
    					}
    				}catch (Exception ee) {
    					System.out.println(ee);
    				}	
    			}

    I want the for-loop to run through the array, and if the entered day doesn't match one of the days in the array, make the check = false.

    At the moment it obviously doesn't work because i'm getting an exception:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ajRengøring.logic.createOpgave.actionPerformed(cre ateOpgave.java:32)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Beginner for loop

    What is line 32?

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Beginner for loop

    Line 32 is:

    day.equals(day + days[i]);

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Beginner for loop

    Ah ok. Your day array has a length of 7 (Monday-Sunday are 7 days). Agreed? So the days.length will return 7. Only the thing is, it starts at 0 instead of 1. So "Monday" will be 0. and then it will do 7 more, since your length is 7. So what will Sunday be? Try to print out "i" and the corresponding day to that after each loop.
    You should be able to figure it out yourself now . if you still have a question about it, don't hesitate to ask.
    Last edited by elamre; August 1st, 2012 at 04:14 PM. Reason: Better

  5. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Beginner for loop

    I can see where you are going, and it solved a problem I probably would have run into at some point (Removed the =). But it does not solve the problem itself. I tried to change a bit in that line 32, which got rid of the exception, but it doesn't seem to really run through the array, or at least not adding the content of the array to the string?

    String[] days = {"Monday", "Tuesday", "Wednesday", "Turshday", "Friday", "Saturday", "Sunday"};
    String opgType = arbejdsOpgaver.opgTypeText.getText();
    String opgave = arbejdsOpgaver.opgText.getText();
    String dag = arbejdsOpgaver.opgDagText.getText();
    String kunde = arbejdsOpgaver.opgKundeText.getText();
     
    boolean opgCheck = false;
    			if (!opgave.equals("") && opgave.charAt(0) != ' ') {
    				if (!dag.equals("") && dag.charAt(0) != ' ') {
    					if (!kunde.equals("") && kunde.charAt(0) != ' ') {
    						opgCheck = true;					
    					}
    				}
    			}
     
    			if (opgCheck == true) {
    				try {
    					String day = null;
    					for (int i=0; i<= days.length; i++) {
    						day += days[i]; // previously day.equals(day + days[i]);
    							if (!day.equals(days)) {
    								opgCheck = false;
    							}
    					}
    				}catch (Exception ee) {
    					System.out.println(ee);
    				}	
    			}

  6. #6
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Beginner for loop

    		String[] days = { "Monday", "Tuesday", "Wednesday", "Turshday", "Friday", "Saturday", "Sunday" };
    		String day = "";
    		for (int i = 0; i < days.length; i++) {
    			day += days[i]; // previously day.equals(day + days[i]);
    		}
    		System.out.println(day);
    This is working fine, so i think it might be something else in your code.
    Also try to switch your String day = null to String day = ""; If it still doesnt work then try printing everything out.

  7. The Following User Says Thank You to elamre For This Useful Post:

    gerre (August 1st, 2012)

  8. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Beginner for loop

    Yeah I can also print it. But the if statement somehow doesn't wanna work. Pitty since I now need to test for the days individually so it says Monday OR Tuesday etc. It just doesn't look good that way.

  9. #8
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Beginner for loop

    What do you want to achieve exactly? I'm not quite understanding the purpose of this yet.
    Also post your full output. This is what i did:
    	public static void main(String[] args){
    		String[] days = { "Monday", "Tuesday", "Wednesday", "Turshday", "Friday", "Saturday", "Sunday" };
    		String day = "";
    		for (int i = 0; i < days.length; i++) {
    			System.out.println("Day: " +days[i]);
    			day += days[i]; // previously day.equals(day + days[i]);
    		}
    		System.out.println(day);
    	}
    And i get the following output, everything is working fine:
    Day: Monday
    Day: Tuesday
    Day: Wednesday
    Day: Turshday
    Day: Friday
    Day: Saturday
    Day: Sunday
    MondayTuesdayWednesdayTurshdayFridaySaturdaySunday
    Could you give some more code ant tell us where it is going wrong?
    Last edited by elamre; August 1st, 2012 at 06:27 PM.

  10. #9
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Beginner for loop

    Well, the end product should be to add data to a database. This particular part of the class is supposed to check whether the entered input is a valid day. In other words the program should go: Is it a monday? No, is it a Tuesday? No etc until the entered data matches one in the array, and it continues in the code.

    The class as it looks at the moment.
    public class createOpgave implements ActionListener {
    	public void actionPerformed(ActionEvent e) {
    		String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
    		String opgType = arbejdsOpgaver.opgTypeText.getText();
    		String opgave = arbejdsOpgaver.opgText.getText();
    		String dag = arbejdsOpgaver.opgDagText.getText();
    		String kunde = arbejdsOpgaver.opgKundeText.getText();
     
    		if (opgType.equals("Privat")) {
    			boolean opgCheck = false;
    			if (!opgave.equals("") && opgave.charAt(0) != ' ') {
    				if (!dag.equals("") && dag.charAt(0) != ' ') {
    					if (!kunde.equals("") && kunde.charAt(0) != ' ') {
    						opgCheck = true;					
    					}
    				}
    			}
     
    			if (opgCheck == true) {
    				String day = "";
    					for (int i=0; i< days.length; i++) {
    						if (!dag.equals(day)) {
    							opgCheck = false;
    							day += days[i];
    						}
    					}
    			}	
    			if (opgCheck == true) {				
    					try{
    						connect.forbindelse();
    						//SQL kaldet laves til at indsætte i afdelingtabellen
    						String priOpg = ("INSERT INTO opgaverPrivat VALUES(null,'"+opgave+"','"+dag+"','"+kunde+"')");
    						connect.st.executeUpdate(priOpg);
    						JOptionPane.showMessageDialog(null, "Registered");
     
    						}catch (Exception ee) {
    							System.out.println(ee);
    						}
    				} else
    				JOptionPane.showMessageDialog(null, "Error");			
    		}

    Apart from that it only getText from some gui component in another class.

  11. #10
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Beginner for loop

    String day = "";
    					for (int i=0; i< days.length; i++) {
    						if (!dag.equals(day)) {
    This part, what should it do?

  12. #11
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Beginner for loop

    Quote Originally Posted by elamre View Post
    String day = "";
    					for (int i=0; i< days.length; i++) {
    						if (!dag.equals(day)) {
    This part, what should it do?
    The code should probably look like this:
    String day = "";
    					for (int i=0; i< days.length; i++) {
                                                    day += days[i];
    						if (!dag.equals(day)) {

    That obviously doesn't do what I want because that's the part that doesn't work. But that is the day check. The plan is to run through the array with the loop, and for every content(day) of the array check for whether that is the entered input in my textfield(dag). If it doesn't matches set the check = false. Since it was true at the beginning of the loop it shouldn't be necessary to change the check if it matches.

    I do see a problem coming up if the current problem is solved, because if you enter Monday it will match, but the loop will continue on and override the String and make it false anyway. So somehow it should also break out if it matches. But at the moment it doesn't work even with Sunday so it can't be that problem.

  13. #12
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Beginner for loop

    In other words the check should be the same as this one at the top, where I individually check for every day:

    if (opgCheck == true) {
    				if (dag.equals("Monday") || dag.equals("Tuesday") || dag.equals("Wednesday") || dag.equals("Thursday")	|| dag.equals("Friday") || dag.equals("Saturday") || dag.equals("Sunday")) {
    					try{
    						connect.forbindelse();
    						//SQL kaldet laves til at indsætte i afdelingtabellen
    						String priOpg = ("INSERT INTO opgaverPrivat VALUES(null,'"+opgave+"','"+dag+"','"+kunde+"')");
    						connect.st.executeUpdate(priOpg);
    						JOptionPane.showMessageDialog(null, "Opgave registeret");
     
    						}catch (Exception ee) {
    							System.out.println(ee);
    						}

Similar Threads

  1. [SOLVED] Beginner, stuck on implementing while loop, compiles fine but still won't run
    By GregC in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 1st, 2012, 06:36 PM
  2. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  3. Loop Question - Very new beginner
    By Callcollect in forum Loops & Control Statements
    Replies: 12
    Last Post: January 18th, 2012, 04:01 AM
  4. Loop Patterns (triangles) [Beginner]
    By me1010109 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 24th, 2010, 05:13 PM
  5. [SOLVED] While Loop Help (beginner)
    By Perplexing in forum Loops & Control Statements
    Replies: 4
    Last Post: October 23rd, 2010, 02:00 PM