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 2 of 2

Thread: Help looping back to top if invalid

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help looping back to top if invalid

    Hello,

    I am working on this java project for my homework for a beginner java class. I have it all done, with one issue. When the user inputs an invalid entry instead of just looping back up and starting over it goes down to the "Enter degrees in Fahrenheit" section. I am sure it is a simple fix. Can anyone help me with this issue? I have attached my code. Thank you for the help

    //Brett Olsen, Assignment 3, Temperature Converter
    //13 July 2014 CMIS 141
    import java.util.Scanner;
     
    //Welcome Screen 
    public class TemperatureConverter {
    	public static void main(String[] args){
    		System.out.print("Welcome to THE TEMPERATURE CONVERTER");
    		temperatureConverter();
    	}
     
    	public static void temperatureConverter(){
     
    		Scanner user_input = new Scanner(System.in);
     
    		char selection;		
    	//Menu 	
    		do {		
    			System.out.println("\n");
    			System.out.println("F/f\tfrom Fahrenheit to Celsius");
    			System.out.println("C/c\tfrom Celcius to Fahrenheit");
    			System.out.println("E/e\tQuit");
    			System.out.println("\nYour Option:  ");
     
    			selection = user_input.next().charAt(0);
    			selection = Character.toUpperCase(selection);
     
    	//Invalid responses		
    			boolean invalid = ((selection != 'F' && selection != 'C' && selection != 'E'));
     
    			if (invalid)
    				System.out.print("Invalid Selection");
     
    	//Selection to convert Fahrenheit to Celcius		
    			if (selection !='C' && selection !='E') {
    				System.out.println("Enter a degree in Fahrenheit:  ");
    				double fahrenheit = user_input.nextDouble();
    				System.out.println(fahrenheit + " Fahrenheit is " + ((fahrenheit -32)*(5/9.0)) + " celsius.");	
     
    	//Selection to convert Celcius to Fahrenheit		
    			} else if (selection!='F' && selection !='E') {
    				System.out.println("Enter a degree in Celcius:  ");
    				double celcius = user_input.nextDouble();
    				System.out.println(celcius + " Celcius is " + ((celcius * 9/5) + 32) + " Fahrenheit.");
     
    	//Selection to Quit program		
    			} else if (selection !='F' && selection !='C') {
    				System.out.println("Thank you have a great day!");
    			}	
    		}	while (selection != 'E' && selection != 'e');
     
     
    	}
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help looping back to top if invalid

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Your program is organized to get a menu selection then execute based on the selection. I believe you're asking how to repeat the menu if the user enters an invalid menu option. A way to accomplish this is to enclose the menu and the user's response in another while or do/while statement of the form:
    boolean valid = false;
    while ( !valid )
    {
        // present menu
     
        // get user input
     
        // if user input valid, valid = true
     
        // else valid = false
    }
    If you were using multiple methods or methods beyond the main(), I would suggest presenting the menu and getting the user's input using another method, but I suspect you're not there yet.

Similar Threads

  1. Writing to a txt file from the top.
    By sakonpure6 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 21st, 2013, 08:39 PM
  2. Why are my flowers stacked on top of each other?
    By Techstudent in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 16th, 2013, 11:00 AM
  3. top 10 high score table
    By wacka in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 15th, 2013, 09:03 AM
  4. Can anybody help me fix this, what I am supposed to do is at the top
    By peplo1214 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 17th, 2012, 07:05 AM