Results 1 to 19 of 19
Like Tree2Likes
  • 1 Post By downWithJava
  • 1 Post By downWithJava

Thread: running total for loop structure.

  1. #1
    downWithJava is offline Member
    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Default running total for loop structure.

    Hi guys new here, Nice to meet you. I have a structure problem with a running total in my for loop, apologies if my terminology is not up to standard. Here is a snippet of code (this is not a great version but the logic should be the same)
    Java Code:
    import java.util.Scanner;
    
    public class models{
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Scanner kb = new Scanner(System.in);
    		final String[] CAR_CODE = { "1a", "2a", "3a",
    				"4a", "5a"};
    		final String[] CAR_NAMES = { "Toyota",
    				"Ford", "Subaru",
    				"Dodge",
    				"cheverolet"};
    		int entry = 0;
    		String code, chosen = "";
    		do{
    			
    		
    		System.out.println("please enter a code: 1a; 2a; 3a; 4a; 5a ");
    		code = kb.nextLine();
    			for (int i = 0; i < CAR_NAMES.length; i++)
    			if (code.equals(CAR_CODE[i])){
    		System.out.println( CAR_NAMES[i]+" is the chosen manufacturer");
    		entry++;
    		if (entry>4){
    			for (int a = 0; a <CAR_CODE.length; a++){
    				if (code.equalsIgnoreCase(CAR_CODE[a]))
    					chosen+=CAR_CODE[a] + CAR_NAMES [a];}
    			System.out.println("models chosen*were:\n"+ chosen);
    			System.exit(0);
    		
    		}
    		}
    		
    		}while(entry<6);
    		
    }
    }
    I want the print out at the end to list all the models I chose but it only prints out the last one that is read in. Any ideas in where my fault is?

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: running total for loop structure.

    Hi downWithJava, welcome to the forums.

    The problem you have is each time the application loops through the do-while loop the user input is assigned to 'code' overwriting the previous entry.
    If you want to accept multiple entries you would need to create an array of user inputs to iterate through.

    Regards.

  3. #3
    downWithJava is offline Member
    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Default Re: running total for loop structure.

    Thanks for reply, I am not very good at this have you any sample that I could go off?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: running total for loop structure.

    Quote Originally Posted by downWithJava View Post
    Thanks for reply, I am not very good at this have you any sample that I could go off?
    Better for you to try to first do this yourself, and failing that, post your attempt here as a response to this question.

  5. #5
    downWithJava is offline Member
    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Default Re: running total for loop structure.

    Havent had the chance to go at the code again yet so once I try a bit I will get back to you.
    DarrylBurke likes this.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: running total for loop structure.

    Please edit your post and wrap the code in code tags so that we can read it.
    BB Code List - Java Programming Forum - Learn Java Programming

    Please tell us how your code isn't working.

  7. #7
    downWithJava is offline Member
    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Default Re: running total for loop structure.

    very sorry about that.
    Java Code:
     
    import java.util.Scanner;
    
    import java.util.Scanner;
    public class testing {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Scanner kb = new Scanner(System.in);
    		final String[] CAR_CODE = { "1a", "2a", "3a",
    				"4a", "5a"};
    		final String[] CAR_NAMES = { "Toyota",
    				"Ford", "Subaru",
    				"Dodge",
    		"cheverolet"};
    		String code = "", chosen = "";
    		int entry = 0;
    		String modList[] = new String[5];
    		int count=0;
    		int i;
    		do{
    			System.out.println("please enter a code: 1a; 2a; 3a; 4a; 5a ");
    			code  = kb.nextLine();
    			for (i = 0; i < 5; i++)
    				modList[i] = code;
    
    			for (i = 0; i <5; i++)
    				if (code.equals(CAR_CODE[i])){
    					System.out.println( CAR_NAMES[i]+" is the chosen manufacturer");
    					entry++;
    					if (entry>4){
    						for (int a = 0; a <CAR_CODE.length; a++){
    							if (code.equalsIgnoreCase(CAR_CODE[a]))
    								modList[a]= chosen;
    						}
    						System.out.println("models chosen*were:\n"+chosen);
    						System.exit(0);
    
    					}
    				}
    
    		}while(entry<6);
    
    	}
    }
    My logic is not great at this point. But I thought by creating the new String[] modList that I could store the user input in this and then print out at the end when its required. But nothing prints out when I run it.
    Last edited by downWithJava; 03-03-2013 at 09:58 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: running total for loop structure.

    Next I'm going to ask you to put some effort into properly formatting your code as your indentations are all over the place, again making your code difficult to read. You need to have all code that is on the same block indented the exact same amount (I use 3 spaces), as this will be able to tell us at a glance where a block of code begins and ends. Again I don't think that this is asking too much effort on your part. For example:

    Java Code:
    public class Foo {
    
       //  indent 3 spaces
       public Foo() {
          // indent 3 more spaces
          System.out.println("inside of a constructor");
          System.out.println("about to leave constructor"); // codes line up
       } // see how the end brace lines up with the constructor line
    
       public int bar() {
          // all code on the same block lines up exactly
          int a = 3;
          int b = 6;
          int c = a + b;
          return c;
       }
    }

  9. #9
    downWithJava is offline Member
    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Default Re: running total for loop structure.

    thanks for this too.. this will hopefully improve my overall outcome..

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: running total for loop structure.

    Good. Now consider re-formatting your existing code, and let's then have a look at it.

  11. #11
    downWithJava is offline Member
    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Default Re: running total for loop structure.

    re-formatted now.. still sucks though..

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: running total for loop structure.

    Your program looks very complex for what I am guessing its purpose is. Exactly what is supposed to occur? I'm guessing that you are trying to get user input 6 times, and the show the user selection. If so, why are you nesting for loops inside of while loops and the like? Can you please tell us exactly what you're trying to do with this code?

  13. #13
    downWithJava is offline Member
    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Default Re: running total for loop structure.

    Yes as you said, ask user to input there choices then display all the chosen ones in a list. the for loops are there to iterate through the arrays to see if what is typed is there e.g. 1a. The main while loop was to ask the question repeatedly and the last for loop was just a stupid error on my part.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: running total for loop structure.

    Then what is this for loop supposed to be doing?

    Java Code:
          do {
             System.out.println("please enter a code: 1a; 2a; 3a; 4a; 5a ");
             code = kb.nextLine();
    
             // this for loop below
             for (i = 0; i < 5; i++)
                modList[i] = code;
    And likewise, what is this for loop supposed to do?
    Java Code:
                      for (int a = 0; a < CAR_CODE.length; a++) {
                         if (code.equalsIgnoreCase(CAR_CODE[a]))
                            modList[a] = chosen;
                      }
    Trust me, they're not doing what you think that they might be doing and in fact they serve no real useful purpose.

    I think that you may wish to restart the program but start with the logic before you write the code. Write out pseudocode detailing exactly the logic that should be performed, and once you have that figured out, and only then, write the code. Then test the code progressively as you create it.

    Also as an aside remember that all loops and blocks should be surrounded in curly braces including for loops and if blocks with only one line of code in them.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: running total for loop structure.

    Tips:
    • If you know in advance how many times that you want to get input from the user, then use a for loop for this, not a do-while loop or a while loop.
    • If you need to loop an unknown number of times, say to get repeated input from the user until the input is correct, then don't use a for loop for this but rather a do-while loop or while loop.
    • Again, enclose all loops, if blocks, else blocks, inside curly braces. This will save your tail in the future.
    • The logic is much more important than the code. For instance it will tell you where to put your System.out.println statements which are currently misplaced.
    • Do not use System.exit(...). You're currently doing this as a kludge because your println statements are in the wrong location. Let the program end when it runs out of code.

  16. #16
    downWithJava is offline Member
    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Default Re: running total for loop structure.

    The second one shown has no purpose. But my idea with the first one was to.. create a new array as suggested to store user inputs and the for loop was to iterate through that list (poor logic really) and now I am just trying to see a clearer picture with it.

  17. #17
    downWithJava is offline Member
    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Default Re: running total for loop structure.

    Thanks for all this effort man, the code I give is just a snippet of what I am really trying to learn from you guys, its not really related to the programs I actually need to do but just ones similar, so even if I get told to much without using to much effort of my own I still have to apply them to my proper programs. I hope this approach gives me a better understanding of the language..

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: running total for loop structure.

    Quote Originally Posted by downWithJava View Post
    The second one shown has no purpose. But my idea with the first one was to.. create a new array as suggested to store user inputs and the for loop was to iterate through that list (poor logic really) and now I am just trying to see a clearer picture with it.
    create the array, sure, but don't fill it with worthless Strings. Only fill it with Strings that make sense. You should fill one item in the array once with each iteration of the outer loop (which probably should be a for loop).

  19. #19
    downWithJava is offline Member
    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Default Re: running total for loop structure.

    Will keep at this when I get time an if get it going I will get back to you.
    Fubarable likes this.

Similar Threads

  1. Trying to create a running total.
    By skybeorn in forum New To Java
    Replies: 13
    Last Post: 03-16-2012, 07:27 PM
  2. Keeping track of running total help
    By masterasia in forum JCreator
    Replies: 1
    Last Post: 03-25-2011, 07:26 AM
  3. Running total little help
    By silverspoon34 in forum Forum Lobby
    Replies: 9
    Last Post: 02-27-2011, 09:19 PM
  4. Running Total by Key Group in HashMap
    By KevMull in forum New To Java
    Replies: 3
    Last Post: 11-16-2009, 03:44 PM
  5. while Loop (Repetition) structure.
    By gapeach97 in forum New To Java
    Replies: 9
    Last Post: 12-03-2008, 11:52 PM

Posting Permissions

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