#Need help with an assignment

1 messages · Page 1 of 1 (latest)

charred peak
#

So theres a lot to this assignment, but ill jus send it in small chunks and work through it with whoever responds to this. I've been at this for awhile and I'm not exactly sure what to do about this, this is due Friday and I got a lot more stuff to do not just for this class but other classes as well. Any sort of help is appreciated.

tardy pumiceBOT
#

<@&987246717831381062> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

sure where to start. Can you help me break it down and figure out the best approach?

chrome stag
#

What have you programmer so far?

charred peak
#

you mean what have I programmed?

#

literally nothing, i keep thinking i have a start but end up scrapping it when it either doesnt work or if im starting to confuse myself some how

#

i feel like this is something really simple but i completely forgot how to do any of this stuff

#

this is the farthest ive gotten bro 😭

chrome stag
#

We can help you with questions, but we don't write the program for you.
So break the assignment up in chunks:

  • use a Scanner to ask for n and x, then output that
  • do the research on Horner's method
  • translate that algorithm to Java code
  • do the research on 3 alternative algorithms
charred peak
#

how do i output the scanner tho

chrome stag
#

You use the Scanner class to capture input.

tardy pumiceBOT
#

Mixing any nextXXX method with nextLine from the Scanner class for user input, will not ask you for input again but instead result in an empty line read by nextLine.

To prevent this, when reading user input, always only use nextLine. If you need an int, do

int value = Integer.parseInt(scanner.nextLine());

instead of using nextInt.

Assume the following:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter your age:");
int age = scanner.nextInt();
System.out.println("Enter your name:");
String name = scanner.nextLine();

System.out.println("Hello " + name + ", you are " + age + " years old");

When executing this code, you will be asked to enter an age, suppose you enter 20.
However, the code will not ask you to actually input a name and the output will be:

Hello , you are 20 years old.

The reason why is that when you hit the enter button, your actual input is

20\n

and not just 20. A call to nextInt will now consume the 20 and leave the newline symbol \n in the internal input buffer of System.in. The call to nextLine will now not lead to a new input, since there is still unread input left in System.in. So it will read the \n, leading to an empty input.

So every user input is not only a number, but a full line. As such, it makes much more sense to also use nextLine(), even if reading just an age. The corrected code which works as intended is:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter your age:");
// Now nextLine, not nextInt anymore
int age = Integer.parseInt(scanner.nextLine());
System.out.println("Enter your name:");
String name = scanner.nextLine();

System.out.println("Hello " + name + ", you are " + age + " years old");

The nextXXX methods, such as nextInt can be useful when reading multi-input from a single line. For example when you enter 20 John in a single line.