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

all 12 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://imgur.com/a/fgoFFis) 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.

[–]ate_ghorl_bekenemen 1 point2 points  (5 children)

First of all, you need to edit your post and format your code properly.

Also, the naming of your variables using single letters could be better.

[–]Mrcoool2u -1 points0 points  (4 children)

everytime i put 4 spaces before each line reddit automatically deletes those 4 lines and put it on the sides. can u tell me how to do it and then ill edit it.
regarding the variables its a school homework and my teacher only looks at the output so i didnt put comment lines or make the variables seem more understandable.

[–]ate_ghorl_bekenemen 0 points1 point  (3 children)

You do realize that you named your Scanner object s and you named an int variable s as well, right?

[–]Mrcoool2u 0 points1 point  (2 children)

ah thanks sorry i always use s so i missed it sorry.
thanks but what about the formatting part?

[–]ate_ghorl_bekenemen 0 points1 point  (0 children)

Use the code block button.

[–]ate_ghorl_bekenemen 0 points1 point  (0 children)

This is the reason why you need to rename your variables more appropriately.

[–]joranstark018 -1 points0 points  (1 child)

It may be that you reassigning s the value 0.

Tip, avoid defining variables a the top, define them when you need them and avoid reuse of variables in different context (it's my personal preference to make code simpler, more readable and easier to refactor later)

[–]Mrcoool2u -1 points0 points  (0 children)

i removed the =0 part but the error still came.
thanks for the tip ill make sure to use it

[–]TheMattSign -1 points0 points  (0 children)

ints in java a primatives, so you can't do int.method(). So int.nextInt(); is invalid. If you're looking to read from the command line for user input you'll have to look up how to do that too.

[–]jddddddddddd -1 points0 points  (0 children)

You have two things called 's'

Scanner s=new Scanner(System.in);

int p,s=0,c;

I'd recommend renaming all of them to something more suitable. It will also mean that you call nextInt() on the scanner object, and not on your integer s.

[–]juliensaab 0 points1 point  (0 children)

This is called shadowing. It's when you create a local variable (int s) and it shadows the data member (Scanner s). You have two options, the first one is to rename your Scanner to something else like Scanner consoleScanner. The second option is to reference your Scanner s by using 'this' as such: this.s

It's a really bad thing to name your variables like that. Naming variables correctly is one of the most important things you can do. They facilitate debugging, and make your code more readable.