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 5 of 5

Thread: Help with a broken for loop?

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Help with a broken for loop?

    Hello all,

    I'm a new member and relatively new to Java; I took two years in high school but that was about four years ago. I'm making a turn based strategy game based on Final Fantasy Tactics and I'm trying to make a system that will read in the speeds for each character and then (for the time being) just spit it back out, with hopes that I might eventually have it process the speeds and create a fluid and variable system that allows for applying multipliers like haste/slow/stop and such.

    Right now, though, the issue is that when you input a value in the main argument (the for loop after I instantiate "print") it reads several lines at a time and then outputs numbers that have nothing to do with those input. If that's not a good explanation, running it and throwing in some junk numbers will make the problem quickly apparent. My code looks solid to my atrophied debugging skills and I can't for the life of me figure out why the input is so broken... the code is below and anyone who can offer me any pointers (related to the specific problem or not) would make my day in doing so.

    import java.io.*;
     
    public class turnOrder {
    	public static int[] speeds = new int[14]; //the values for the speed of each character
    	public static int[] ct = new int[14]; //how close each character is to getting a turn
    	public static String[] names = {"Red Hume","Red Moogle", "Red Viera", "Red NuMou", "Red Bangaa", "Red Seeq", "Red Summon",
    							"Blue Hume", "Blue Moogle", "Blue Viera", "Blue Scamron" ,"Blue NuMou", "Blue Bangaa", "Blue Seeq", "Blue Summon"};
     
     
    	public static void main(String[] args) throws IOException {
     
    		int print; //for loop
    		for (print=0;print<12;print++){
    			System.out.println("Input speed for "+ names[print]);
    			speeds[print]=System.in.read();} //requests and reads in all the speeds for each character
     
     
    		speeds[12]=0; //this space reserved for beastmaster class summons
    		speeds[13]=0; //this space reserved for beastmaster class summons
     
    		for (print=0;print<12;print++)
    			System.out.println("Speed for "+ names[print] + " is " + speeds[print]);
     
     
    	}
     
    	public void haste (int multiplier, int index)
    	{
     
    		speeds[index]*=multiplier;
    		//needs a way to make the haste count down automatically? Perhaps instantiate a count variable and implement a for or while loop
    		// keep track of how many turns haste has left, and as long as haste has at least a turn left, keep the speeds[index]*=multiplier up
    		/*
    		 * int count;
    		 * while (count > 0){
    		 * 		if (count = 0)
    		 * 			xxxx
    		 * }
    		 */
     
     
    	}
     
    	public void turns()
    	{
    		int a; //for loop
    		int turnindex=-1; //set to a positive when a unit reaches 1000CT, otherwise stays -1
    		for (a=0; a<15; a++) { //loop cycles through the indexes of ct and adds the corresponding speed values, just once
    			ct[a]+=speeds[a];  //will likely need to run 10-12 times before anyone gets a turn
    			System.out.println("Turn has ended");
    			if (ct[a]>999)	   //at the end of each run, if nothing printed (such as a unit getting a turn) run again
    				turnindex=a;   //once a unit gets a turn, break and wait for a prompt to continue or to set a character's speed or CT
    		}
    		if (turnindex>0){
    			System.out.println(names[turnindex]+" gets a turn!");
    			ct[turnindex]=0;
    			turnindex=-1;}
    	}
     
    	public void summon(int speed, boolean blueteam)
    	{
    		int x=0;
    		if (blueteam = true) 
    			x+=1;
    		ct[12+x]=0; //resets the summoned monster's CT
    		speeds[12+x]=speed; //sets index 12 or 13 to the given speed depending on summoned monster's allegiance
     
    	}
     
    	public void setSpeed(int speed, int index)
    	{
    		//reads from console to set one character's speed
    	}
     
    	public void setCT (int index)
    	{
    		ct[index]=0;
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Help with a broken for loop?

    then outputs numbers that have nothing to do with those input.
    Can you copy the output and paste it here? Add some comments saying what is wrong with the output and show what the output should be.


    You are reading directly from the OS and getting what it sends without any help. The Scanner class methods would help you get cleaner data.
    System.in.read()
    The pressing the Enter key sends the characters entered plus the endline characters one at a time.
    To see: write a simple program with a loop that reads with the above and prints out what is returned by read().
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Help with a broken for loop?

    Quote Originally Posted by Iantaylora View Post
    speeds[print]=System.in.read();
    System.in.read() reads 1 byte from the standard-input. If you type '1', you get the ascii code of the character '1' which is 49. It is surely not what you want.
    If you want to read numerical values, use java.util.Scanner.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  4. The Following User Says Thank You to andbin For This Useful Post:

    Iantaylora (December 19th, 2013)

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,801
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: Help with a broken for loop?

    By Java convention, class names begin with capital letters.

    'print' is a lousy name for a variable.

    Use an instance of Scanner to get the user's input from the keyboard. There are several examples of how to do this on the Internet. Search for and pick your favorite.

    Once you've implemented using a Scanner instance for input, I think your problem will be solved. Then it'll be on to new ones.

  6. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Help with a broken for loop?

    Andbin, that makes perfect sense, thanks so much! It completely solved my problem. If I run into further issues, should I start a new thread or should I edit the OP for this one, or what is the etiquette here?

    --- Update ---

    The next problem is a little bit more vague. Using the speeds that were indexed at the start of the main argument, I want to increment values in an array until one hits 1000, at which point the method pauses, outputs who hits 1000 and thereby gets a turn, and waits for a value to subtract from that 1000 (getting a turn is -200, and then a further -400 for moving or acting). I figure the best way to do this is a for loop, like so:

    Scanner scan=new Scanner(System.in);
    		int a; //for loop
    		int turnindex=-1; //set to a positive when a unit reaches 1000CT, otherwise stays -1
    		for (a=0; a<15; a++) { //loop cycles through the indexes of ct and adds the corresponding speed values, just once
    			ct[a]+=speeds[a];  //will likely need to run 10-12 times before anyone gets a turn
    			if (ct[a]>999)	   //at the end of each run, if nothing printed (such as a unit getting a turn) run again
    				turnindex=a;   //once a unit gets a turn, break and wait for a prompt to continue or to set a character's speed or CT
    		}
    		if (turnindex>0){
    			System.out.println(names[turnindex]+" gets a turn! How much CT do they lose?");
    			ct[turnindex]-=scan.nextInt(); //subtracts appropriate CT
    			turnindex=-1;} //resets turnindex
    }

    The most obvious issues I can see with this is that it hiccups when two units with similar or identical speeds hit 1000 in the same run through the for loop. Is the best way to deal with this by making a method within the loop, or by having the loop "break" when a unit hits 1000, or by some other avenue entirely? Again, thank you all for your help.

    --- Update ---

    Okay, I think I like what I've got going here.

    public void turns()
    	{
    		int a; //for loop
    		boolean b = true; //keeps while loop running
    		while(b=true)
    			for (a=0; a<15; a++) { //loop cycles through the indexes of ct and adds the corresponding speed values, just once
    				ct[a]+=speeds[a];
    				if(ct[a]>999)
    					turnBreak(a);}  //will likely need to run 10-12 times before anyone gets a turn
    		}
     
     
    	public void turnBreak(int whoseTurn)
    	{
    		Scanner scan = new Scanner(System.in);
    		System.out.println(names[whoseTurn]+" gets a turn! How much CT do they lose?");
    		ct[whoseTurn]-=scan.nextInt(); //subtracts appropriate CT
    		System.out.println("Got it. Anything else to report?");
    		//filler space for applying death, haste, slow, etc.
    	}

    Is there anything really tremendously stupid going on here? If not, then I think I only have one major problem left.

Similar Threads

  1. Replies: 2
    Last Post: December 16th, 2013, 03:36 PM
  2. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  3. 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, 07:57 PM
  4. send image file from java to php broken pipe exception occured
    By ashi123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 21st, 2011, 02:53 PM
  5. Broken JPanel?
    By psu in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 22nd, 2011, 11:11 AM