Question

Please edit my following JAVA code so that there is no main function. I would like...

Please edit my following JAVA code so that there is no main function. I would like one function in this class that completes what the current program is trying to do so that I can call this class in my main class which will be separate.

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Scanner;

public class enterwklyincme {

    private static DecimalFormat df = new DecimalFormat("0.00");

    public static float readFloat(Scanner reader) {
        float num;
        while (true) {
            try {
                num = Float.parseFloat(reader.nextLine());
                return num;
            } catch (Exception e) {
                System.out.println("Please enter a numerical value.");
            }
        }
    }

    public static void main(String args[]) {

        float weeklyIncome;

        Scanner in = new Scanner(System.in);

        System.out.println("What is you weekly income?");

        weeklyIncome = readFloat(in);

        while (weeklyIncome < 0) {
            System.out.println("Please enter a value of 0 or higher.");

            weeklyIncome = readFloat(in);
        }
        System.out.println("Weekly income of $" + df.format(weeklyIncome) + " has been saved to your user profile!");
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Scanner;
class enterwklyincme {
    DecimalFormat df = new DecimalFormat("0.00");
    Scanner in = new Scanner(System.in);
    public void readFloat() {
        float num;
        System.out.println("What is you weekly income?");
        while (true) {
            try {
                num = Float.parseFloat(in.nextLine());
                break;
                }
            catch (Exception e) {
                System.out.println("Please enter a numerical value.");
                }
        }
        while(num<0)
        {
             System.out.println("Please enter a value of 0 or higher.");
           while (true) {
            try {
                num = Float.parseFloat(in.nextLine());
                break;
                }
                catch (Exception e)
                {
                System.out.println("Please enter a numerical value.");
                }
          
                }
        }
       System.out.println("Weekly income of $" + df.format(num) + " has been saved to your user profile!");

      
    }
}
public class Main{
    public static void main(String args[]) {

        enterwklyincme ob=new enterwklyincme();
        ob.readFloat();
      
            }
}

output

input What is you weekly income? -5 Please enter a value of 0 or higher. Weekly income of $4.00 has been saved to your user p

input What is you weekly income? -5 Please enter a value of 0 or higher. Weekly income of $4.00 has been saved to your user profile! ... Program finished with exit code o Press ENTER to exit console.

Add a comment
Know the answer?
Add Answer to:
Please edit my following JAVA code so that there is no main function. I would like...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please put both of this loops into one program. Set it up so that there is...

    Please put both of this loops into one program. Set it up so that there is one main function and both loops are within it. Thank You. import java.util.Scanner; public class WhileDavidMungomaLab12 { public static void main(String[] args) { int aScore = -1; while (aScore < 0 || aScore > 100) { Scanner sc = new Scanner(System.in); System.out.println("Please enter a valid number that is within the specified range(0 and 100): "); aScore = sc.nextInt(); } } } AND import java.util.Scanner;...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation...

    I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation problem: at EvenOdd.main(EvenOdd.java:10) I am unable to find what I can do to fix this issue. Can you please assist? My code is below: import java.util.InputMismatchException; import java.util.Scanner; public class EvenOdd {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        System.out.println("Welcome to this Odd program!");        int userInput1 = input.nextInt();    }       public...

  • Please fix my code so I can get this output: Enter the first 12-digit of an...

    Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...

  • Hi everyone! I need help on my Java assignment. I need to create a method that...

    Hi everyone! I need help on my Java assignment. I need to create a method that is called sumIt that will sum two values and will return the value.It should also invoke cubeIt from the sum of the two values. I need to change the currecnt program that I have to make it input two values from the console. The method has to be called sumIt and will return to the sum that will produce the cube of the sum...

  • I need help on creating a while loop for my Java assignment. I am tasked to...

    I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...

  • I wrote two java classes. One is main and other one is getter and setter. I...

    I wrote two java classes. One is main and other one is getter and setter. I brought methods from Digit class to Main class to print result. However, for some reason, only welcome print statement got printed and others got ignored. On the output screen, only welcome statement appear. nDigit should be input from user. import java.util.Scanner; public class Main {    public static void main(String[] args)    {        Digit digitGet = new Digit();              ...

  • I need the following java code commented import java.util.Scanner; public class Main { public static void...

    I need the following java code commented import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); int productNo=0; double product1; double product2; double product3; double product4; double product5; int quantity; double totalsales=0; while(productNo !=0) System.out.println("Enter Product Number 1-5"); productNo=input.nextInt(); System.out.println("Enter Quantity Sold"); quantity=input.nextInt(); switch (productNo) { case 1: product1=1.00; totalsales+=(1.00*quantity); break; case 2: product2=2.00; totalsales+=(2.00*quantity); break; case 3: product3=6.00; totalsales+=(6.00*quantity); break; case 4: product4=23.00; totalsales+=(23.00*quantity); break; case 5: product5=17.00; totalsales+=(17.00*quantity); break;...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • This is my code but my overtime calculation is wrong and I cant figure out how...

    This is my code but my overtime calculation is wrong and I cant figure out how to round to two decimal places import java.util.Scanner; public class HourlyWage { public static void main(String[] args) { //Variables double totalWage; double totalOvertimePay; double totalPay; String name; double overtimeHours = 0.0; double hoursWorked = 0.0; double hourlyWage = 0.0; Scanner keyboard = new Scanner(System.in); System.out.println("Please enter your name "); name = keyboard.next(); System.out.println("Please enter your hourly wage "); hourlyWage = keyboard.nextDouble(); System.out.println("How many hours...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT