Results 1 to 8 of 8
- 04-07-2016, 01:28 AM #1
Having trouble with primitive type 'long'
Please help with my coding chaos! I'm getting some errors in my code like:
Spiral.java:25: possible loss of precision
found : long
required: int
int yNeg = hCen - 20 - (20 * fib(j));
I understand that I gave it the wrong data type but I'm not sure how to cast (is that the right term?) an int to a long. What are your recommendations? Here is my code:
Java Code:/* Created by John Thrush and user3266259 (of stackOverflow) 4/06/2016 Spiral is an app that displays a rectangular spiral and should incorporate growth in the fibonacci series based on the hardcoded value of "N" so as not to draw through infinity. */ import java.awt.*; import javax.swing.JPanel; import javax.swing.JFrame; public class Spiral extends JPanel { public void paintComponent(Graphics g) { long w = getSize().width; long h = getSize().height; long wCen = w / 2; long hCen = h / 2; int N = 13; for (int j = 1; j <= N; j++) { int xPos = wCen + (20 * fib(j)); int xNeg = wCen - 20 - (20 * fib(j)); int yPos = hCen + 20 + (20 * fib(j)); int yNeg = hCen - 20 - (20 * fib(j)); System.out.println(j + ": " + fib(j)); g.drawLine(xPos, yNeg, xPos, yPos); g.setColor(Color.blue); g.drawLine(xPos, yPos, xNeg, yPos); g.setColor(Color.red); g.drawLine(xNeg, yPos, xNeg, yNeg); g.setColor(Color.green); g.drawLine(xNeg, yNeg, xPos, yNeg); g.setColor(Color.cyan); } } public static long fib(int n) { if (n <= 1) return n; else return fib(n-1) + fib(n-2); } public static void main(String[] args) { Spiral panel = new Spiral(); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(panel); frame.setSize(1000, 1000); frame.setVisible(true); } }
"The secret to getting what you want is to reject everything that you don't want." -Wolbers
- 04-07-2016, 05:51 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: Having trouble with primitive type 'long'
Yes you cast it like this.
long a = 20;
int b = (int)a;
In your case the entire right side is converted to a long so you should put it in () and cast the entire expression.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 04-07-2016, 09:20 PM #3
Re: Having trouble with primitive type 'long'
Thanks Jim! But when I do:
Java Code:int xPos = int(wCen + (20 * fib(j)));
Spiral.java:22: '.class' expected
int xPos = int(wCen + (20 * fib(j)));
Have I done something wrong?"The secret to getting what you want is to reject everything that you don't want." -Wolbers
- 04-07-2016, 09:57 PM #4
Re: Having trouble with primitive type 'long'
Oops, I misinterpreted your advice, Jim. This seems to work now, thanks again:
Java Code:/* Created by John Thrush and user3266259 (of stackOverflow) 4/06/2016 Spiral is an app that displays a rectangular spiral and should incorporate growth in the fibonacci series based on the hardcoded value of "N" so as not to draw through infinity. */ import java.awt.*; import javax.swing.JPanel; import javax.swing.JFrame; public class Spiral extends JPanel { public void paintComponent(Graphics g) { int w = getSize().width; int h = getSize().height; int wCen = w / 2; int hCen = h / 2; int N = 13; for (int j = 1; j <= N; j++) { int xPos = (int)(wCen + (20 * fib(j))); int xNeg = (int)(wCen - 20 - (20 * fib(j))); int yPos = (int)(hCen + 20 + (20 * fib(j))); int yNeg = (int)(hCen - 20 - (20 * fib(j))); System.out.println(j + ": " + fib(j)); g.drawLine(xPos, yNeg, xPos, yPos); g.setColor(Color.blue); g.drawLine(xPos, yPos, xNeg, yPos); g.setColor(Color.red); g.drawLine(xNeg, yPos, xNeg, yNeg); g.setColor(Color.green); g.drawLine(xNeg, yNeg, xPos, yNeg); g.setColor(Color.cyan); } } public static long fib(int n) { if (n <= 1) return n; else return fib(n-1) + fib(n-2); } public static void main(String[] args) { Spiral panel = new Spiral(); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(panel); frame.setSize(1000, 1000); frame.setVisible(true); } }
"The secret to getting what you want is to reject everything that you don't want." -Wolbers
- 04-08-2016, 10:31 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Re: Having trouble with primitive type 'long'
A simpler solution, since you are not using longs anyway, would be to get fib to return an int, unless it simply has to return a long for some reason?
Please do not ask for code as refusal often offends.
** This space for rent **
- 04-10-2016, 09:39 PM #6
Re: Having trouble with primitive type 'long'
"The secret to getting what you want is to reject everything that you don't want." -Wolbers
- 04-10-2016, 10:52 PM #7
Member
- Join Date
- Dec 2012
- Posts
- 28
- Rep Power
- 0
Re: Having trouble with primitive type 'long'
Hello
On left side of your equation
Java Code:int yNeg = hCen - 20 - (20 * fib(j));
- 04-11-2016, 08:18 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Similar Threads
-
syntax error on token '?', invalid primitive type
By passy in forum Advanced JavaReplies: 4Last Post: 09-25-2014, 02:07 PM -
Passing Primitive Data Type Arguments
By tariqm in forum New To JavaReplies: 6Last Post: 04-15-2012, 01:58 PM -
Cannot invoke toString() on the primitive type int
By ankiit in forum New To JavaReplies: 5Last Post: 04-02-2012, 03:26 PM -
Primitive data type and class
By Roselicious in forum New To JavaReplies: 3Last Post: 04-19-2010, 04:27 PM -
JNI accessing non primitive data type
By H_P in forum Advanced JavaReplies: 1Last Post: 04-14-2010, 06:43 AM
Bookmarks