Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java help needed please

Options
  • 29-01-2014 12:55am
    #1
    Registered Users Posts: 4,056 ✭✭✭


    I know my code is pretty poor but in my defense i can only use what is in the first 12 chapters of the Blue pelican java book.

    What i am having massive trouble with is avoiding the infinite loop that i created to keep the program running until the user wants to end(I keep hearing that they are a mortal sin)

    The project entails 5 options with the 1st one password protected the user must be able to navigate through the program until they decide to quit.



    Am i making a mistake in using switch with scanner?
    Should i dump all i have done and start from scratch?
    Why is there no GoTo statement in java?(I would happily take a ticking off if i could find an easy way to jump back to main menu without the infinite loop)

    This is a outline of my code as with it being a college project i cant post the actual code.

    I am looking for any ideas and observations that you might have.I am not looking for of expecting code that i can copy and paste as i really want to get a good handle on java.

    Thank you for taking the time to read this

    Sparks43




    import java.util.*;
    import java.io.*;
    
    
    
    
    public class Project
    	{
    	public static void main(String args[])
    		
    	{
    		Scanner a = new Scanner(System.in);// for main choice
    		Scanner ch = new Scanner(System.in);//For choice in case 1
    
    
    		for(c=1;c>0;c=c)          // I want the program to keep looping until i subtact 1 from c which ends it
    
    		{
    
    
    
         		System.out.println(" 1");					
    		System.out.println(" 2");   // Choices prompts
    		System.out.println(" 3");
    		System.out.println(" 4");
    		System.out.println(" 5");
    		int choice = a.nextInt();
    
    		switch(choice)
    
    
    
    		case 1:     //now it gets fun 
    		
    		int p = 3 //3 attempts to login then get the boot 
    		
    		for(p=3;p>0;p-1)
    		{	
    			System.out.println(" Password");
    			String verify = ch.next();
    				
    				if (verify.equals(correct))		
    				{
    
    
    
    
    
    
    					System.out.println(" A");					
    					System.out.println(" B");   // Choices prompts
    					System.out.println(" C");
    					char x = ch.charAt(0);
    			
    					
    
    					case "A":
    
    							//do stuff then go back to case 1 menu
    					case "B":
    
    							//same as Case A
    			
    					case "C":
    							//Quit to main menu
    
    
    					}
    
    				else if (verify !=(correct)) //not sure of correct syntax for this ideas please
    				{
    				
    					System.out.println("Incorrect")
    					p = p-1
    						if p = 0// after 3 incorrect attempts this triggers and program is terminated
    							{
    								c=c-1
    								break;
    									}
    					break;
    
    					}
    
    		}  //end of case 1 loop	
    
    
    		
    
    
    
    
    
    
    		case 2:
    
    			//Do some stuff then prompt user to choose if they want to exit
    
    			if (choice = yes)
    			
    			{
    			c = c-1
    			break; // Terminate initial loop and end program
    			}
    			else
    			{
    			break;//To go back to initial menu
    
    			}		
    		case 3: //same as case 2
    		case 4: //same as case 2
    		case 5: //same as case 2
    
    
    		}
    			System.out.println("Thank you Goodbye")//end program
    
    
    	}
    



    tl;dr Tbh i dont blame you:D


Comments

  • Registered Users Posts: 1,144 ✭✭✭Ballyv24


    Few changes that I'd recommend.

    Add a while statement instead of the for loop for the c variable. Something like:
    boolean blah = true
    while(blah) {
    Add the switch logic here. When you are ready to exit, set blah = false
    }

    Then clear all the code from the switch/case sections and just add a println in each so you know that the case logic is working.
    I tthink that there should be a break at the end of each case block.


  • Registered Users Posts: 4,056 ✭✭✭Sparks43


    Ballyv24 wrote: »
    Few changes that I'd recommend.

    Add a while statement instead of the for loop for the c variable. Something like:
    boolean blah = true
    while(blah) {
    Add the switch logic here. When you are ready to exit, set blah = false
    }

    Then clear all the code from the switch/case sections and just add a println in each so you know that the case logic is working.
    I tthink that there should be a break at the end of each case block.

    That is absolutely brilliant

    Much more streamlined and efficient

    Spent a good few hours struggling with getting some progression of logic with no joy but doing a full rebuild and using the boolean has made it so much better.

    After i submit the assignment in the next couple of weeks i will post the final code up to see if i could have trimmed it down any further.

    Thank you very much m8


  • Registered Users Posts: 7,863 ✭✭✭The_B_Man


    Is that a direct copy and paste of your code, or jut pseudocode?
    Coz I just skimmed it there and its missing some "break" statements in the switch, and I think I saw a missing semi-colon or two, as well as some use of the assignment operate "=" instead of the "==" to compare two values.
    They might be giving you the issues you mentioned.

    As for the infinite loop, in your first for() loop, you're ending with "c=c" so unless you're decrementing it somewhere, it will never break out of the loop.


  • Registered Users Posts: 4,056 ✭✭✭Sparks43


    The_B_Man wrote: »
    Is that a direct copy and paste of your code, or jut pseudocode?
    Coz I just skimmed it there and its missing some "break" statements in the switch, and I think I saw a missing semi-colon or two, as well as some use of the assignment operate "=" instead of the "==" to compare two values.
    They might be giving you the issues you mentioned.

    As for the infinite loop, in your first for() loop, you're ending with "c=c" so unless you're decrementing it somewhere, it will never break out of the loop.

    I retyped a rough copy of my original code to give an idea of the problem i was having as i couldnt post the full code that would compile.

    I was breaking the loop with c=c-100 to terminate the loop and end the program but the boolean is much better.

    Have another issue atm but i am going to give it a bit of thought and effort before i give up post it and grab a bottle of wine.

    So about 8pm this evening if i dont figure it out :D


Advertisement