private double convertLiquid (double none2 ,char unit2, char amount2)
    {
        if (unit == 1)
            return (amount * 33.8140227);
        else if (unit == 2)
            return (amount * 0.001);
        if (unit == 3)
            return (amount * 1000);
        if (unit == 4)
            return (amount * 0.00852167911);
        if (unit == 5)
            return (amount * 4.22675284);
        if (unit == 6)
            return (amount * 202.884136);
        if (unit == 7)
            return (amount * 67.6280454);
        if (unit == 8)
            return (amount * 0.264172052);
        if (unit == 9)
            return (amount * 1.05668821);
        if (unit == 10)
            return (amount * 2.11337642);
        else
            return (none2);
    }


    public void displayData ()
    {
        c = new Console ();
        title ();
        c.println ("Your amount is " + amount + " converted to " + convertLiquid (none2, unit2, amount2));
        c.println ("If you want to go back the main menu press 1, if you wish to exit press 0: ");
        while (true)

            {
                try
                {
                    procstr = c.readLine ();
                    proc = Integer.parseInt (procstr);
                    if (proc == 1)
                        mainmenu ();
                    if (proc == 0)
                        goodbye ();
                    break;
                }
                catch (NumberFormatException e)
                {
                    c.println ("That's not an integer. Please Try Again");
                }
            }


    }

What's wrong with my paremeters? It keeps getting error?

Recommended Answers

All 25 Replies

Can you show the error please. Or post me your whole code, so I can check it out in my compiler.

c.println ("Your amount is " + amount + " converted to " + convertLiquid (none2, unit2, amount2));

The error occurs at that line saying that none2 isn't found..

Can you post all of your code? Are you actually passing a value to none2? Also, why do you have if, else if, then a bunch of ifs?

Yes none2 has a value of 0.

// The "LiquidConversion" class.
import java.awt.*;
import hsa.Console;

public class LiquidConversion
{
    static Console c;           // The output console
    static String procstr;
    static int proc;
    static String amountstr;
    static double amount;
    static String unitstr;
    static int unit;
    static double none2 = 0;
    static char unit2;
    static char amount2;
    static boolean runLoop = true;

    private void title ()
    {
        c.print (' ', 31);
        c.print ("Liquid Conversion");
        c.println ();
    }


    public void intro ()
    {
        c = new Console ();
        title ();
        c.println ("This program will allow you to conver an amount of litre into kilolitres, millilitres, barrels, cups, teaspoons, tablespoons, gallons, quarts, and pints. Press 0 to go back to the main menu.");

        while (runLoop = true)
        {
            try
            {
                procstr = c.readLine ();
                proc = Integer.parseInt (procstr);
                if (proc == 0)
                    mainmenu();
                if (proc != 0)
                    runLoop = true;
                break;
            }
            catch (NumberFormatException e)
            {
                c.println ("That's not an integer. Please Try Again");
            }
        }
    }


    public void mainmenu ()
    {
        title ();
        c.println ("If you wish to exit press 5, if you want to go the intro press 2, and if you want to continue 3:");

        while (runLoop = true)
        {
            try
            {
                procstr = c.readLine ();
                proc = Integer.parseInt (procstr);
                if (proc == 3)
                    askData ();
                if (proc == 2)
                    intro ();
                if (proc == 5)
                    goodbye ();
                if (proc == 5 || proc == 3 || proc == 2)
                    runLoop = false;
                break;
            }
            catch (NumberFormatException e)
            {
                c.println ("That's not an integer. Please Try Again");
            }
        }
        //while (proc == 5 && proc == 2 && proc == 3);
    }


    public void askData ()
    {
        c = new Console ();
        c.print ("1 for Litres, 2 for Kilolitres, 3 for Millilitres, 4 for Barrels, 5 for cups, 6 for teaspoons, 7 for tablespoons, 8 gallons, 9 for quartz and 10 for pints.");
        c.println ("The amount your converting will be in litres. Please input the amount you want to convert: ");

        while (true)

            {
                try
                {
                    amountstr = c.readLine ();
                    amount = Double.parseDouble (amountstr);
                    break;
                }
                catch (NumberFormatException e)
                {
                    c.println ("That's not an integer. Please Try Again");
                }
            }

        c.println ("Please input the unit you want your amount to be converted to: ");

        while (true)
        {
            try
            {
                unitstr = c.readLine ();
                unit = Integer.parseInt (unitstr);
                displayData ();
                break;
            }
            catch (NumberFormatException e)
            {
                c.println ("That's not an integer. Please Try Again");
            }
        }

    }


