I'm creating a class with Java that has a function similar to Python's Range, but I keep getting Cannot find symbol errors.

Asked 2 years ago, Updated 2 years ago, 110 views

this.Error:cannot find symbol occurs on the line with the variable name. It's my first time using Java, so I don't understand it well, but isn't this able to access variables in objects like Python's self? In this case, I thought I should use super because it is a class in the range, but there is still an error. The error is private class RangeIterator implementationsIterator<Integer>.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.zip.DataFormatException;
import java.util.NoSuchElementException;

//import sun.font.TrueTypeFont;

/**
 * * Simulates python's range function
 */

public class Range implements Iterable<Integer> {
    // // you probably need some variables here
    private int start;
    private int stop;
    private int diff;
    public Range(int start, int stop, int diff) {
        this.diff = diff;
        this.start = start;
        this.stop = stop;
        if (diff == 0)
        {
            throw new DataFormatException("range() arg 3 must not be zero");
        }
    }
    public Range(int start, int stop) {
        this(start, stop, 1);
    }
    public Range(int stop)
    {
        this(0, stop, 1);
    }

    private class RangeIterator implements Iterator<Integer>
    {
        private int value = this.start;

        public boolean hasNext()
        {
            if(this.stop >= this.value)
            {
                if ((this.stop - this.value) >= this.diff) return true;
                else return false; 
            }
            else
            {
                if((this.start - this.value) <= this.diff) return true;
                else return false;
            }
        }

        public Integer next()
        {
            if (hasNext())
            {
                try
                {
                    return this.value;
                }
                finally
                {
                    this.value += this.diff;
                }
            }
            else
            {
                throw new NoSuchElementException("Iteration exceeded.");
            }
        }
    }

    public java.util.Iterator<Integer> iterator()
    {
        return new RangeIterator();
    }

    /*
    * * Test the Range class
    */
    public static void main(String[] args)
    {
        System.out.println("One:");
        for(Integer j : new Range(1,8,1))
        {
            System.out.print(j);
        }
        // 1234567

        System.out.println("\nTwo:");
        for(Integer j : new Range(1,8,2))
        {
            System.out.print(j);
        }
        // 1357

        System.out.println("\nThree:");
        for(Integer j : new Range(1,8))
        {
            System.out.print(j);
        }
        // 1234567

        System.out.println("\nFour:");
        for(Integer j : new Range(8,0,-1))
        {
            System.out.print(j);
        }
        // 87654321

        System.out.println("\nFive:");
        for(Integer j : new Range(8,8,1))
        {
            System.out.print(j);
        }
        // // prints error msg

        System.out.println("\nSix:");
        for(Integer j : new Range(8,10,0))
        {
            System.out.print(j);
        }
        // // prints error msg
    }
}

java class cannot-find-symbol

2022-09-22 13:50

1 Answers

The first this points to the object itself, just like Python self.

To add a little bit more, most object-oriented languages receive objects from that method as the first parameter. Except, of course, if it's declared static.

I think Python's self should also have not explicitly dealt with variables such as self if the language was well designed. Python may be easy to use in terms of language design, but it also seems to be lacking.

In the description, the error below indicates that this is an object in the RangeIterator. But the questioner wants to approach the start of the Range. Of course, this is not accessible. There is also an access restrictor called private that Python does not have. This access restrictor means that it is only accessible in that class. It's the same meaning as the variable name in Python.

private class RangeIterator implements Iterator<Integer>
{
        private int value = this.start;

I think you understood the cause of the error. The workaround is to re-design the class.

Within the Range class, start, stop, and diff are declared static and class name.You can also access the variable name, and you can use the constructor as shown below.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.zip.DataFormatException;
import java.util.NoSuchElementException;

//import sun.font.TrueTypeFont;

/**
 * * Simulates python's range function
 */

public class Range implements Iterable<Integer> {
    // // you probably need some variables here
    private int start;
    private int stop;
    private int diff;
    public Range(int start, int stop, int diff) throws DataFormatException {
        this.diff = diff;
        this.start = start;
        this.stop = stop;
        if (diff == 0)
        {
            throw new DataFormatException("range() arg 3 must not be zero");
        }
    }
    public Range(int start, int stop) throws DataFormatException
    {
        this(start, stop, 1);
    }
    public Range(int stop) throws DataFormatException
    {
        this(0, stop, 1);
    }

    private class RangeIterator implements Iterator<Integer>
    {
        private int start;
        private int stop;
        private int diff;

        public RangeIterator(int start, int stop, int diff) {
            this.diff = diff;
            this.start = start;
            this.stop = stop;
        }
        private int value = this.start;

        public boolean hasNext()
        {
            if(this.stop >= this.value)
            {
                if ((this.stop - this.value) >= this.diff) return true;
                else return false;
            }
            else
            {
                if((this.start - this.value) <= this.diff) return true;
                else return false;
            }
        }

        public Integer next()
        {
            if (hasNext())
            {
                try
                {
                    return this.value;
                }
                finally
                {
                    this.value += this.diff;
                }
            }
            else
            {
                throw new NoSuchElementException("Iteration exceeded.");
            }
        }
    }

    public java.util.Iterator<Integer> iterator()
    {
        return new RangeIterator(start, stop, diff);
    }

    /*
     * * Test the Range class
     */
    public static void main(String[] args) throws DataFormatException {
        System.out.println("One:");
        for(Integer j : new Range(1,8,1))
        {
            System.out.print(j);
        }
        // 1234567

        System.out.println("\nTwo:");
        for(Integer j : new Range(1,8,2))
        {
            System.out.print(j);
        }
        // 1357

        System.out.println("\nThree:");
        for(Integer j : new Range(1,8))
        {
            System.out.print(j);
        }
        // 1234567

        System.out.println("\nFour:");
        for(Integer j : new Range(8,0,-1))
        {
            System.out.print(j);
        }
        // 87654321

        System.out.println("\nFive:");
        for(Integer j : new Range(8,8,1))
        {
            System.out.print(j);
        }
        // // prints error msg

        System.out.println("\nSix:");
        for(Integer j : new Range(8,10,0))
        {
            System.out.print(j);
        }
        // // prints error msg
    }
}


2022-09-22 13:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.