Problem with variables and arrays of arrays.

Put your problem here if it does not fit any of the other categories.

Problem with variables and arrays of arrays.

Postby Nomad0404 » Mon Mar 07, 2011 10:29 pm

So my code is below it's just me playing with Android for the moment trying to work out how to do things with the canvas. However, I've come across a problem I can't work out.

The line in question is:

int highest = UKX[0][1]; // start with the first value

Brings up a syntax error on token.

It works fine as a stand alone in java so what am I doing wrong in Android???????

Thanks in advance.

Phil

Code: Select all
package com.dummies.android.grid;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.Paint.Style;
import android.graphics.DashPathEffect;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.Window;

public class MainActivity extends Activity
{
   DemoView demoview;
      
      int[][] UKX =  {{591566,601708,591566,600509},
            {593573,594182,586592,591489},
            {600088,602233,596358,599401},
            {592071,601150,592071,600120},
            {592292,593668,586095,591998},
            {599672,599672,591552,592353},
            {601442,602856,592655,599676},
            {606747,610577,601314,601480},
            {609190,609678,604883,608299}, 
            {609659,610142,606671,608738},
            {604655,609576,604238,608527},
            {605158,607207,602377,603708},
            {607479,609148,604210,606009}, 
            {600472,607140,597344,606290},
            {603668,604520,598648,602001}, 
            {606743,608430,604950,605229},
            {604232,609133,603288,609133},
            {600916,605409,599716,605103},
            {600264,602341,598683,599738},
            {598570,599359,595182,598334},
            {597654,602046,597271,600007},
            {592071,601150,592071,600120}};
   
   int highest = UKX[0][1]; // start with the first value      
   for (int i=1; i<UKX.length; i++)
   {
       if (UKX[i][1] > highest)
       {
           highest = UKX[i][1];   //new maximum
       }
   }
   int highest = 610577;
   int lowest = 586095;
   double range = (highest-lowest)*1.1;
   
   float width = 0;
   float height = 0;
   float previous = 0;
   float current = 0;
   float stepX = 0;
   float stepY = 0;
   float stepPercent = 0;
   int multiY = 10;
   int multiX = 22;
   
   float topLeftX = 0;
   float topLeftY = 0;
   float bottomRightX = 0;
   float bottomRightY = 0;
   float graphHeight = 0;
   float graphWidth = 0;
   double graphLow = 0;
   double graphRange = 0;
      
   /** Called when the activity is first created. */
   @Override public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      // No Title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
       
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        //WindowManager.LayoutParams.FLAG_FULLSCREEN);
      demoview = new DemoView(this);
      setContentView(demoview);
   }
      
   private class DemoView extends View
   {
      public DemoView(Context context)
      {
         super(context);
      }

      @Override
      protected void onDraw(Canvas canvas)
      {
         super.onDraw(canvas);

         // custom drawing code here
         // remember: y increases from top to bottom
         // x increases from left to right

         Paint paint = new Paint();
         paint.setStyle(Paint.Style.FILL);

         // make the entire canvas white
         paint.setColor(Color.TRANSPARENT);
         canvas.drawPaint(paint);

         Display display = getWindowManager().getDefaultDisplay();
         width = display.getWidth();
         height = display.getHeight();
               
         topLeftX = (float)(width*0.05);
         topLeftY = (float)(height*0.05);
         
         bottomRightX = (float)(width*0.95);
         bottomRightY = (float)(height*0.55);
   
         graphWidth = bottomRightX-topLeftX;
         graphHeight = bottomRightY-topLeftY;
         
         graphRange = range/graphHeight;
         graphLow = lowest*0.95;
         
         paint.setStyle(Style.STROKE);
         
         //stepX = space between vertical bars
         stepX = graphWidth/multiX;
         //stepY = space between horizontal bars
         stepY = graphHeight/multiY;
         //step = 1 hundredth of the total height of the box.
         stepPercent = (graphWidth)/100;
         
         //draw the outline box
         paint.setColor(Color.WHITE);
         canvas.drawRect(topLeftX, topLeftY, bottomRightX, bottomRightY, paint);
               
         //draw horizontal lines
         for (int i = 1; i < multiY; i++)
         {//canvas.drawLine(startX, startY, stopX, stopY, paint)
            paint.setColor(Color.rgb(255,0,0));
            canvas.drawLine(topLeftX, topLeftY+(stepY*i), bottomRightX, topLeftY+(stepY*i), paint);
         }
         
         //draw vertical lines and diagonal random price lines
         for (int i = 1; i < multiX; i++)
         {//canvas.drawLine(startX, startY, stopX, stopY, paint)
            paint.setColor(Color.rgb(255,255,255));
            paint.setStrokeWidth(1);
            canvas.drawLine(topLeftX+(stepX*i), topLeftY, topLeftX+(stepX*i), bottomRightY, paint);
         }
         /*
         //draw close line graph
         for (int i = 1; i < multiX+1; i++)
         {
            
            DashPathEffect dashPath = new DashPathEffect(new float[]{10,10}, 1);
            paint.setPathEffect(dashPath);
            
            previous = (float)((UKX[i-1][3]-graphLow)*graphRange);            
            current = (float)((UKX[i][3]-graphLow)*graphRange);
            System.out.println("previous: " + previous);
              System.out.println("current: " + current);
            if(previous>current)
            {
               paint.setColor(Color.rgb(255,0,0));
               paint.setStrokeWidth(2);
            }
            else
            {
               paint.setColor(Color.rgb(0,255,0));
               paint.setStrokeWidth(2);
            }
            //canvas.drawLine(startX, startY, stopX, stopY, paint)
            
            canvas.drawLine(topLeftX+(stepX*(i-1)),previous, topLeftX+(stepX*i), current, paint);
         }
         */
      }
   }
}
Nomad0404
Junior Developer
Junior Developer
 
