• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Variable Madness

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

I'm new here and a have a little (and probably very simple...but I've been trying to solve it for hours) problem.
We have to write a program, that picks the smallest number from a user input array and returns the array without it. My findMin method works and shows that smallest number when it's run seperately. But when I tried adding the removeElem method, it refused to compile stating that something was wrong with my variables. I was thinking about maybe adding a new empty int and then returning it but I don't know if I'm on the right trail.

Can you please tell me, what I am doing wrong? I'll have my first lab test in one week and am very scared of it because I still don't manage to write flawless programs even though I study a lot at home.

Here comes the code:



Best wishes,

Natalia Smith
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Natalia Smith wrote:But when I tried adding the removeElem method, it refused to compile stating that something was wrong with my variables.


When you ask a question like this, you should always include the exact message that was produced; however, in this case, I suspect it's telling you that it can't find 'k', because there is no field called k that is accessible by your removeElem() method.

My suggestion: add it as a 2nd parameter to your removeElem() method.

[I'm] very scared of it because I still don't manage to write flawless programs even though I study a lot at home...


I'd stick to 'correct' and 'good' if I were you - and by the looks of it, you're not starting off too badly. I've been doing this for 35 years and I've yet to write a flawless program (come pretty close a couple of times though).

Winston
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to Coderanch,

in the removeElement method where have you declared the variable k? in other words what is the scope of the variable k being used in the program?
 
Natalia Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your answers!
Now I've moved my variable declaration into removeElem and only get one error:

Less.java:43: cannot find symbol
symbol : variable smallest
location: class Less
int k = smallest;



I want k to be the index of the smallest number in the array so that I can remove it.

Here is my new code:

 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read the compiler errors carefully.

Now what is the scope of the smallest varaible? Also did you follow Winston's suggestion or passing the variable as a parameter to the method?
 
Natalia Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahhh...now I understand (I hope). Since English isn't my first language, it took me a while to figure out how scope translates. I think, the way my code is now, k is only accessible within the removeElem method since I moved it out of the public class. I tried to follow Winston's suggestion but understood it wrong and removed k instead of adding it. Is that what you mean?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Natalia Smith wrote:Is that what you mean?


Basically, instead of:
removeElem(zahlen);
you want:
removeElem(zahlen, k);

So, in order to do that, what else do you need to do?

Viel glück.

Winston
 
Natalia Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm probably missing a return statement in my second method and if you ask like this there should probably also be an "else" around. But I don't know it it would make sense in findMin and if I put it in removeElem, I still need an "if" there of which I don't know what to put in. Maybe something similar as in the "if" from findMin and then declare a new variable, do newVar = zahlen.remove (k); and then return newVar?

Vielen Dank
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i didn't read all of this post,sorry. maybe if i have time i will and reply again. the main problem(at least in your first code) is where you declared k. if you need to use a variable in more than one method declare it at the top as a member variable.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so your flow of code would be:


So how would your removeElem look then?


But your current method signature for removeElem indicates that you want to return some int value. What is that you want to return? Your method definition doesn't return any value.

And do you really need a method to removeElem? You can directly use the ArrayList API to remove the element.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Natalia Smith wrote:I'm probably missing a return statement in my second method and if you ask like this there should probably also be an "else" around. But I don't know it it would make sense in findMin and if I put it in removeElem, I still need an "if" there of which I don't know what to put in. Maybe something similar as in the "if" from findMin and then declare a new variable, do newVar = zahlen.remove (k); and then return newVar?


Basically, java.util.List provides you with two choices:
1. List.remove(Object) - which removes the first entry equal to 'Object' and returns a boolean (true if an entry was removed; false if not).
and
2. List.remove(int) - which removes the entry at index 'int', and returns its value.

The first method has no choice but to search through the whole List in order to find a matching entry; the second one may be able to locate the entry directly (if the List also implements RandomAccess - which ArrayList does). Therefore the second method is likely to be faster with an ArrayList.

In your case, the choice is slightly complicated by the fact that your elements are Integers, which get boxed and unboxed to ints; however, if you pass an int to remove(), the compiler will always call the most appropriate method, which is number 2.

So, if you want to use option 2, you need a method that returns the index of the smallest value in your list, maybe something like this:and in your main() method, you would do:and then, as Mohamed says, you don't really need your 'removeElem()' method.

You may notice that I used Integers when dealing with elements in the List. This isn't strictly necessary, but it stops a lot of boxing and unboxing, which does save time.
It also highlights whether you're dealing with an index or an element. Remember, your List contains Integers, not ints.

In addition, you'll also see that I made the parameter to the method a List, not an ArrayList. This is a general rule that's worth remembering:
  when dealing with collections, always use the interface, not the class, except when you're actually creating it.
Thus, the definition of 'zahlen' should be:
List<Integer> zahlen = new ArrayList<Integer>();

HIH

Winston
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using a static method for removing from the List, which should be an instance field? You should get all that code out of the main method, and reduce your main method to something very short, like thisNow, your local variables become instance fields, and you call the other methods from the go() method. It will look something like this:Since your List<Integer> is now an instance field, you do not need to pass it to the methods.
Why are you adding numbers into the List, and them immediately removing them?

Get a decent text editor, eg NotePad++ NotePad2, jEdit, gedit, kate. Set them up with automatic indentation, syntax and bracket highlighting, and changing tabs to 4 spaces automatically. That will make coding easier, and will also improve your indentation.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . . Basically, java.util.List provides you with two choices:
1. List.remove(Object) - which removes the first entry equal to 'Object' and returns a boolean (true if an entry was removed; false if not).
and
2. List.remove(int) - which removes the entry at index 'int', and returns its value. . . .
if you pass an int to remove(), the compiler will always call the most appropriate method, which is number 2.
. . .

You should note WG’s point, which may explain apparent errors in your code.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell is right. your main() method should do very little. mainly just create an instance of your class. all that other code you have in there belongs elsewhere.
 
Natalia Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for all the explanations!
Now I finally understood how it works. I hope that they will give us similar exercises in the lab test as we had in the normal lab.
reply
    Bookmark Topic Watch Topic
  • New Topic