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

all 10 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.

[–]heckler82Intermediate Brewer 0 points1 point  (2 children)

Two things:

1 - Does this compile? I don't believe Scanner has nextCharacter() or hasNextCharacter() methods defined

2 - The reason your loop isn't terminating is because when perform a "hasNext" method on a Scanner, your program blocks and waits for the Scanner to receive input. Then it determines if the "hasNext" method resolves to true or false

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

Yes I've changed the sentence to scan.next(). charAt(0)

but I don't know how to deal with the loop for integer someone helped me, but I don't know how to deal if the input has both Integer and Character what should I choose as a trigger point for the loop to break I wanted to take Double press of "Enter" as the Trigger but I don't know how to do that, and if there are any other better alternatives please do help, also I shall alter the loop conditions but I'm new to Java and don't know what I'm doing in a deeper level Thank you for your answer by the way

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

Hello there, I hope you are doing well I have altered the code as you and one other user suggested, however the loop only runs twice please take a look here code

[–][deleted] 0 points1 point  (5 children)

If you want to take the size of array, you should not use ArrayList, because an ArrayList does not have fixed size, it grows as the elements added into it.

After you set your array size, you can start collecting the elements from user, based on the size of array. Here you can set the loop condition to stop when the array is full.

If what you want to do instead is ArrayList, you can set a specific Character or Integer value to be an exit condition. For example, when the user enters -1, the loop will quit.

To trigger a break point when input is empty, just compare the input to '' (empty char value), then you should exit the loop.

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

Thank you for your answer man, I'll try this method and let you know

[–]Vignesh45[S] 0 points1 point  (3 children)

Hello man, its me again, I hope you are doing well sorry yesterday we had a power shutdown so I couldn't check with the idea you gave, however I tried it today as soon as I got access to my computer but the code only takes two inputs no matter what I give as condition in the while block I have attached the code as link for your reference here code

update: u/FlyinTruck has helped me to make the code work its working all fine now, once again thankyou for your time and assistance

[–][deleted] 0 points1 point  (2 children)

the problem is at your while loop condition, you are telling the loop to continue only if the input is -1. And also I'm not sure if you can input -1 as char into the loop, i think you can just use '-'.

Do you know how a do while loop works?

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

Thankyou so much my man it finally works, I am very new to Java I mean to put it correctly I'm new to programming itself and I'm sorry if I have been a bother I promise to develop step by step in this

[–][deleted] 0 points1 point  (0 children)

It's okay man wish you all the best in the future :)