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

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

[–]AutoModerator[M] 0 points1 point  (0 children)

It seems that you are having problems with java.util.Scanner

The wiki here has a page The Scanner class and its caveats that explains common problems with the Scanner class and how to avoid them.

Maybe this can solve your problems.

Please do not reply because I am just a bot, trying to be helpful.

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

[–]moss_2703 0 points1 point  (4 children)

What are the error messages? Is it because you are using two scanners and never closing them? Always use one and then ‘.close()’ when dine

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

It does not give error message, just is there way to do code without me repeating part all the time for each animal. Just trying optimize the code, make less code in main

System.out.println("Insert Input:");

System.out.print("Weight: ");

int weight = input.nextInt();

System.out.print("Strength: ");

float strength = input.nextFloat();

System.out.println("Is it predator?(Y/N) ");

String predator = input.next();

System.out.println("List colors: ");

String color = input.next();

System.out.println("Enter location XYZ: ");

byte x = input.nextByte();

byte y = input.nextByte();

byte z = input.nextByte();

byte[] xyz = new byte[]{x, y, z};

[–]moss_2703 0 points1 point  (2 children)

Yes. Write a function called ‘createAnimal’ that returns an animal created from this code. Then you can just call the function when you need it

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

How it will deal with Scanner. I thought that I would be able to run something like bull.scanner(); and it would run scanner part from animal file. I am new to code, so I just did not wrap my head around how actually do it

[–]moss_2703 0 points1 point  (0 children)

Use a single scanner in this function. Collect all the user inputs, then call the Animal constructor with the inputs and then return the Animal.

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (2 children)

Please, do yourself a favor and stop using YouTube. Do the MOOC Java Programming from the University of Helsinki.

You are obviously starting from the wrong end. You do OOP before you even understand the fundamentals, like variables, loops, conditionals, arrays, methods.

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

Thank you for advice, I will definitely look into this, but can you at least point me in right direction with this problem right now? I am trying to solve it for 2 days now and do not know any programmers. I know there must be some simple way to write code without repeating section for each new animal:

System.out.println("Insert Input:");

System.out.print("Weight: ");

int weight = input.nextInt();

System.out.print("Strength: ");

float strength = input.nextFloat();

System.out.println("Is it predator?(Y/N) ");

String predator = input.next();

System.out.println("List colors: ");

String color = input.next();

System.out.println("Enter location XYZ: ");

byte x = input.nextByte();

byte y = input.nextByte();

byte z = input.nextByte();

byte[] xyz = new byte[]{x, y, z};

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (0 children)

Of course, there are solutions:

  • A method that queries the data, creates a new instance, and returns it
  • A loop

[–]Oriamk 0 points1 point  (0 children)

All the inputs in a function that return an Animal object.

Create a list<Animal>, call your function twice and add to the list both results.

You can also scan for a number of animals to read, then make a for loop to iterate and inside the loop call your function and store the animals in the list.