Java Thread

Troncoso

VIP Member
Yup, I'll give you a full example when I'm off work. I had a similar issue when i was writing a notepad like program, and using a buffered reader over a scanner worked to fix my problem. I'd write one out now, but I can't remember the exact code :p

Edit: You are saying that it IS printing the "/n" character correct?

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.
 

Troncoso

VIP Member
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.
 

Troncoso

VIP Member
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
 

NyxCharon

Active Member
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;
 

NyxCharon

Active Member
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);
	}
 

Troncoso

VIP Member
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);
	}

Haha. Oh man. How sad. At least I know that it was just an over sight. Thanks for the help
 

Bananapie

New Member
Haha. Oh man. How sad. At least I know that it was just an over sight. Thanks for the help

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.
 

Troncoso

VIP Member
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.

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.
 

Bananapie

New Member
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.

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:

Troncoso

VIP Member
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.
 

Bananapie

New Member
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.

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.
 

zombine210

New Member
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.
 

Troncoso

VIP Member
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.
 

ScottALot

Active Member
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:

Troncoso

VIP Member
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.

Are the 7 random numbers suppose to be whole numbers or can they be decimal values?
 

Troncoso

VIP Member
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.
 
Top