    private double convertLiquid (none2, unit2, amount2)
    {
        if (unit == 1)
            return (amount * 33.8140227);
        if (unit == 2)
            return (amount * 0.001);
        if (unit == 3)
            return (amount * 1000);
        if (unit == 4)
            return (amount * 0.00852167911);
        if (unit == 5)
            return (amount * 4.22675284);
        if (unit == 6)
            return (amount * 202.884136);
        if (unit == 7)
            return (amount * 67.6280454);
        if (unit == 8)
            return (amount * 0.264172052);
        if (unit == 9)
            return (amount * 1.05668821);
        if (unit == 10)
            return (amount * 2.11337642);
        else
            return (none);
    }


    public void displayData ()
    {
        c = new Console ();
        title ();
        c.println ("Your amount is " + amount + " converted to " + convertLiquid (none2, unit2, amount2));
        c.println ("If you want to go back the main menu press 1, if you wish to exit press 0: ");
        while (true)

            {
                try
                {
                    procstr = c.readLine ();
                    proc = Integer.parseInt (procstr);
                    if (proc == 1)
                        mainmenu ();
                    if (proc == 0)
                        goodbye ();
                    break;
                }
                catch (NumberFormatException e)
                {
                    c.println ("That's not an integer. Please Try Again");
                }
            }


    }


    public void goodbye ()
    {
        c = new Console ();
        title ();
        c.print ("Thank you for using the program!");
    }


    public static void main (String[] args)
    {
        c = new Console ();
        LiquidConversion d;
        d = new LiquidConversion ();
        d.mainmenu ();

        // Place your program here.  'c' is the output console
    } // main method
} // LiquidConversion class

That's the entire code. Thanks.

All kinds of things happening here...

Firstly, you're passing in a parameter of char unit2 but that parameter never gets used in the routine. So presumably unit is a numeric class member of some kind that your method can use. Is that what you want?

Secondly, in the method displayData, you're using variables called none2. unit2 and amount2 but they aren't declared in the method. So if they aren't class members then they don't exist at all outside the scope of convertLiquid.

You have to remember that your parameters are local to the method that declares them - nowhere else. Also, the names of your parameters are independent of the names of the variables you want to pass into the method - they don't have to be the same as anything outside the method. In fact, it's better if they're not otherwise you get difficult-to-track shadowing situations where parameters and local variables hide class members.

I suspect - although this is a guess-by-God - that you intend your call to ready something like this: c.println ("Your amount is " + amount + " converted to " + convertLiquid (-1.0, unit, amount)); And your convertLiquid method should (perhaps) be using unit2 rather than unit...

The third thing is that this is an awfully ugly way of doing your conversion method, although as a student you may not have learned a better way yet. You could use an array.

double [] convertFactors = {33.8140227, 0.001, 1000, 
      0.00852167911, 4.22675284, 202.884136, 67.6280454, 
      0.264172052, 1.05668821, 2.11337642
      };
  double convertLiquid (double none2, char unit2, double amount2) {
    double answer = none2;
    if (unit2 >= 1) && (unit2 <= convertFactors.length) {
      answer = amount2 * convertFactors [unit2 - 1];
    }
    return answer;
  }

Hope this helps...

Yup you're right, we haven't learned of arrays yet so I don't think I can use it on this assignment.

Thanks, you've helped a lot. :)

Ok, I am not familiar with the hsa package, and I did not download it to import, but from what I briefly read to see what it was, it sounds like bad news(but that is only what I read I have NO experience with it.) But I did debug your code by removing the hsa package and just using the java.util package. Did you have to use the hsa package for your assignment?

It will take a minute to type up but I will walk you through what I did.(keep in mind I did not check your values for the conversion and am not going to) just trying to help you figure out your code.

Nope we don't have to use the hsa package.

Can you show me what you did?

Yes, I am commenting on the code now so you can read through it and understand.

Thank you so much

Ok here is the code with my changes. Please carefully read the comments and try to understand what I did. You do not have to thank me but for my integrity please read the comments. If you do not understand something ask, google it, or reference old threads on the forum. Also, on your if statements I would recommend using the brackets, I noticed you did not on any of them. I would also read up on the scanner class and if statements. Also, you should read up on the switch statement, if you adjusted your code a little bit it could be a nice alternative to the huge block of if statements you have.

