Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: First post, very new to Java, don't kill me...NEED HELP

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default First post, very new to Java, don't kill me...NEED HELP

    Hey guys,
    I am very new to programming and very new to this forum. That is why I am asking everyone to go easy on me. I'm basically trying to teach myself Java. I have over 7 years in the administration and hardware profession and am just trying to get into programming. My question is below:

    I've starting my first application that basically does easy calculations, for examply kg to lbs and another from ft to cm's. What is the best way to repeat a line of code? I know back in the day you could use 'goto' which could let a program skip to any line in the code. What is the equivalent in java? my code is below: I'm trying to ask the user whether they want to do another kg to lbs convert or move to the ft to cm convert:

    			Scanner Scans2 = new Scanner(System.in);
    			double kg;
    			double answerkg;
    			System.out.println("Kg to lbs converter.");
    			System.out.println("Enter KG: ");
    			kg = Scans2.nextDouble();
    			answerkg = kg * 2.205;
    			System.out.println(kg+" kg is equal to "+answerkg+" lbs");
    			System.out.println("");
    			System.out.println("would you like to do another kg to lbs convert?");
    			String yesNo2 = name.next ();
    			if(yesNo2 == yesString){
    				 	}
     
    			Scanner Scans3 = new Scanner (System.in);
    			double ft;
    			double answercm;
    			System.out.println("ft to cm converter.");
    			System.out.println("enter ft now ");
    			ft = Scans3.nextDouble ();
    			answercm = ft * 30.48;
    			System.out.println(ft+" ft is equal to "+answercm+" cm");


    I started to do an if statement but i'm not sure if that is the right answer..... Any help would be great!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: First post, very new to Java, don't kill me...NEED HELP

    What is the best way to repeat a line of code?
    IF you mean you want to execute a line of code more than once, put it inside of a loop.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: First post, very new to Java, don't kill me...NEED HELP

    What is the best way to repeat a line of code? I know back in the day you could use 'goto' which could let a program skip to any line in the code. What is the equivalent in java?
    The equivalent is to break up what you are trying to do into separate methods. A method has a specific task which it performs (like converting kg into lb) and you can call it any number of times.

    It's worth underlining the point that methods are specific: one method, one task. Converting kg->lb is one task, asking the user whether they want another conversion is another task. Typically they will be in different methods.

    import java.util.Scanner;
     
    public class Converter {
     
        public static void main(String[] args) {
            String yesStr = "yes";
            Scanner in = new Scanner(System.in);
     
            System.out.println("Kg to lbs converter.");
            String resp = "";
            do {
                kgToLb();
                System.out.println("Would you like to do another kg to lbs convert?");
                resp = in.nextLine();
            } while(resp.equalsIgnoreCase(yesStr));
     
            System.out.println("ft to cm converter.");
            // similar
        }
     
            /**
             * Prompts user for kg amount and displays the result of converting
             * to kg.
             */
        private static void kgToLb() {
            Scanner in = new Scanner(System.in);
            System.out.println("Enter KG: ");
            double kg = in.nextDouble();
            in.nextLine();
            double lb = kg * 2.205;
            System.out.println(kg+" kg is equal to " + lb + " lbs");
            System.out.println("");
        }
    }

    This is basically your code but arranged so a do/while loop calls a separate method. In the kgToLb() method I call in.nextLine() after reading the double value to skip past the rest of that line.

    There are many ways to break up a complex task into smaller, more manageable pieces. I guess programming *is* the art of breaking things up this way. I don't think there is One Right Way: for instance in this code the conversion could be separated from the prompting and kgToLb() broken up into two pieces.

    The more methods, the better! Each one is simple and hence easy to verify and test in isolation. And they can be reused.

    import java.util.Scanner;
     
    public class Converter2 {
     
        private static final String YES = "yes"; // static final things are "constants"
     
        public static void main(String[] args) {
            kgToLbConverter();
            // ftToCmConverter();
            // etc
        }
     
            /**
             * Repeatedly prompts the user for a kg amount and converts it to lb until
             * the user has had enough.
             */
        private static void kgToLbConverter() {
            Scanner in = new Scanner(System.in);
     
            System.out.println("Kg to lbs converter.");
            String resp = "";
            do {
                kgToLb();
                System.out.println("Would you like to do another kg to lbs convert?");
                resp = in.nextLine();
            } while(resp.equalsIgnoreCase(YES));
        }
     
            /**
             * Prompts user for kg amount and displays the result of converting
             * to kg.
             */
        private static void kgToLb() {
            Scanner in = new Scanner(System.in);
            System.out.println("Enter KG: ");
            double kg = in.nextDouble();
            in.nextLine();
            double lb = kg * 2.205;
            System.out.println(kg+" kg is equal to " + lb + " lbs");
            System.out.println("");
        }
    }

    The documentation illustrated here is quite important for the processes of testing, verifying and reusing. It costs little effort to document things and it should become a reflex action soon after saying "Hello" to the world programmatically. (It is also of great - if little used - value when asking questions in a forum like this because it means everyone can see upfront what a method is *intended* to do even when it fails.)

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: First post, very new to Java, don't kill me...NEED HELP

    Thank you so much for this. methods seem to make things A LOT more organized it makes my train of thought much more clear when designing! I've added a couple things to the code below but and wondering how I would get the very first method (convertoption) to work. Basically I want it to ask what converter The user wants to use. I've spent the last two hours trying to figure it out on my own but am lost... Again, I really appreciate the help you are offering. Here is the new code:

    package package2;

    import java.util.Scanner;

    public class Program2 {

    private static final String YES = "yes"; // static final things are "constants"
    private static final String KGTOLB = "KGtoLB";
    public static void main(String[] args) {
    convertoption();
    ftToCmConverter();
    kgToLbConverter();
    goodbye();
    // etc
    }

    private static void convertoption() {//CHOOSE A CONVERTER TO USE
    Scanner in = new Scanner(System.in);


    private static void kgToLbConverter() { //THIS PROMPTS TO DO THE KG TO LB CONVERTER OVER
    Scanner in = new Scanner(System.in);

    System.out.println("Kg to lbs converter.");
    String resp = "";
    do {
    kgToLb();
    System.out.println("Would you like to do another kg to lbs convert?");
    resp = in.nextLine();
    } while(resp.equalsIgnoreCase(YES));
    }
    private static void ftToCmConverter() { //THIS PROMPTS TO DO THE KG TO LB CONVERTER OVER
    Scanner in = new Scanner(System.in);

    System.out.println("Ft to Cm Converter.");
    String resp = "";
    do {
    ftToCm();
    System.out.println("Would you like to do another ft to cm convert?");
    resp = in.nextLine();
    } while(resp.equalsIgnoreCase(YES));
    }

    private static void kgToLb() { //THIS IS THE KG TO LB CONVERTER
    Scanner in = new Scanner(System.in);
    System.out.println("Enter KG: ");
    double kg = in.nextDouble();
    in.nextLine();
    double lb = kg * 2.205;
    System.out.println(kg+" kg is equal to " + lb + " lbs");
    System.out.println("");
    }
    private static void ftToCm() { //THIS IS THE FT TO CM CONVERTER
    Scanner in = new Scanner (System.in);
    System.out.println("Enter FT: ");
    double ft = in.nextDouble();
    in.nextLine();
    double cm = ft * 30.48;
    System.out.println(ft+" ft is equal to " + cm + " cm");
    }
    private static void goodbye(){ //Says goodbye to everyone
    System.out.println("Thanks for using our converters!");
    }
    }

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: First post, very new to Java, don't kill me...NEED HELP

    private static void convertoption() {//CHOOSE A CONVERTER TO USE
        Scanner in = new Scanner(System.in);

    To begin with I don't think a void method is going to work here. This method has to return something to whoever calls it. What it returns will reflect what the user wants to do. It could as simple as returning 1 if the user wants to do the length conversion and return 2 if they want to do the mass conversion. (The other methods didn't have to return anything because they just did their job - they didn't produce a result.)

    Overall the structure will look like:

    // in main()...
    int conversion = getConvertOption();
    if(conversion == 1) {
        ftToCmConverter();
    } else if(//etc
     
     
    // later...
        /**
         * Prompts for and returns the conversion required by the user.
         * @return 1 for length conversion, 2 for mass conversion
         */
    private static void getConvertOption() {
        // prompt user: 1==convert lengths, etc.  Like a menu.
        // get user response.  eg nextInt()
     
        // ??? check that response is valid ie is 1 or 2
     
        // return response
    }

Similar Threads

  1. Kill the Mice!
    By KevinGreen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2011, 06:17 AM
  2. Problem sending POST request with Java..
    By lost in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2010, 09:16 PM
  3. Java: Handling cookies when logging in with POST
    By cloakbot in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 16th, 2010, 04:31 PM
  4. Kill a process
    By subhvi in forum Java Theory & Questions
    Replies: 5
    Last Post: January 14th, 2010, 09:11 PM
  5. How to kill proceses
    By oyekunmi in forum Java Theory & Questions
    Replies: 3
    Last Post: October 16th, 2009, 08:34 AM