Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to create a small piece of code where it Interviews you by asking your name and age then asks if this is the correct information by using a yes/no concept. I have just have a small amount of errors i would like to ask, i have included them as comments in the piece of code. I have researched and try various ways to fix them but still have not found a solution.

What I have tried:

Java
import java.util.*;
// I have created a simple Interviewer where it asks for name and age and ask if it is correct with a yes/no function.
// I have compiled and ran this in Eclipse IDE, i have not encountered any critical errors but i have with yellow warnings.
// I'm struggling with using the Scanner utility.
// Apologises with structure, new to this.
public class scanner {
	
	public static void main(String[] args) {
		Scanner userInput = new Scanner(System.in); // Yellow warning, is this right? Says 'convert local variable to field'*
		
		String version = "v1";
		String wlcmsg;

		
		wlcmsg = "Welcome to Interviwer " + version + ".";
		System.out.print(wlcmsg); // Welcome message.
		System.out.println(" Please press 'enter' to continue...");
		
		try {
			System.in.read();
		}
		catch (Exception e) { //Works fine, Press the 'enter' key and takes me to next step.
			
		}
			for(int clear = 0; clear <= 50; clear++)
			  {
			     System.out.println(" ");  // Prints 50 Lines of spaces, Using this as clearing screen but not sure if this is recommended. Any easier way or other code to do the same concept? **
		        }
			
			System.out.println("\n");
			String blank = userInput.nextLine(); // I AM NOT SURE I NEED THIS? When i remove line 27 & 28, userInput does not work for line 29/30.
			System.out.println(blank + "Please enter your name: ");
			String name = userInput.nextLine();
		
		for(int clear = 0; clear <= 50; clear++)
		  {
		     System.out.println(" ") ;  //**
	        }
		
			System.out.print("Hello " + name + ", I am your personal interviewer " + version + "\n" + "So, How old are you?");
			int age = userInput.nextInt();
		
		for(int clear = 0; clear <= 50; clear++)
			
		  {
		     System.out.println(" ") ; //**
		  }	

			System.out.println("So your name is " + name + " and you are " + age + " years old." + "\n" + "Is this correct?");
			Scanner options = new Scanner(System.in); //*
			
		if(options.next().equalsIgnoreCase("y")||options.next().equalsIgnoreCase("options")) { // The yes option seems to work but no and the invalid characters does not seem to print out. Have i went wrong here?
		    System.out.println("This will be fun");
		} else if(options.next().equalsIgnoreCase("n")||options.next().equalsIgnoreCase("no")) {
		    System.out.println("Maybe next time");
		} else { 
		System.out.println("Invalid character");
		}
	}
}
Posted
Comments
Maciej Los 27-Feb-18 16:15pm    
What's your question?
Richard Deeming 27-Feb-18 17:19pm    
A guess for the last one: options.next() reads and consumes the next character from the input. If the user types "n", the first call will return "n", and the subsequent calls will wait for the user to type something else.

You need to call options.next() once; store the returned value in a variable; and then test that variable for the possible responses.
Member Hemal 21-Mar-18 5:15am    
Here is your solution .....




import java.util.*;

public class scanner
{
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in);

String version = "v1";
String wlcmsg;


wlcmsg = "Welcome to Interviwer " + version + ".";
System.out.print(wlcmsg);
System.out.println(" Please press 'enter' to continue...");

try
{
System.in.read();
}
catch (Exception e)
{

}
for(int clear = 0; clear <= 50; clear++)
{
System.out.println(" ");
}

System.out.println("\n");
String blank = userInput.nextLine();
System.out.println(blank + "Please enter your name: ");
String name = userInput.nextLine();

for(int clear = 0; clear <= 50; clear++)
{
System.out.println(" ") ;
}

System.out.print("Hello " + name + ", I am your personal interviewer " + version + "\n" + "So, How old are you?");
int age = userInput.nextInt();

for(int clear = 0; clear <= 50; clear++)
{
System.out.println(" ") ;
}

System.out.println("So your name is " + name + " and you are " + age + " years old." + "\n" + "Is this correct?");
Scanner options = new Scanner(System.in);

String ans = options.next();

if(ans.equalsIgnoreCase("y")||ans.equalsIgnoreCase("options"))
{

System.out.println("This will be fun");
}
else if(ans.equalsIgnoreCase("n")||ans.equalsIgnoreCase("no"))
{
System.out.println("Maybe next time");
}
else
{
System.out.println("Invalid character");
}
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900