Posts: 19
Joined: Mon Mar 07, 2011 10:23 pm

Top

Re: Problem with variables and arrays of arrays.

Postby raj_J2ME » Tue Mar 08, 2011 7:07 am

You have declare the highest variable two times, try to rename either of them... :)
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: Problem with variables and arrays of arrays.

Postby Nomad0404 » Tue Mar 08, 2011 11:34 am

That's not the problem, although I noticed I hadn't deleted the second declaration after I had posted this.

I added the second declaration to get the rest of it to work. At the time I had the highest loop commented out.

So I'm still officially stumped as to why I'm getting the error though.
Nomad0404
Junior Developer
Junior Developer
 
Posts: 19
Joined: Mon Mar 07, 2011 10:23 pm

Re: Problem with variables and arrays of arrays.

Postby raj_J2ME » Tue Mar 08, 2011 11:39 am

Nomad0404 wrote:That's not the problem, although I noticed I hadn't deleted the second declaration after I had posted this.

I added the second declaration to get the rest of it to work. At the time I had the highest loop commented out.

So I'm still officially stumped as to why I'm getting the error though.

So what is the error you are getting right away?
Could you share the much details about the errors and code?
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: Problem with variables and arrays of arrays.

Postby Nomad0404 » Tue Mar 08, 2011 12:25 pm

Actually further investigation appears to indicate that the issue is with my array of arrays not with the int highest declaration at all.

This in Java runs fine:

Code: Select all
class Highest
{
   public static void main(String[]args)
   {
      int[][] UKX =  {{591566,601708,591566,600509},
                    {593573,594182,586592,591489},
                    {600088,602233,596358,599401},
                    {592071,601150,592071,600120},
                    {592292,593668,586095,591998},
                    {599672,599672,591552,592353},
                    {601442,602856,592655,599676},
                    {606747,610577,601314,601480},
                    {609190,609678,604883,608299},
                    {609659,610142,606671,608738},
                    {604655,609576,604238,608527},
                    {605158,607207,602377,603708},
                    {607479,609148,604210,606009},
                    {600472,607140,597344,606290},
                    {603668,604520,598648,602001},
                    {606743,608430,604950,605229},
                    {604232,609133,603288,609133},
                    {600916,605409,599716,605103},
                    {600264,602341,598683,599738},
                    {598570,599359,595182,598334},
                    {597654,602046,597271,600007},
                    {592071,601150,592071,600120}};

      int highest = UKX[0][1];
      // start with the first value     
      for (int i=1; i<UKX.length; i++)
      {
         if (UKX[i][1] > highest)
         {
            highest = UKX[i][1];   //new maximum
         }
      }
      System.out.println(highest);
   }
}


In the console I see 610577 as expected.

In my Android MainActivity I get the token error. If I delete the int highest = UKX[0][1]; line then the token error steps back to the end of the array declaration.
Nomad0404
Junior Developer
Junior Developer
 
Posts: 19
Joined: Mon Mar 07, 2011 10:23 pm

Re: Problem with variables and arrays of arrays.

Postby Nomad0404 » Tue Mar 08, 2011 12:33 pm

The code is as originally posted just without the second Int Highest declaration.

Image

That error code is directly over the line:

int highest = UKX[0][1];

Does that help?

Phil
Nomad0404
Junior Developer
Junior Developer
 
Posts: 19
Joined: Mon Mar 07, 2011 10:23 pm

Top

Re: Problem with variables and arrays of arrays.

Postby raj_J2ME » Tue Mar 08, 2011 12:59 pm

Nomad0404 wrote:The code is as originally posted just without the second Int Highest declaration.

Image

That error code is directly over the line:

int highest = UKX[0][1];

Does that help?

Phil

I think that I catch the cause to the error... :)

When you are referring to the code which is inside the Core Java's, main() method this runs fine as the for loop in inside the main() method. However in your activity's code you are putting the for loop in the class definition but not in any method, this for loop must be the part of any method which is not right away in your activity class.

try to change the same and revert.
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: Problem with variables and arrays of arrays.

Postby Nomad0404 » Thu Mar 10, 2011 10:16 am

You were right, thanks for the help.

Phil
Nomad0404
Junior Developer
Junior Developer
 
Posts: 19
Joined: Mon Mar 07, 2011 10:23 pm

Re: Problem with variables and arrays of arrays.

Postby ismail12 » Thu Mar 10, 2011 10:48 am

Hello,

array of array not declare for all the time.so don't use array of array each time.
ismail12
Freshman
Freshman
 
Posts: 3
Joined: Thu Mar 10, 2011 10:24 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google Feedfetcher and 20 guests