import java.util.Scanner; 
//Imported scanner through util pkg, keep in mind if you are going to 
//use more than one function from a package you can often import something like
//this import java.util.*;(this imports everything from the package)
//Also .util pkg is already supplied by java VS hsa that is no, like you were using
 
//Changed the methods to static since they were non-static and trying to be referenced
public class LiquidConversion {
	
    //static Console c;   I removed console because it is no longer needed.      
    static String procstr;
    static int proc;
    static String amountstr;
    static double amount;
    static String unitstr;
    static int unit;
    static double none2 = 0;
    static char unit2;
    static char amount2;
    static boolean runLoop = true;
    //Creates a scanner object so the program can read in the data that a user enters
    static Scanner kb = new Scanner(System.in);
    //created a global scanner object because you use it in several places in your code, 
    //you will see it where it is kb.(something)
    
    public static void main (String[] args)
    {
        @SuppressWarnings("unused")
		LiquidConversion d;
        d = new LiquidConversion ();
		mainmenu();       
    } 
    
    private static void title ()
    {
    	//Changed to System.out.println so it prints to the console
        System.out.println ("Liquid Conversion");
        //System.out.println (); Removed line so output looks cleaner.
    }
 
    public static void intro ()
    {
        title ();
        //Changed to System.out.println so it prints to the console and added \n so output looks cleaner
        System.out.println ("This program will allow you to convert an amount of litre into kilolitres, millilitres, barrels, cups, " +
        		"\n teaspoons, tablespoons, gallons, quarts, and pints. Press 0 to go back to the main menu.");
        while (runLoop = true)
        {
            try
            {
            	//Changed to kb.next(); to read in the string that user has entered into console.
                procstr = kb.next();
                proc = Integer.parseInt (procstr);
                //Changed to an if-else format VS all ifs like you had. if-else because there can only be
                // two outcomes here. proc==0 or proc !=0, so if it equals 0 then good otherwise the only
                //other outcome would be false.
                if (proc == 0){
                    mainmenu();
                }else{
                    runLoop = true;
                }
                break;
            }
            catch (NumberFormatException e)
            {
            	//Changed to System.out.println so it prints to the console
            	System.out.println ("That's not an integer. Please Try Again");
            }
        }
    }
 
 
    public static void mainmenu ()
    {
        title ();
        //Changed to System.out.println so it prints to the console
        System.out.println ("If you wish to exit press 5, if you want to go the intro press 2, and if you want to continue 3:");
        while (runLoop = true)
        {
            try
            {
            	//Changed to kb.next(); to read in the string that user has entered into console.
                procstr = kb.next();
                proc = Integer.parseInt (procstr);
                //Changed to an if-else if format VS all ifs like you had. if-else-if statements are good because
            	//it test all the conditions from top to bottom and once a condition is true, it stops testing.
                if (proc == 3){
                    askData ();
                }else if (proc == 2){
                    intro ();
                }else if (proc == 5){
                    goodbye ();
                }else if (proc == 5 || proc == 3 || proc == 2){
                    runLoop = false;
                }
                break;
            }
            catch (NumberFormatException e)
            {
            	//Changed to System.out.println so it prints to the console
            	System.out.println ("That's not an integer. Please Try Again");
            }
        }
    }
 
 
    public static void askData ()
    {
    	//Removed line since not needed any more because you are printing to the console
        //c = new Console (); 
    	//Changed to System.out.println so it prints to the console and added \n to make program look cleaner in console
        System.out.println ("1 for Litres, 2 for Kilolitres, 3 for Millilitres, 4 for Barrels, 5 for cups, 6 for teaspoons, " +
        					"\n7 for tablespoons, 8 gallons, 9 for quartz and 10 for pints.\n");
        //Changed to System.out.println so it prints to the console
        System.out.println ("The amount your converting will be in litres. Please input the amount you want to convert: ");
        while (true)
            {
                try
                {
                	//Changed to kb.next(); to read in the string that user has entered into console.
                    amountstr = kb.next();
                    amount = Double.parseDouble (amountstr);
                    break;
                }
                catch (NumberFormatException e)
                {
                	//Changed to System.out.println so it prints to the console
                	System.out.println ("That's not an integer. Please Try Again");
                }
            }
        //Changed to System.out.println so it prints to the console
        System.out.println ("Please input the unit you want your amount to be converted to: ");
 
        while (true)
        {
            try
            {	
            	//Changed to kb.next(); to read in the string that user has entered into console.
                unitstr = kb.next();
                unit = Integer.parseInt (unitstr);
                displayData ();
                break;
            }
            catch (NumberFormatException e)
            {
            	//Changed to System.out.println so it prints to the console
            	System.out.println ("That's not an integer. Please Try Again");
            }
        }
 
    }
 
    private static double convertLiquid (double none2,char unit2,char amount2)
    {
    	//Changed to an if-else if format VS all ifs like you had. if-else-if statements are good because
    	//it test all the conditions from top to bottom and once a condition is true, it stops testing.
        if (unit == 1){
            return (amount * 33.8140227);
        }else if (unit == 2){
            return (amount * 0.001);
        }else if (unit == 3){
            return (amount * 1000);
        }else if (unit == 4){
            return (amount * 0.00852167911);
        }else if (unit == 5){
            return (amount * 4.22675284);
        }else if (unit == 6){
            return (amount * 202.884136);
        }else if (unit == 7){
            return (amount * 67.6280454);
        }else if (unit == 8){
            return (amount * 0.264172052);
        }else if (unit == 9){
            return (amount * 1.05668821);
        }else if (unit == 10){
            return (amount * 2.11337642);
        }else{
        	//changed to "none2", you had "none"
            return (none2);
        }
    }
 
    public static void displayData ()
    {  
        title ();
        //Changed to System.out.println so it prints to the console
        System.out.println ("Your amount is " + amount + " converted to " + convertLiquid (none2, unit2, amount2));
        //Changed to System.out.println so it prints to the console
        System.out.println ("If you want to go back the main menu press 1, if you wish to exit press 0: ");
        while (true)
            {
                try
                {
                	//Changed to kb.next(); to read in the string that user has entered into console.
                    procstr = kb.next();
                    proc = Integer.parseInt (procstr);
                    if (proc == 1)
                        mainmenu ();
                    if (proc == 0)
                        goodbye ();
                    break;
                }
                catch (NumberFormatException e)
                {	
                	//Changed to System.out.println so it prints to the console
                	System.out.println ("That's not an integer. Please Try Again");
                }
            }
    }
    
    public static void goodbye ()
    {
        title ();
        //Changed to System.out.println so it prints to the console
        System.out.println ("Thank you for using the program!");
    }
}

Yay! Thank you so much!

Judging from your code, you did not know about \n. When using this in a print statement, everything after it drops down to a new line. This can make output appear "cleaner".

And for the scanner class, I used: "datafield" = kb.next();
.next() is used for strings but if you have a different type of datafield you can read in other data types for example: .nextDouble(); for doubles, .nextInt() for integers...etc.

This way you do not have to use parse methods all the time.

Ahhh. Thanks. I have a question though, it just stops on executing on the intro and mainmenu if the user input another number other than 0 on the intro and 5,3, or 2 on the mainmenu. Can you help me with that?

I forgot to make comment in the code, but reread what Paul Norris wrote. Especially the part where you did not declare the the types for the parameters in the method:

private static double convertLiquid (double none2,char unit2,char amount2)

You had:

private static double convertLiquid (none2, unit2, amount2)

Yeah I got that fixed. Thanks.

Ahhh. Thanks. I have a question though, it just stops on executing on the intro and mainmenu if the user input another number other than 0 on the intro and 5,3, or 2 on the mainmenu. Can you help me with that?

Look through your code and find where it ask for the input from user for this. Make some kind of test condition for users input. You need to have some control over what they are entering. If you only want them to enter 0 to go back to main menu then write a test condition that checks and makes sure they only entered 0.

Isn't it this:

if (proc == 0){
mainmenu();
}else{
runLoop = true;

. Isn't that asking for an input then saying that any other input will run the loop again?

That is not asking for input, that is checking the input.

Right

procstr = kb.next();
proc = Integer.parseInt (procstr);
if (proc == 0){
mainmenu();
}else{
runLoop = true;

That one is the one with the asking for input and checking it. Right?

Thank you so much for all the help! I fixed it.

No problem hope you understand it more clearly now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.