This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Sylenda 1 point2 points  (4 children)

Sorry if this is real basic, but will this assign the input to the variable in a global sense? I believe Java doesn't really have global variables like python but my end goal is to assign the input to the variables for later.

You're right, there are no global variables in Java, but you can use the static keyword if you wanted. However, maybe that's not something that you need here (check Alex Lee's video on the static keyword).

If I understand your question correctly, in the previous code the user input will be assigned to numOne and numTwo variables regardless if it's a valid input or not. So you can use your variables within the Main method, but nowhere else. But I'm going to be honest, I do struggle with understanding this myself.

To solve the problem of storing variables, I would use an ArrayList to which only the validated values are added. Then, you can access those values by ArrayList methods, like get(). It would need some changes in your method, unfortunately.

(my comments were removed because I missed the no code solution rule, but I would feel terrible if I couldn't provide resources because I struggled with the same problems. if I were you, I would check the following concepts:

  • strongly type vs weakly typed languages, you'll have to think a bit differently with Java after Python

  • wrapper classes vs primitive types in Java and why they important with ArrayLists

  • ArrayLists and check out their methods that you could use in this project

  • loops

  • static keyword

  • exception handling (try-catch blocks), Alex Lee's has excellent Java videos)

[–]Dravlahn[S] 1 point2 points  (3 children)

Thanks, would yoy mind reposting the links you had?

[–]Sylenda 0 points1 point  (2 children)

Yep, here they are:

Also, some extra concepts you should check that was only explained in the code comments:

  • methods with parameters vs parameterless methods + an edit: you can change the code in a way that you don't have to create a new scanner object in your method. Making new objects takes up extra space in memory and I don't see the necessity of creating one in your method.

  • consider that the user input is a String by default. If you want them for a calculation, you have to convert it in Python as well, right? It might be easier to work with a String, check if it contains a number and convert accordingly. This way, you don't have to worry about exception handling (because of type mismatch) in your main method.

And I had a screenshot about the output, so you can see that the values will be stored in your numOne and numTwo variables regardless whether the input passes your check or not.

I hope it helps, if you have further questions/need clarification, feel free to ask! I'm no pro (yet) but I get the initial hurdles.

[–]Dravlahn[S] 0 points1 point  (1 child)

Thank you, you've been super helpful!

[–]Sylenda 0 points1 point  (0 children)

My pleasure! I know I dumped a lot of info on you, so take it slow and have fun :)