aspose file tools*
The moose likes Beginning Java and the fly likes Range class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Range class" Watch "Range class" New topic
Author

Range class

Laurie carrera
Greenhorn

Joined: Jun 07, 2006
Posts: 14
Hi,
I am at a beginners level with java and I am a trying to get as much practice as possible.
I am writing a program which generates a random set of numbers in a loop that runs 1000 times. I would like to store this in an instance of the class Range.
Can some tell me how to create an instance of a Range that stores integers, with an unbound upperlimit. I would like the program to get the max value from this range.
I went through the java api on ranges and used the example in the docs. But the program gives an error message saying "cannot resolve symbol".

Any help would be apprecaited.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Please post the exact code you are using and the complete error message that you get when compiling. You need to be as specific as possible for us to help you since often paraphrasing loses important information that we need to understand what is going on.

Layne


Java API Documentation
The Java Tutorial
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Agree with Layne Lund, but there are certain circumstances which give rise to certain error messages.

Look at the error message, because it gives a hint where the error occurs, above the caret (^), although in a lot of cases I have found the real error occurs farther back in the program.

Find yourself a site with lists of error messages on: there's one here. Or look around for another, and when you find one you like, bookmark it on your internet browser.
"Cannot resolve symbol" has several meanings, which you can find on the URL I have just quoted.

See whether that helps at all. If you can't sort it out soon, post the text around where the error occurs.

CR

Oh, nearly forgot.
Welcome to the Ranch
[ June 07, 2006: Message edited by: Campbell Ritchie ]
Laurie carrera
Greenhorn

Joined: Jun 07, 2006
Posts: 14
Alright,
I am trying to fix the code by finding a workaround, since ranges are a new concept to me. But if you guys could point me to some range info that would be great.
Here's the code(half complete):
package tutorial;

import java.lang.Object;
import java.util.Arrays;


public class Maxdivisor {
public static void main(String [] args)
{
//Range rng = new Range(start,end);
int i;
double divis,rem;
int x;
//first loop
for (i=1;i<6;i++)
{System.out.print("Int is "+i+"\n");
int z=0;
//second loop
for (x=1;x<6;x++)
{
divis = i/x;
rem = i%x;
//if cond
if (rem==0)
{
z=z+1;
//System.out.print("Its divisors are "+x+"\n");
}

}
System.out.print("The total number of divisors are "+z+"\n");
rng[i]=z;

//end 2 loop
}
//end 1 loop

}
}
The line of code in bold is the where I initialize an instance of the range object. Now I understand the initialize may be totally wrong.when I run the code. The error msg is as follows:
init:

deps-jar:

Compiling 1 source file to C:\Documents and Settings\susmitha\Tutorial\build\classes

C:\Documents and Settings\susmitha\Tutorial\src\tutorial\Maxdivisor.java:12: cannot resolve symbol

symbol : class Range

location: class tutorial.Maxdivisor

Range rng = new Range(start,end);

C:\Documents and Settings\susmitha\Tutorial\src\tutorial\Maxdivisor.java:12: cannot resolve symbol

symbol : class Range

location: class tutorial.Maxdivisor

Range rng = new Range(start,end);

C:\Documents and Settings\susmitha\Tutorial\src\tutorial\Maxdivisor.java:12: cannot resolve symbol

symbol : variable start

location: class tutorial.Maxdivisor

Range rng = new Range(start,end);

C:\Documents and Settings\susmitha\Tutorial\src\tutorial\Maxdivisor.java:12: cannot resolve symbol

symbol : variable end

location: class tutorial.Maxdivisor

Range rng = new Range(start,end);

4 errors

BUILD FAILED (total time: 0 seconds)

The incomplete code so far divides ints 1-6 with ints 1-6, finds the calculations that result in a remainder =0 and counts the number of divisors for that particluar calculation.
The reason I was looking for a range is that, I would like to store the total no. of divisors for each int in a range and pick the highest number.
I still haven't looked at the error links in one of the replies.
Thx for the replies so far.
Laurie carrera
Greenhorn

Joined: Jun 07, 2006
Posts: 14
Bummer!!
The error links posted by Campbell are blocked by the firewall on my work lan. I will have to check them at home.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The error message is trying to tell you that there is no such class as Range. This is NOT a class from the standard Java API. So, either you need to find this class in your textbook or write it yourself. Either way, you need to create a file named Range.java with the appropriate code and compile it along with this Maxdivisor class.

Layne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
There is more than Range class going missing.

You hvae obviously come from C programming if you put the whole of your programming into the main method. Try to get it out of the main method. Let's imagine you put the action into a go method. You would write this.

Now, you declare a Range object rng. You pass arguments start and end, but you don't say where you get them from. You will need to declare them and give them values.
Then later you use it as if it were an arrayYou will have to decide whether it is an array or a Range object.

You have double rem and divis. Actually when you are dividing ints, they follow the strange rules of integer arithmetic:-
  • All results are integers.
  • No fractions. 7 / 3 comes to 2, not 2.333333333333333.
  • They have a remainder. No fractions. 7 % 3 comes to 1.
  • There is in Java no overflow exception. If you manage to get above the maximum value for your type of integer (for int 2^31 - 1), you go back to the most negative value (-2^31).
  • So your i/x will come out to an integer, not a double. You might be happy with an int; in which case declare divis and rem both as ints.

    The easiest way to force it into double format is to write this:-The 1.0 is a double number, which will "promote" the int and for the duration of that line they will be treated as if they were doubles.

    And as Layne Lund says, you will have to find or create a Range class.

    Edit: Have had to change i to j in the [] after rng, otherwise the UBB code misinterprets it as an italic tag.
    [ June 07, 2006: Message edited by: Campbell Ritchie ]
    Edwin Dalorzo
    Ranch Hand

    Joined: Dec 31, 2004
    Posts: 961
    There is a Range class in the Jakarta Commons Lang, maybe that is what your are missing.

    Is this declared in your imports?

    org.apache.commons.lang.math.Range
    Laurie carrera
    Greenhorn

    Joined: Jun 07, 2006
    Posts: 14
    Just so everyone knows....people who are still sending me replies....I solved the problem.

    I used a two dimensional array to store both the integer and its total number of divisors. The first dimension consisted of the divisors and the sec dimension the integers.I used a method to get the largest element in that array. The method checks for the largest element in the first dimension and then passes the value of the index of the largest element in the first dimension to the second dimension, which would automatically be the corresponding integer.

    If anyone needs the code, let me know. There may other ways of writing this program, but I am pretty happy with mine.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39436
        
      28
    I solved the problem.

    I used a two dimensional array


    Much simpler than an additional class. Well done.
    Laurie carrera
    Greenhorn

    Joined: Jun 07, 2006
    Posts: 14
    Thanks!
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39436
        
      28
    That's a pleasure
    Laurie carrera
    Greenhorn

    Joined: Jun 07, 2006
    Posts: 14
    Hi,
    This question is for Campbell ritchie. In your second reply to me in this thread, you mentioned writing code in a seperate method and then calling it in the main method, and the syntax to call that method is:
    new<class><method with code in it>.

    My questions are:
    What does the "new" signify?
    Why do we have we mention the name of the class, when the method we are calling is inside the very class?
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39436
        
      28
    Sorry, I didn't notice that last posting earlier.

    The keyword new is used as an operator; it creates an object as an instance of a class. This is how you create an instance of a class and give it a name:-
    Now you can invoke a method of that object/instance by putting a dot and the method name after the name of the object.

    Now, if you use an object once and once only, you can dispense with part of that sequence. You don't need to give the object a name/identifier.

    The following is an example of how you can use an objct without giving it a name. It is cribbed from the API for BufferedReader and shows the old-fashioned way of setting up file input.There is a FileReader object there, but you could miss it if you don't realise that new FileReader("foo.in") is its representation. Yo can't get at the FileReader object again because you haven't given it a name, but that doesn't matter becasue you don't need to use it again. This is sometimes called an anonymous object.

    What I wrote down in the main method was an anonymous object. That is in the form:-You put whatever arguments the constructor needs in the brackets; in this case no arguments are required. This example won't work because the statement doesn't finish. If however, you have a method which starts off whatever the object is supposed to do (I usually write "go()," then, you can put that method after the constructor invocation.

    Now, what happens is that a MyClass object is created, and stays in memory until the go() method finishes what it is supposed to do, then vanishes quietly.
    If you have enough functionality in your go() method, by the time this MyClass object vanishes, your application is finished.

    CR
    Laurie carrera
    Greenhorn

    Joined: Jun 07, 2006
    Posts: 14
    Thanks for the reply. That cleared up a lot of things. I have one more question.
    I have found that a lot of java examples use TextIO class, and I have noticed that is the only I/O class that reads char input (TextIO.getchar()).
    But this class is no where to be found in the API. I am using ver 1.5 of the JDK and 5.0 netbeans. Is there something I am missing? Also is there any other I/O function that reads char input?

    Sorry thats two questions.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39436
        
      28
    I think we are getting off the track here and you really ought to start another thread, but . . .


    You are right, there is no such thing as the TextIO class in the API. It must be a leftover from the good old days of J1.4 (or 1.3 or 1.2 or 1.1 or 1.0). In those days everybody who had been programming for more than a few months tended to write their own classes for keyboard-based input, and once they had them running OK, tended to reuse them. You end up with something like this:-
    Alternative readChar method:-
    As I said, everybody wrote their own, and TextIO is obviously what somebody wrote. For most things you can use the java.util.Scanner class, but it doesn't have the readChar method. You probably now have enough information to write your own keyboard input method now.

    Probably best to copy this posting to a new thread.

    CR
    Laurie carrera
    Greenhorn

    Joined: Jun 07, 2006
    Posts: 14
    Thnx. That will be the end of this thread.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Range class