This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes what's wrong with this code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what Watch "what New topic
Author

what's wrong with this code?

Ameer Alqallaf
Greenhorn

Joined: May 24, 2011
Posts: 6
Hi,
I've been trying to write my first code ever..
I think I somehow managed to do it but there is a compiler error and I can't figure out what it is.
The program suppose to ask the user for 5 numers and then it print the maximum value and the minumum value.
Here is what I wrote:




and here is the error I get
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.util.Scanner.netxtInt
Enter five numbers
at firstA.FirstA.main(First1.java:11)

the wrrors are in these lines
a = keyboard.netxtInt ();
b = keyboard.netxtInt ();
c = keyboard.netxtInt ();
d = keyboard.netxtInt ();
e = keyboard.netxtInt ();

PS. I know there maybe another way to do it but I want to do it this way, and please exuse the mess because I am totaly new to this forum.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Hi Ameer. Welcome to The Ranch!

Check your spelling of "netxtInt".

Edit: A more general comment - I'd suggest using an array of ints instead of five separate variables, as you'll be able to replace your complicated else/if statements with a loop and a simple test. Imagine what happens if you're now asked "that's great, but can you modify it to take 6/7/8 numbers instead?". With an array, that would be a matter of changing one number. With your approach, you've got a lot of changes to make.
Ameer Alqallaf
Greenhorn

Joined: May 24, 2011
Posts: 6
Thank you very much for you fast replay.

it was really a silly mistake
I'm sure it is because I didn't sleep unitl now trying to write this code

thanks for your comment , you are write about the arry thing , and that is why I mentioned in my post that I know there are other methods ut I was asked to do it this way without arrys as I am totaly beggener to programing and I need to learn it step y step :-)

thank you once more and have a good day
Joe Hepp
Greenhorn

Joined: Feb 07, 2005
Posts: 12
What happens in your code if your highest or lowest values are entered more than once?

Enter 5,1,2,1,5 for example.

Something to think about.
Ameer Alqallaf
Greenhorn

Joined: May 24, 2011
Posts: 6
I've tried that. It gave me the max and min = 0 !
is there a way ti fix that?!

thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

I'd start with using >= instead of >. If "a" > "b" then "a" is the max of "a" and "b". But if "a" >= "b" then "a" is still the max of "a" and "b". It's just that "b" might also be the max in the case when "a" == "b".

The easy way is of course to use nested calls to Math.max / Math.min:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Pranav Raulkar
Ranch Hand

Joined: Apr 20, 2011
Posts: 73

Hi Ameer,
The solution is very easy, replace every < by <= and > by >= and you will get the correct results for repeating elements.
Ameer Alqallaf
Greenhorn

Joined: May 24, 2011
Posts: 6
Wow, you guys are very amazing and fast )

Thanks for help, now I'm trying to change the code using JOptionpane to get the numbers and show the result in visual box instead of entering it in the counsil
and I'm stuck here



any help what to do next ?
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

The user's response is returned from the JOptionPane.showInputDialog() call. You just need to assign it to a variable.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

And convert it from a String into an int. java.lang.Integer has some static methods to help you out.
Ameer Alqallaf
Greenhorn

Joined: May 24, 2011
Posts: 6
here is my final project


Is there any modifications you suggest me to do with it ?
Rob thanks for the advice about using math.max it would really make it a lot easier but as I said I'm trying to learn java step by step and I would defenetly use your advice in the future
Bill foster
Greenhorn

Joined: May 17, 2011
Posts: 13


Hello friend,

I worked on the code that was submitted to the forum the first time.
I was able to get this running I did a test case using numbers in this sequence1,2,3,4,5.

Result was Max was 5 Min was 1

Then I reverse the order of the sequence 5,4,3,2,1.

Result was Max was 5 Min was 1.

The last entry I noticed you had two sets of decision blocks. You might want to try using the decision block in the code with the scanner 9from the first version of your code) with the JOptionPane one (the last update to your code) and see if it works. Your algorithm(if/else statements) worked in the first version of your code. See if it works.

You might want to test the code using numbers like 10, 3, 30, 0, and 5 to test the way the program handles out of sequence numbers. Here's the code:



Try to trace the process using paper and pencil then when you think you have a understanding then compile and see if you were right. This will make you think like a compiler and you can start reading code. Note I commented out the code at the beginning of the program. Check out the comments I gave you some pointers.



Your friend,

Bill Foster
Bill foster
Greenhorn

Joined: May 17, 2011
Posts: 13

Hi again,

I'm sorry I read the posts and noticed that testing with all numbers of the same value will give you 0.
You might try to check the If/Else statements, I did not catch that.

Bill Foster.
Bill foster
Greenhorn

Joined: May 17, 2011
Posts: 13

Hi friend,

Here is the update code for the alagorithm:




If you have 1,1,1,1,1. then result is Max = 1 and Min = 1 in words the same.
this passes the test cases for multiply same values. i.e. 10,10,20,30,50. Result is Min=10 and Max is 50.

That was a brain teaser


your friend,

Bill Foster
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

There are simpler ways of finding the max and min of a list of numbers. Perhaps you should consider storing a-e in a structure so you can utilize those.

Hunter
Bill foster
Greenhorn

Joined: May 17, 2011
Posts: 13



My fellowRanchPeeps,

I can do a int type array[] and then use a for {} loop to process the algorithm.

I'll see if I can do that, after some sleep

But if a programmer does not know how to use a container or a for loop the above post will do fine, there is more than one way to skin a cat.

That was a niffy trick with the JOptionPane (Using swing package--forgot about that, should look at the API for JOptionPane).

Right now I'm working with Assignment, Increment and decrement of variables. Doing some self study.

Good Night!

Your friend,

Bill Foster
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what's wrong with this code?
 
Similar Threads
Largest and smallest number
Need help with Incompatible type error
Scanner
Why does my 'd' variable stay at zero?
Initializing variables