Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what's wrong with this code?

 
Ameer Alqallaf
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried that. It gave me the max and min = 0 !
is there a way ti fix that?!

thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Pranav Raulkar
Ranch Hand
Posts: 73
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The user's response is returned from the JOptionPane.showInputDialog() call. You just need to assign it to a variable.

Hunter
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And convert it from a String into an int. java.lang.Integer has some static methods to help you out.
 
Ameer Alqallaf
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
Java Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 22
Java Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
Java Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
Java Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic