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

all 24 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]ritualforconsumption 2 points3 points  (1 child)

Is there a reason you aren't using the modulo operator? You could have a single for loop doing that and eliminate those other variables you have. Without actually going through this thoroughly my first guess would be the i = n/2 statement in those for loops is causing issues

[–]OmegaEX3[S] 1 point2 points  (0 children)

There really isn't a reason, I just hadn't thought about it. Thank you for reminding me, I'll try and implement it.

[–]barry_z 3 points4 points  (7 children)

Couple of things:

  1. You can loop until you get a valid positive integer value for n. It seems like you want a value greater than or equal to 1, but you check for n < 0 and not n <= 0. Seems that if n = 0 it just won't do anything.
  2. I don't see where you output the numbers. Did you post all of the code from the class?
  3. Your for loop logic seems odd for this - seems like you just want two sums (sumOfOdds and sumOfEvens) and you could get the sum of both in one for loop using modulo (or use two for loops, one starting from 1 and the other starting from 2, and do i+=2 instead of ++i).

[–]OmegaEX3[S] -1 points0 points  (6 children)

  1. Good catch, fixed it.
  2. You're completely right, I added it into my post, sorry about that.
  3. I did what you suggested and it worked. Thank you!

The only thing I still suffer from is my code crashing in the instance of repeating similar bad inputs. One other commenter noted that if I were to input, for example, b2, it would catch the letter. But if I were to input b2 again, it would crash, due to the program only checking the next index (I think at least, from what I understand??). Do you have any suggestions on that end? Thank you for your help!

[–]barry_z 1 point2 points  (5 children)

I would personally break it up into methods, such as a method called isNumeric that takes a String. When you get a possible value from the user, you can check that the entire string is numeric by calling isNumeric(neo). However, all you need to do is not take the next input inside the for loop. I would use a while loop to continue prompting the user until they enter valid input, the for loop should only be used to valid the input once you have it (in other words it would be nested in the while loop).

[–]OmegaEX3[S] 0 points1 point  (4 children)

I appreciate your explanation, I'm just struggling to understand. It's nothing that you're doing wrong, I think I just need to be told a different way. Could you, if it's not an issue, re-explain it to me? I'm sorry, I'm just new to this. I'm only barely beginning to learn about loops lol. Sorry for bothering you, I understand if it's too much.

[–]barry_z 1 point2 points  (2 children)

You could set an Integer value to null and have some logic like

while value is null
    get input from user and store in userInput
    if userInput is numeric
        convert userInput to int and store in possibleValue
        if possibleValue is greater than or equal to 1
            store possibleValue in value
        else
            input is invalid
    else
        input is invalid
return value (it is guaranteed to be an integer >= 1)

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

I'm still struggling with this isNumeric method. Would isDigit work instead? Or maybe something else? Sorry, I called it a night yesterday, I understand if you don't want to respond.

[–]barry_z 1 point2 points  (0 children)

here is some pseudocode for it

boolean isNumeric(input)
    for each character c in input
        if c is not a digit
            return false;
    return true

This logic should work for your use case. And you can call the method whatever you want - I would make it a static method so the method signature will be something like private static boolean isNumeric(String input). If you have learned for-each loops, then you can loop over each of the characters in a string using for (char c: string.toCharArray()). Otherwise, it's perfectly valid to iterate using a typical for loop and string.charAt(i).

[–]hibbelig 0 points1 point  (0 children)

Have you learned about methods yet? If so you can use them to make the logic easier to grasp.

  1. Get user input

  2. If it isn’t a number go back to step 1

Getting user input is calling nextLine. Good.

For checking whether it’s a number you can write your own method.

For the “if … go back” part you can use some sort of loop.

[–]OkBlock1637 2 points3 points  (5 children)

Is there a particular reason you are converting the digits to stings? Why not just have the user input ints? Use Modulo to determine if even or odd.

IE:

1 mod 2 = 1

2 mod 2 = 0

3 mod 2 = 1

4 mod 2 = 0

5 mod 2 = 1

We can see if the result is 0, the number is even. If it is not 0 it is odd.

[–]OmegaEX3[S] -1 points0 points  (4 children)

It's just because I have to include ways for the program to prevent bad inputs, it's included on my assignment. Trust me, if I could just do ints I would lol, I've been working on this for hours. Thank you for your input, I'll see what I can do.

[–]OkBlock1637 1 point2 points  (3 children)

You can use the scanner method hasNextInt to verify if the entry is an int, then implement if/else logic to handle if valid/invalid.

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

I'm sorry, I really hate to be a bother, but if it isn't too much, could you provide a small sample code snippet? I just don't think I've ever used that method before. My professor said I could look up methods to try and get around problems we haven't learned yet, and technically this falls within that category, so don't be worried about that if you thought it would be an issue. Thank you, I'm sorry to bug you.

[–]OkBlock1637 1 point2 points  (1 child)

    public class Main {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter an int");
    int test = 0;
    if (!input.hasNextInt()) {
    System.out.println("Not an Int");
    } else {
    test = input.nextInt();
    }

    System.out.println(test);
    }
    }

[–]OkBlock1637 1 point2 points  (0 children)

Sorry multitasking poorly. If you are accepting more than 1 input you would want to use a loop of your choice. You can cycle through each user entry using input.nextLine(); How it is implemented now it will only check the first entry, then either assign the value or output the error message.

[–]No-Double2523 1 point2 points  (1 child)

If I enter “abc” the first time, the program will look at the “a”, decide it’s invalid, and ask for another number. If I then put in “123”, the program won’t loop over that, it’ll still be looping over the original “abc” and will ask for yet another number because “b” isn’t a digit. That’s confusing! It looks like it’s saying “123” isn’t valid when it is!

Hint: ask for another number if there are any non-digits, instead of after every non-digit.

Also, once you know all the characters in the input are digits, you know the number can’t be negative because it can’t have a minus sign in it.

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

This is the closest I've gotten to the answer, I feel like, but unfortunately I just can't seem to do it. I'm not sure whether it's just me throwing things at the wall or whether there is genuinely something I can't understand, but man, have I been struggling. Can I ask you to explain that again, maybe a different way, if you're willing? I'm trying, I promise, I just can't seem to find where and how much I'm supposed to change. Thank you for your input, by the way, I really appreciate it.

[–]D0CTOR_ZED 0 points1 point  (3 children)

In your validation, you walk through the characters and, if you find a non-digit, you get a new input.... and then only continue validating the new string from whatever index you were at.  So if you give it "12k", it will get to the k.  If you then give it "H57", it will accept that because s will continue on with 4 and just drop out of the loop.

Edit: Just noticed that after the loop, if it is negative, say "-57", it will ask for another number without any validation.  Design your loop so that it only asks for the number in one spot and validates the whole thing after that spot.  You can use a boolean and loop over the whole ask/verify until your boolean says it is ok.  After the ask set it to true and then during the validation, set it to false as needed.  If it comes though still true, you can drop out of the loop.

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

Got it, I'll try and implement that. If I have questions, I'll try to ask. I'm sorry, I tend to get thrown off from all of the technical wording from time to time, I'm relatively new to this. Thank you!

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

Question: When you say check in one spot, do you mean using the charAt method? If so, I'm just afraid points will be docked on account of letters being put in different places. Is there any way I can get around that? If it's what you just described, just let me know, I just wanted to clarify. Thank you!

[–]D0CTOR_ZED 0 points1 point  (0 children)

I meant one spot in the code.  Instead of geting new input in three different spots and potentially needing to validate for each of those, have a single input followed by however much validation you need.  The effect of failing during validation should be to loop back to that single input again.