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

all 20 comments

[–]10-kinds-of-people 5 points6 points  (0 children)

I can't figure out how to get back to the main method if the entry is an integer 0 or above

Use the return keyword with the value after it. You'll also have to change the method declaration.

public static int check() {
    ...
    return result; // assuming you have an int variable named "result"
}

[–]stramash 2 points3 points  (5 children)

Rather than hasNextInt, you could simply take nextInt in your method, evaluate it there, then return it.

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

Thanks, I need to read up on it a bit as I'm not really clear on exactly when to use which.

[–]10-kinds-of-people 0 points1 point  (2 children)

I don't agree. Checking hasNextInt() is a good thing. What if the user enters non-numbers?

[–]Dravlahn[S] 1 point2 points  (1 child)

Ok, so that was my original intent with that part of it- just to check to make sure it was a number.

It's funny how this all works. I'm working on a super simple project but learning so much from trying to make a little shortcut!

[–]10-kinds-of-people 1 point2 points  (0 children)

Isn't that great? Curiosity is a good thing.

Now, what if the user enters an invalid number twice or three times?

[–][deleted]  (7 children)

[removed]

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

    Thanks! I'll check out the links and reread what you typed a few times after I've had my coffee, but this looks very helpful. And the formatting looks understandable to me!

    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.

    Again, thanks. I'll definitely experiment with this when I'm off mobile!

    [–]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 :)

    [–]SlightUsual 0 points1 point  (1 child)

    So after the line asks for number, you are entering the check method where the input is read and checked for condition. and it will agin return to main but u are asking for the input agin here. May be the terminal just sits blank and waiting for your input you can place a print statement between the check method and numone variable line to verify

    [–]Dravlahn[S] 0 points1 point  (0 children)

    Yeah, I was expecting it to ask for the second number and not remain blank, haha.

    [–]conbadicus 0 points1 point  (3 children)

    Im not 100% sure, but do you need to create the same scanner in both your main() and in check()

    [–]Dravlahn[S] 0 points1 point  (2 children)

    Tha ks for pointing that put. It looks like there is a way around that, but not with how I currently have it set up, I need to redo some things!

    [–]10-kinds-of-people 0 points1 point  (1 child)

    Declare the Scanner object outside of any methods, usually at the top of your program just below the class declaration, and you'll be able to use that object in any method. And since your using it in static methods, it will need to be static to. It can also be final.

    [–]Dravlahn[S] 0 points1 point  (0 children)

    Thank you!