Java Thread

Discussion in 'General Software' started by Cromewell, Jan 17, 2011.

  1. Troncoso

    Troncoso VIP Member

    Messages:
    4,421
    It is printing the "\n" as in you see \n in the text, it doesn't cut to a new line. I used this to fix the issue though:

    Code:
    if (deck.getCardDesc(chosenCard).length() > 38){
                if (deck.getCardDesc(chosenCard).length() > 76){
                    wrapText1 = deck.getCardDesc(chosenCard).substring(0, 38);
                    wrapText2 = deck.getCardDesc(chosenCard).substring(39, 76);
                    wrapText3 = deck.getCardDesc(chosenCard).substring(77);
                    System.out.println("| " + wrapText1 + "\n|   " + wrapText2 + "\n|   " + wrapText3);
                }
                else{
                    wrapText1 = deck.getCardDesc(chosenCard).substring(0, 38);
                    wrapText2 = deck.getCardDesc(chosenCard).substring(39);
                    System.out.println("| " + wrapText1 + "\n|   " + wrapText2);
                }
            }
            else
                System.out.println("| " + deck.getCardDesc(chosenCard));
    Not the neatness algorithm. But it does want I want it too. Thanks for the help though.
     
  2. NyxCharon

    NyxCharon Active Member

    Messages:
    1,433
    Yea. I can still give you the example when I get back if you want?
     
  3. Troncoso

    Troncoso VIP Member

    Messages:
    4,421
    That would be awesome. I'm still in the beginning stages of java, trying to learn as much as I can. I probably shouldn't have tried tackling a game so early, but it's actually going rather well.
     
  4. Troncoso

    Troncoso VIP Member

    Messages:
    4,421
    What the heck is wrong with this? It's just suppose to take the array, and find the largest "span"(a span is the distance from one number to the next next duplicate. So an example:

    {1,2,3,2,3,1} the largest span is 6 - from 1 to the next 1))

    here is the code I thought would work:

    Code:
    public class tests {;
    	
    	public static void main(String args[]){
    		int largest = 0, end = 0;
    		int[] nums = {1,2,1,1,3};
    		int size = nums.length;
    		  
    		  for (int i = 0; i < size ; i++){
    		       System.out.println(i);
    		       System.out.println();
    		       for (int j = 0; j < size ; i++){
    		            if (nums[j] == nums[i]){
    		            	end = j;
    		                System.out.println(j);
    		            }
    		       }
    		       if ((end - i + 1) > largest)
    		            largest = (end - i) + 1;
    		  }
    		  System.out.println(largest);
    	}
    }
    I put the print statements in there to show what values are being stored each time, which is always 0. and I keep getting this error:

    Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    	at tests.main(tests.java:13)
    Any help would be cool
     
  5. NyxCharon

    NyxCharon Active Member

    Messages:
    1,433
    nums is of size 5. Arrays start at index 0, so when you try to access the number at slot 5, you cant because the biggest slot is 4. Hence, you get a error thrown.

    make it
    int size = nums.length-1;
     
  6. Troncoso

    Troncoso VIP Member

    Messages:
    4,421
    But when I use it in the for loop I say i< size, so it shouldn't try to aaccess a 5the element.
     
  7. NyxCharon

    NyxCharon Active Member

    Messages:
    1,433
    figured it out, simple error. you had a i++ instead of a j++, so j never got incremented.
    Code:
    public static void main(String[] args) 
        {
           int largest = 0, end = 0;
    		int[] nums = {1,2,1,1,3};
    		int size = nums.length;
    		  
    		  for (int i = 0; i < size ; i++)
                      {
    		       System.out.println(i);
    		       System.out.println();
                               for (int j = 0; j < size ; j++)
                               {
                                    if (nums[j] == nums[i])
                                    {
                                        end = j;
                                        System.out.println(j);
                                    }
                               }
    		       if ((end - i + 1) > largest)
    		            largest = (end - i) + 1;
    		  }
    		  System.out.println(largest);
    	}
     
  8. Troncoso

    Troncoso VIP Member

    Messages:
    4,421
    Haha. Oh man. How sad. At least I know that it was just an over sight. Thanks for the help
     
  9. Bananapie

    Bananapie New Member

    Messages:
    184
    Yeah, I was just reading your code and saw the mistake, haha. The chance to help someone out, and I am beat to it. :(

    How is your game coming along? I've been wanting to make a text based game for a while. I am relatively new as well, but I think it would be a blast just to write something up with no time limit or grade. Just need to figure out what I want to do exactly.
     
  10. Troncoso

    Troncoso VIP Member

    Messages:
    4,421
    Honestly, It's going a lot better than I thought. My usual experience with making anything, is having a really clear vision in my head of what I want and even detailed blue prints on how I'm gonna do it....but it never really turns out like I want.

    This is an exception. So far, it's working just like I plan it to at each stage of production.

    I urge you to go for it. I'm like you, I really enjoy programming when I'm programming what I want, and I don't have to finish by a certain date. As far as an idea is concerned, a lot come to you every day. Just get in the habit of thinking "hey, that would make a good program". That's what happened with me.
     
  11. Bananapie

    Bananapie New Member

    Messages:
    184
    Yeah, I just need to get to it. It sounds amazing. Something I can build now and it be relatively well, and enjoyable, and expand on as I go. :good:

    :edit:
    Definitely going to start brainstorming ideas for something to do for fun. So far, a kind of mix of Final Fantasy and other games, text based sounds most appealing at the moment. Not sure if I should plan to make it die(dice) based game or not. Would definitely make for something fun. Whether or not I am up to the skill level to do something of this sort, but basic, not sure. Worth the shot, and right now I am in the mood for a challenge. Given, it is only a side thing. Classes come first. :(
     
    Last edited: Oct 19, 2011
  12. Troncoso

    Troncoso VIP Member

    Messages:
    4,421
    1 step at a time. I'm starting text based, because it's letting me concentrate on the game play mechanics. Final fantasy based( if you are talking 1 and 2) would be really simple to start out with.
     
  13. Bananapie

    Bananapie New Member

    Messages:
    184
    Yeah, one of the first couple, text based. Gives a lot of room later to add-on and whatnot. Definitely going to try and implement a character creation (Name/Gender/Race/Class and whatnot) to start. Then levels, stats/skills and such. Pretty basic stuff, but a lot to do.

    I figure that I have a lot of time left, why not do something that will last me.

    I'm relatively new to programming in C++(which I plan to do the above in). I programmed previously in Java, which their syntax is close, but there is stuff that I am not entirely used to in C++ yet. Pointers and the sort, which this gives me reason to play with them.

    Definitely not looking to do anything graphically any time soon. Just a clean cut text-based game, with the gameplay mechanics. Just need to brainstorm movement of the game. Maybe a "Proceed to this destination in so-and-so moves" or a dice affiliated movement option. Get that and then do some kind of story/mission deal down the road.
     
  14. zombine210

    zombine210 New Member

    Messages:
    1,358
    nice thread.

    i'm just finishing through my first semester of Java. while i can't write something like this myself yet, i kind of understand what's going on in ya'll programs. it's pretty neat.

    i also wanted to write a game, something simple like black jack.
     
  15. Troncoso

    Troncoso VIP Member

    Messages:
    4,421
    if you've gone through a class (and actually learned the stuff), black jack won't be hard to accomplish (save if you want to build a GUI for it). I would try it, and if you really want to learn how to program, you'll find this will teach you more than anything.

    I like classes on programming to learn syntax, semantics and what not, but when it comes to actual coding, you have to do it yourself to understand it.
     
  16. ScottALot

    ScottALot Active Member

    Messages:
    4,963
    This is supposed to use the Newtonian Method of finding which value of x makes f(x) = 0. The beginning creates random numbers for a6 through a0 so that the function is as follows:

    f(x) = a6*x^6 + a5*x^5 ... a0*x^0

    And the derivative is easy enough to figure out.

    Newtonian's method a dynamic value of x that is represented as (x :) in the function:

    x: = x - f(x) / fprime(x)

    The 'while' loop will continue looping until the value of f(x) (represented as double y) is less than the 0.001. In other words, it continues looping until it reaches a set accuracy. I did remember to set it so that f(x) is an absolute value so it doesn't loop more or less than it's supposed to.

    The program works fine about half of the time, creating a valid approximation of x. However, the other half of the time, it goes into an infinite loop. I programmed this in an easy-teaching program/compiler/debugger called BlueJ, so the format may be a tad different.

    Code:
    import java.util.*;
    
    public class Newton
    {
        public static void main(String [] args)
        {
            Scanner input = new Scanner(System.in);
            System.out.println("Scott Catlin (ScottALot)");
            System.out.println("Program v1.0.0");
            System.out.println("Bug Infested :D");
            System.out.println("Enter up to seven values for a[0] to a[6]. Input 0 for values you would not like to include.");
            System.out.println("All values can be easily edited in the source code. Simply replace the asix - azero values with your respective values.");
            double asix = Math.random();
            double afive = Math.random();
            double afour = Math.random();
            double athree = Math.random();
            double atwo = Math.random();
            double aone = Math.random();
            double azero = Math.random();
            System.out.println("");
            System.out.println(asix+"X^6 + "+afive+"X^5 + "+afour+"X^4 + "+athree+"X^3 + "+atwo+"X^2 + "+aone+"X^1 + "+azero+"X^0");
            System.out.println("");
            System.out.println("Enter in a random x-value");
            
            double x = input.nextDouble();
            double y = asix*Math.pow(x,6)+afive*Math.pow(x,5)+afour*Math.pow(x,4)+athree*Math.pow(x,3)+atwo*Math.pow(x,2)+aone*Math.pow(x,1)+azero*Math.pow(x,0);
            System.out.println("f("+x+") = "+y);
            double yprime = asix*Math.pow(x,6)+afive*Math.pow(x,5)+afour*Math.pow(x,4)+athree*Math.pow(x,3)+atwo*Math.pow(x,2)+aone*Math.pow(x,1)+azero*Math.pow(x,0);
            while (Math.abs(y)>0.001)
            {
                x = x-y/yprime;
                y = asix*Math.pow(x,6)+afive*Math.pow(x,5)+afour*Math.pow(x,4)+athree*Math.pow(x,3)+atwo*Math.pow(x,2)+aone*Math.pow(x,1)+azero*Math.pow(x,0);
                yprime = 6*asix*Math.pow(x,5)+5*afive*Math.pow(x,4)+4*afour*Math.pow(x,3)+3*athree*Math.pow(x,2)+2*atwo*Math.pow(x,1)+1*aone*Math.pow(x,0);
                System.out.println("f("+x+") = "+y);
            }
            System.out.println("");
            System.out.println("f(x) = 0 when x = approximately " + x);
            System.out.println("");
            System.out.print("Thank you for using my program. Improvements will be made in the future");
        }
    }
    
    NOTE: When I made azero = 1 or 0, it works 100% of the time... so maybe that's the source of the issue? I'd like to keep it random, but I might be having a dipshit moment with it.
     
    Last edited: Nov 20, 2011
  17. Troncoso

    Troncoso VIP Member

    Messages:
    4,421
    Are the 7 random numbers suppose to be whole numbers or can they be decimal values?
     
  18. ScottALot

    ScottALot Active Member

    Messages:
    4,963
    Math.random() produces random numbers 0<=x<1, so decimals between 0 and 1.
     
  19. Troncoso

    Troncoso VIP Member

    Messages:
    4,421
    Ah, well, I never use the Math class for random, there is actually a Random class you could use.

    I don't know how well you know your way around Bluj, but it has a pretty extensive debugging mode. You need to find a set of data that gives you an infinite loop and hard code those numbers into your program. After that, you can set a break point at your while loop and keep stepping through it to see why it's not exiting the loop.
     
  20. ScottALot

    ScottALot Active Member

    Messages:
    4,963
    Teach me your ways.
     

Share This Page