Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Scanner and While loop question I try to prompt user to enter a postive number and if the user's input is negative, there should

Java Scanner and While loop question

I try to prompt user to enter a postive number and if the user's input is negative, there should be a message saying enter a positive number. otherwise if user enters "quit" the loop should break.

So I use scanner method nextLine to read what's user's input and cast it into double if user enters number. However, I might've done it wrong as I keep getting an error message. Also should I put another while loop within the while loop? like what I've done below.

image text in transcribed

image text in transcribed

Library/Java/Javavirtua LMachines/jdkT.8.0 161.3ak/Contents/Home/bin/java .. Enter a positive number; [type quit to exitl quit Exception in thread "main" java. lang.NumberFormatException: For input string: "quit" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimaL.java:110) at java. Lang.Double. parseDouble (Double.iava:538) at Random.main(Random.java: 10) Process finished with exit code 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PC Magazine Guide To Client Server Databases

Authors: Joe Salemi

1st Edition

156276070X, 978-1562760700

More Books

Students also viewed these Databases questions