I can't figure out my simple Java homework

I have this programming that converts between meters and feet, and between kilograms and pounds. When I tell the program, I want to convert the weight (by entering "w" when prompted), this gives me the following error:

Error: Too many input characters error.

I have been working on this for a long time, but cannot figure it out. Can someone please tell me how to do weight conversion as length conversion?

import java.util.Scanner;

/**
 * This class..
 */
public class UnitConversion3b
{
    public static void main(String[] args)   
    {
        Scanner keyboard = new Scanner(System.in);

        String maxInputWarning = "\nError: Too many input characters."
        + "\nProgram is now terminating.";
        String lengthOrWeight;
        final double LENGTH_CONVERSION_FACTOR = 3.2808399;
        final double WEIGHT_CONVERSION_FACTOR = 2.20462;
        String whichWeightConversion = "empty" , whichLengthConversion = "empty";
        double feet = 0, meters = 0, pounds =0 , kilograms = 0;
        double metersConvertedToFeet, feetConvertedToMeters;
        double poundsConvertedToKilograms, kilogramsConvertedToPounds;

        System.out.println("");
        System.out.print("What kind of value would you like to convert?");
        System.out.print("\nEnter L for length, or W for weight: ");
        lengthOrWeight = keyboard.nextLine();
        if (lengthOrWeight.length() > 1 ) {
            System.out.println(maxInputWarning);
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if ((!(lengthOrWeight.equalsIgnoreCase("l"))
            && (!(lengthOrWeight.equalsIgnoreCase("w"))))){
            System.out.println("\nError: Unrecognized conversion type."
            + "\nProgram is now terminating.");
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if (lengthOrWeight.equalsIgnoreCase("l")){
            System.out.println("\nConverting feet or meters?");
            System.out.print("Enter F to convert feet, or M for meters: "); 
            whichLengthConversion = keyboard.nextLine();
        }

        if (whichLengthConversion.length() > 1 ) {
            System.out.println(maxInputWarning);
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if ((!(whichLengthConversion.equalsIgnoreCase("f"))
            && (!(whichLengthConversion.equalsIgnoreCase("m"))))){
            System.out.println("\nError: Unrecognized unit of "
            + "measurement.\nProgram is now terminating."     );
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if (whichLengthConversion.equalsIgnoreCase("f")){
            System.out.print ("Enter the number of feet to"
            + " convert to meters: ");
            feet = keyboard.nextDouble();
            feetConvertedToMeters = feet / LENGTH_CONVERSION_FACTOR;
            System.out.println("The number of meters in " + feet +
            " feet is " + feetConvertedToMeters + ".");
            keyboard.nextLine();
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if (whichLengthConversion.equalsIgnoreCase("m")){
            System.out.print ("Enter the number of meters to"
            + " convert to feet: ");
            meters = keyboard.nextDouble();
            metersConvertedToFeet = meters * LENGTH_CONVERSION_FACTOR;
            System.out.println("The number of feet in " + meters +
            " meters is " + metersConvertedToFeet + ".");
            keyboard.nextLine();
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        }

        if (lengthOrWeight.equalsIgnoreCase("w")){
            System.out.println("Converting pounds or kilograms?");
            System.out.print("Enter P to convert pounds, or K for kilograms: ");
            whichWeightConversion = keyboard.nextLine();
        }

        if (whichWeightConversion.length() > 1 ) { 
            System.out.println(maxInputWarning);
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if ((!(whichWeightConversion.equalsIgnoreCase("p"))
            && (!(whichWeightConversion.equalsIgnoreCase("k"))))){
            System.out.println("\nError: Unrecognized unit of "
            + "measurement.\nProgram is now terminating."     );
            System.out.print("Press Enter to continue ... ");
            return;
        } else if (whichWeightConversion.equalsIgnoreCase("p")){
            System.out.println("Enter the number of pounds to"
            + " convert to kilograms:");
            pounds = keyboard.nextDouble();
            poundsConvertedToKilograms = pounds / WEIGHT_CONVERSION_FACTOR;
            System.out.println("The number of pounds in " + kilograms +
            " kilograms is " + poundsConvertedToKilograms + ".");
            keyboard.nextLine();
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if (whichLengthConversion.equalsIgnoreCase("k")){
            System.out.print ("Enter the number of kilograms to"
            + " convert to pounds: ");
            kilograms = keyboard.nextDouble();
            kilogramsConvertedToPounds = kilograms * WEIGHT_CONVERSION_FACTOR;
            System.out.println("The number of pounds in " + pounds +
            "pounds is " + kilogramsConvertedToPounds + ".");
            keyboard.nextLine();
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;

        } else{ 
            return;
        }
    }
}
+3
source share
7 answers

, , . , "", ... , .

import java.util.Scanner;

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

        Scanner keyboard = new Scanner(System.in);

        String maxInputWarning = "\nError: Too many input characters."
                + "\nProgram is now terminating.";
        String lengthOrWeight;
        final double LENGTH_CONVERSION_FACTOR = 3.2808399;
        final double WEIGHT_CONVERSION_FACTOR = 2.20462;
        String whichWeightConversion = "empty", whichLengthConversion = "empty";
        double feet = 0, meters = 0, pounds = 0, kilograms = 0;
        double metersConvertedToFeet, feetConvertedToMeters;
        double poundsConvertedToKilograms, kilogramsConvertedToPounds;

        System.out.println("");
        System.out.print("What kind of value would you like to convert?");
        System.out.print("\nEnter L for length, or W for weight: ");

        lengthOrWeight = keyboard.nextLine();
        if (lengthOrWeight.length() > 1) {
            System.out.println(maxInputWarning);
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if ((!(lengthOrWeight.equalsIgnoreCase("l")) && (!(lengthOrWeight
                .equalsIgnoreCase("w"))))) {
            System.out.println("\nError: Unrecognized conversion type."
                    + "\nProgram is now terminating.");
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
        } else if (lengthOrWeight.equalsIgnoreCase("l")) {
            System.out.println("\nConverting feet or meters?");
            System.out.print("Enter F to convert feet, or M for meters: ");
            whichLengthConversion = keyboard.nextLine();

            if (whichLengthConversion.length() > 1) {
                System.out.println(maxInputWarning);
                System.out.print("Press Enter to continue ... ");
                keyboard.nextLine();
                return;
            } else if ((!(whichLengthConversion.equalsIgnoreCase("f")) && (!(whichLengthConversion
                    .equalsIgnoreCase("m"))))) {
                System.out.println("\nError: Unrecognized unit of "
                        + "measurement.\nProgram is now terminating.");
                System.out.print("Press Enter to continue ... ");
                keyboard.nextLine();
                return;
            } else if (whichLengthConversion.equalsIgnoreCase("f")) {
                System.out.print("Enter the number of feet to"
                        + " convert to meters: ");
                feet = keyboard.nextDouble();
                feetConvertedToMeters = feet / LENGTH_CONVERSION_FACTOR;
                System.out.println(feet + " Feet in Meters is "
                        + feetConvertedToMeters + ".");
                keyboard.nextLine();
                System.out.print("Press Enter to continue ... ");
                keyboard.nextLine();
                return;
            } else if (whichLengthConversion.equalsIgnoreCase("m")) {
                System.out.print("Enter the number of meters to"
                        + " convert to feet: ");
                meters = keyboard.nextDouble();
                metersConvertedToFeet = meters * LENGTH_CONVERSION_FACTOR;
                System.out.println(meters + " Meters in Feet is "
                        + metersConvertedToFeet + ".");
                keyboard.nextLine();
                System.out.print("Press Enter to continue ... ");
                keyboard.nextLine();
                return;
            }
        }

        else {
            System.out.println("Converting pounds or kilograms?");
            System.out.print("Enter P to convert pounds, or K for kilograms: ");
            whichWeightConversion = keyboard.nextLine();

            if (whichWeightConversion.length() > 1) {
                System.out.println(maxInputWarning);
                System.out.print("Press Enter to continue ... ");
                keyboard.nextLine();
                return;
            } else if ((!(whichWeightConversion.equalsIgnoreCase("p")) && (!(whichWeightConversion
                    .equalsIgnoreCase("k"))))) {
                System.out.println("\nError: Unrecognized unit of "
                        + "measurement.\nProgram is now terminating.");
                System.out.print("Press Enter to continue ... ");
                return;
            } else if (whichWeightConversion.equalsIgnoreCase("p")) {
                System.out.println("Enter the number of pounds to"
                        + " convert to kilograms:");
                pounds = keyboard.nextDouble();
                poundsConvertedToKilograms = pounds / WEIGHT_CONVERSION_FACTOR;
                System.out.println(pounds + " Pounds in Kilograms is "
                        + poundsConvertedToKilograms + ".");
                keyboard.nextLine();
                System.out.print("Press Enter to continue ... ");
                keyboard.nextLine();
                return;
            } else if (whichWeightConversion.equalsIgnoreCase("k")) {
                System.out.print("Enter the number of kilograms to"
                        + " convert to pounds: ");
                kilograms = keyboard.nextDouble();
                kilogramsConvertedToPounds = kilograms
                        * WEIGHT_CONVERSION_FACTOR;
                System.out.println(kilograms + " Kilograms in Pounds is "
                        + kilogramsConvertedToPounds + ".");
                keyboard.nextLine();
                System.out.print("Press Enter to continue ... ");
                keyboard.nextLine();
                return;

            }
        }
    }
}
+5

.

- , 47, , . , , , , .

+5

, . . , , SOP/ DemoMain, UnitConversion3b . , , { , , , , , . , - , . , - , , : -/

+2

(!(lengthOrWeight.equalsIgnoreCase("l"))
            && (!(lengthOrWeight.equalsIgnoreCase("w"))))){

else

else{
    System.out.println("\nError: Unrecognized conversion type."
            + "\nProgram is now terminating.");
            System.out.print("Press Enter to continue ... ");
            keyboard.nextLine();
            return;
}

, .

, line.equalsIgnoreCase( "l" ), , .

+1

, , , Scanner nextLine() , ('\n'), .

, String trim(),  :

lengthOrWeight = keyboard.nextLine().trim();
+1

, , : , , . - . , , , , -, , Cougar. , - . , , .

!!! - . , . , . Cougar.

, . , . .

Banana Republic Nikon - . Float . , .

", , ?"

" ", :

This is the length of the Cougar in feet, I need it it converted to meters...

This is the length of the Cougar in  meters, I need it it converted to feet...

" ", :

This is the weight in pounds, I need it converted to kilos...

This is the weight in kilos, I need it converted to pounds...

, , , : " "? , , , .

0

, : :

TestMain.java :

import java.util.Scanner;

public class TestMain
{
public static void main(String[] args) 
    {
    float theValue;
    float theAnswerIs;
    char getLengthOrWeight;
    String theValueAsString;
    boolean lOrW; //length or width
    boolean fOrM; //feet or meters
    boolean pOrK;  //pounds or kilos... it a CS joke haha
    char getFeetOrMeters;
    char getPoundsOrKilos;


    //Set up a Scanner instance called keyboard   
    Scanner keyboard = new Scanner(System.in);

    UnitConversion3b converterInstance = new UnitConversion3b();

    //Request user for the number to convert
    System.out.println("What is the value you will be converting?");
    theValueAsString = keyboard.nextLine();

    //convert that value and trap
    theValue = floatToString(theValueAsString);


    //Request user for length or weight conversion
    System.out.println("What kind of value would you like to convert?"); 
    System.out.println("Enter L for length, or W for weight: ");
    //variable = console.next().charAt(0);
    getLengthOrWeight = keyboard.next().charAt(0);
    lOrW = converterInstance.lengthOrWeight(getLengthOrWeight);

    //create a new UnitConversion3B object and pass it the L or W or bad string the user inputs



    //if(true) then user asked for length
    if(lOrW)
    {
        System.out.println("\nConverting feet or meters?"); 
        System.out.print("Enter F to convert feet to meters, or M for meters to feet: ");
        //set our main feetOrMeters variable to the value received when we ask our
        //converterInstance the question whichLengthConversion?
        getFeetOrMeters = keyboard.next().charAt(0);
        fOrM = converterInstance.feetOrMeters(getFeetOrMeters);

        //if(fOrM) aka user asked for a length conversion in feet, let convert it:
        if(fOrM)
        {
            theAnswerIs = (float) (theValue * 3.28083);
            System.out.println("The answer is: " + theAnswerIs + " feet."); 
        }

        //if(!fOrM) aka user asked for a length conversion in meters, let convert it:
        if(!fOrM)
        {
            theAnswerIs = (float) (theValue * 0.3048);
            System.out.println("The answer is: " + theAnswerIs + " feet."); 
        }
        //bad input should be trapped in the feetOrMeters function of the converterInstance
    }

    //if(false) then user asked for weight
    else if(!lOrW)
    {
      System.out.println("Converting pounds or kilograms?"); 
      System.out.print("Enter P to convert pounds to kilos, or K for kilograms to pounds: "); 

        getPoundsOrKilos = keyboard.next().charAt(0);
        pOrK = converterInstance.poundsOrKilos(getPoundsOrKilos);

        //if(pOrK) aka user asked for a pounds to kilos conversion, let convert it:
        if(pOrK)
        {
            theAnswerIs = (float) (theValue * 0.45359237);
            System.out.println("The answer is: " + theAnswerIs + " feet."); 
        }

        //if(!pOrK) aka user asked for a kilos to pounds conversion, let convert it:
        if(!pOrK)
        {
            theAnswerIs = (float) (theValue * 2.20462262);
            System.out.println("The answer is: " + theAnswerIs + " feet."); 
        }
        //bad input should be trapped in the poundsOrKilos function of the converterInstance

    }

}

private static float floatToString(String theValueAsString) {
    // thanks for this method from http://devdaily.com/java/edu/qanda/pjqa00013.shtml
    float f = 0;

    try
    {
      f = Float.valueOf(theValueAsString.trim()).floatValue();
    }
    catch (NumberFormatException nfe)
    {
      System.out.println("NumberFormatException: " + nfe.getMessage());
    }


    return f;
}

}

and UnitConversion3b.java is as follows:

public class UnitConversion3b 
{

    private boolean lengthOrWeightSwitch;
    boolean feetOrMeters;
    final double LENGTH_CONVERSION_FACTOR = 3.2808399; 
    final double WEIGHT_CONVERSION_FACTOR = 2.20462;
    boolean poundsOrKilograms;

    public UnitConversion3b(String getLengthOrWeight) {
        if(getLengthOrWeight == "W")
            lengthOrWeightSwitch = true;
        else if(getLengthOrWeight == "L")
            lengthOrWeightSwitch = false;
        else
        {
            badInput();
        }   
    }

    public boolean getConversionType()
    {
        return lengthOrWeightSwitch;
    }

    public boolean whichLengthConversion(String whichLength)
    {

        if(whichLength == "F")
            feetOrMeters = true;
        else if(whichLength == "M")
            feetOrMeters = false;
        else
        {
            badInput();
        }
        return feetOrMeters;
    }

    public  boolean whichWeightConversion(String whichWeight)
    {

        if(whichWeight == "P")
            poundsOrKilograms = true;
        else if(whichWeight == "K")
            poundsOrKilograms = false;
        else
        {
            badInput();
        }
        return poundsOrKilograms;

    }


    public void badInput()
    {
        System.out.println("Invalid input");
        System.exit(0);
    }

    public String valueToFeet(float theValue) {
        //assumes value entered need to be converted from meters to feet
        return "" + (theValue*LENGTH_CONVERSION_FACTOR);
    }

    public String valueToMeters(float theValue) {
        //assumes value entered need to be converted from feet to meters
        return "" + (theValue/LENGTH_CONVERSION_FACTOR);
    }

    public String valueToPounds(float theValue) {
        // assumes value entered needs to be converted to pounds
        return ""+ (theValue * WEIGHT_CONVERSION_FACTOR);
    }

    public String valueToKilos(float theValue) {
        // TODO Auto-generated method stub
        return ""+ (theValue / WEIGHT_CONVERSION_FACTOR);
    }

    public void setConversionType(char getLengthOrWeight) {
        if(getLengthOrWeight == 'L')
            lengthOrWeightSwitch = true;
        if(getLengthOrWeight == 'W')
            lengthOrWeightSwitch = false;
        else
            badInput();
    }

    public boolean lengthOrWeight(char getLengthOrWeight) {
        if(getLengthOrWeight == 'L')
            return true;
        if(getLengthOrWeight == 'W')
            return false;

        return false;
    }

    public boolean feetOrMeters(char getFeetOrMeters) {
        if(getFeetOrMeters == 'F')
            return true;
        if(getFeetOrMeters == 'M')
            return false;

        //these functions return false under 'false' conditions... work on the logic :-) 
        return false;
    }

    public boolean poundsOrKilos(char getPoundsOrKilos) {
        if(getPoundsOrKilos == 'P')
            return true;
        if(getPoundsOrKilos == 'K')
            return false;

        //these functions return false under 'false' conditions... work on the logic :-) 
        return false;
    }


}

Now, notice that even if I put it in correctly, you will get the worst result if you include this code. It compiles and runs, but ignores the maximum input char #, which, in your opinion, is limited by your purpose. There are probably other problems, but I think this is somewhat consistent code. I would probably like to further break it down into more classes, but I hope this helps.

0
source

Source: https://habr.com/ru/post/1767721/


All Articles