This is my code. The program won't give me the last print line "Thank you for using the Basic user Interface program."
public class nameClass {
public static void main(String[] args) {
String input;
String name;
int age;
double mileage;
displayApplicationInformation();
displayDivider("Start Program");
TerminateApplication();
// process name
displayDivider("Get Name");
name = getInput("name");
System.out.println("Your name is: " + name);
// Process age
displayDivider("Get Age");
input = getInput("Your age");
age = Integer.parseInt(input);
System.out.println("Your age is: " + age);
// Process Mileage
displayDivider("Get Mileage");
input = getInput("Your MPG");
mileage = Double.parseDouble(input);
System.out.println("Your car MPG is: " + mileage);
}// end of main
public static void displayApplicationInformation()
{
System.out.println("Welcome to the Basic User Interface Program");
}// end of displayApplicaionInformation
public static void displayDivider(String outputTitle) {
System.out.println("*********" + outputTitle + "********");
}// end of displayDvider
public static String getInput(String inputType)
{
String input = "";
input = JOptionPane.showInputDialog("Enter the " + inputType);
return input;
}
public static void TerminateApplication()
{
System.out.println("Thank you for using the Basic User Interface program");
return;
}
}// end of MainClass
You have to actually call the method TerminateApplication;
System.out.println("Your car MPG is: " + mileage);
TerminateApplication();
Simple, call TerminateApplication.
You are doing it on the 9th line of main().
Here, check this out:
displayDivider("Get Mileage");
input = getInput("Your MPG");
mileage = Double.parseDouble(input);
System.out.println("Your car MPG is: " + mileage);
//add this...
TerminateApplication();
Hope this helps!
Related
I am making a game and a the end of the game I want it to call the user by the name that they put in,, this is the code I have.
private static final Scanner console = new Scanner(System.in);
public static void main(String[] args) {// follow the prompts.//
System.out.println("Hello user! what is your name? ");
String Name = console.nextLine();
System.out.println("Really? " + Name + " is too weird to be a real name.");
confirmation();
Mascot();
System.out.println("Thank you for playing the demo");
console.close();
}
public static void confirmation() {
System.out.print("is that REALLY your name? (type Y/N) ");
String yN = console.nextLine();
String a = yN;
if (a.toLowerCase().contains("y")) {
System.out.println("I still dont belive you, so you will have to answer 3 riddles before you can continue to the game");
} else {
calledIt();
}
}
public static void calledIt() {
System.out.println("I knew it!");
System.out.print("whats your real name? ");
String realName = console.nextLine();
System.out.println(
"" + realName + " sounds like a real name, but you lied the first time so you will need to answer riddles 3 to continue to the game");
}
public static boolean Mascot() {
System.out.println("what Is our school mascot?");
String b = console.nextLine();
if (b.toLowerCase().contains("tiger")) {
System.out.println("Good, next riddle.");
System.out.println("What runs around the whole yard without moving?");
String c = console.nextLine();
if (c.toLowerCase().contains("fence")) {
System.out.println("Good, next riddle.");
System.out.println("What goes on four feet in the morning, two feet at noon, and three feet in the evening? ");
String d = console.nextLine();
if (d.toLowerCase().contains("man")) {
System.out.println("You, have sucsefully passed the third riddle");
return true;
} else {
System.out.println("You have failed");
return false;
}
} else {
System.out.println("You have failed");
return false;
}
} else {
System.out.println("You have failed");
return false;
}
}
I want for it to at the end print * user's name*, you have successfully passed the third riddle.
but it needs to be able to weather the first name was kept, or if this sequence was used.
public static void calledIt() {
System.out.println("I knew it!");
System.out.print("whats your real name? ");
String realName = console.nextLine();
System.out.println(
"" + realName + " sounds like a real name, but you lied the first time so you will need to answer riddles 3 to continue to the game");
}
and if it has been activated it needs to use the new name.
Change return type of calledIt() to String and return realName from this method
Change return type of confirmation() to String. Initialize a String (String name = null). In the else part, assign the value returned from calledIt() to this string (String name = calledIt()). Return name.
In main, if the value returned from confirmation() is not null, update Name with this new value.
Pass the Name as input to Mascot method. For this, you have to update the Mascot method to accept a String as input.
You can pass the variable into confirmation() and calledIt() like this
public static void main(String[] args) {// follow the prompts.//
System.out.println("Hello user! what is your name? ");
String Name = console.nextLine();
System.out.println("Really? " + Name + " is too weird to be a real name.");
confirmation(Name);
Mascot();
System.out.println("Thank you for playing the demo");
console.close();
}
public static void confirmation(String name) {
System.out.print("is that REALLY your name? (type Y/N) ");
String yN = console.nextLine();
String a = yN;
if (a.toLowerCase().contains("y")) {
System.out.println("I still dont belive you, so you will have to answer 3 riddles before you can continue to the game");
} else {
calledIt(name);
}
}
public static void calledIt(String realName){
System.out.println("I knew it!");
System.out.print("whats your real name? ");
System.out.println(
"" + realName + " sounds like a real name, but you lied the first time so you will need to answer riddles 3 to continue to the game");
}
You could do the following change:
public static void main(String[] args) { // follow the prompts.//
System.out.println("Hello user! What is your name? ");
String name = console.nextLine();
System.out.println("Really? " + name + " is too weird to be a real name.");
System.out.print("Is that REALLY your name? (type Y/N) ");
String yN = console.nextLine();
String a = yN;
if (a.toLowerCase().contains("y")) {
System.out.println("I still don't believe you, so you will have to answer 3 riddles before you can continue to the game");
} else {
System.out.println("I knew it!");
System.out.print("Whats your real name? ");
name = console.nextLine();
System.out.println(
"" + name + " sounds like a real one, but you lied the first time so you will need to answer riddles 3 to continue to the game");
}
mascot(name);
System.out.println("Thank you for playing the demo");
console.close();
}
public static boolean mascot(String name) {
System.out.println("what Is our school mascot?");
String b = console.nextLine();
if (b.toLowerCase().contains("tiger")) {
System.out.println("Good, next riddle.");
System.out.println("What runs around the whole yard without moving?");
String c = console.nextLine();
if (c.toLowerCase().contains("fence")) {
System.out.println("Good, next riddle.");
System.out.println("What goes on four feet in the morning, two feet at noon, and three feet in the evening? ");
String d = console.nextLine();
if (d.toLowerCase().contains("man")) {
System.out.println(name + ", you have successfully passed the third riddle");
return true;
} else {
System.out.println("You have failed");
return false;
}
} else {
System.out.println("You have failed");
return false;
}
} else {
System.out.println("You have failed");
return false;
}
}
Okay so i created this program which is unfinished that allows you to insert a students name and test score into a string. But my probably is, im trying to use if statements so that the user is able to type in a number and choose what they wish to do.
Im trying to have it so after the user has inserted the name and test score, they can choose to have the name and test score printed out, or they can quit or insert another name but i cant because the string is not set globally.
does anyone know how to do this? Thank You,
package week14;
import java.util.Scanner;
public class Task4 {
private static Scanner input;
private static Scanner input2;
public static void main (String [] args){
input2 = new Scanner (System.in);
System.out.println("Student Mark Program");
System.out.println("\nHere are your options:");
System.out.println("\n1. Insert Student name and mark");
System.out.println("2. Quit");
int choice = input2.nextInt();
System.out.println();
if (choice == 1){
opt1();
}
else if (choice == 2){
quit();
}
}
public static void opt1(){
input = new Scanner(System.in);
String firstname;
String surname;
int score1;
System.out.print("Enter the students first name: ");
firstname = input.next();
System.out.print("Enter the students surname: ");
surname = input.next();
String full_name;
full_name = firstname + " " + surname;
System.out.println("Please enter students score: ");
score1 = input.nextInt();
System.out.println("Name : " + full_name);
System.out.println("Score: " + score1);
System.out.println();
System.out.println("\nHere are your options:");
System.out.println("\n1. Insert Another Student name");
System.out.println("2. Get student name");
System.out.println("3. Get student name and mark");
System.out.println("4. Quit");
int choice = input.nextInt();
System.out.println();
if (choice == 1){
opt6();
}
else if (choice == 2){
opt2();
}
else if (choice == 3){
opt3();
}
System.out.println();
input.close();
}
public static void opt2(){
System.out.println("Name : " + full_name);
}
public static void opt3(){
}
public static void opt4(){
}
public static void opt5 (){
}
public static void opt6 (){
}
public static void quit(){
System.out.println("You have quit the program");
System.exit(0);
}
}
Put this static String full_name; under where you declare private static Scanner input2; in your class. Not necessarily good programming practice to use global variables, but for your purposes it will likely get the job done.
Well, I would initialize a new global variable set your full_name to and return it as an accessor method.
public static String getName()
{
return (nameOfString);
}
Also, I see that you want to make an other command to change your name and you could do that with a settor method.
public static void setName(String newName)
{
originalString = newName;
}
This is for personal knowledge of how this works, is not for school
Program requirements - Enter 2 Names. Have the program find the assigned values with the names and print the average between the two people.
I an not sure how to get the Scanner to take the input and go to the class to make it start processing. For example, in the main method if I sysout print a, it should display the string inside the method getName.
import java.util.Scanner;
public class RainFallApp {
public static void main(String[] args) {
rainfall a = new rainfall();
rainfall b = new rainfall();
System.out.println(a);
// System.out.print("Please enter month one: ");
// Scanner = new Scanner(System.in);
// rain1 = aRain;
// System.out.print("Please enter month two: ");
// Scanner = new Scanner(System.in);
//
// int average = (rain1 + rain2) / 2;
// System.out.println("The average rainfall for " + var +
"and " + var2 +"is: " + average);
}
}
class rainfall {
String rainamt;
String Rain_Amount;
Scanner input = new Scanner(System.in);
String rainMonth = input.nextLine();
String rainAmount(String rainMonth) {
Rain_Amount = getName(rainMonth);
return Rain_Amount;
}
private String getName(String rainMonth) {
if (rainMonth.equals("Jan")) {
rainamt = "3.3";
}
else if (rainMonth.equals("Feb")) {
rainamt = "2.2";
}
else {
System.out.println("Not a valid month name");
}
return rainamt;
}
}
You only need to say Scanner scanner = new Scanner(System.in); once. Then you can use the scanner's nextLine() method to input data. It returns a string, so be sure to store the result in a variable.
I completed my program
import java.util.Scanner;
public class RainFallApp {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter the first month: ");
String aMonth = input.nextLine();
System.out.print("Please enter the second month: ");
String bMonth = input.nextLine();
rainfall aRainfall = new rainfall();
String aName = aRainfall.rainAmount(aMonth);
Double aAmount = Double.parseDouble(aName);
rainfall bRainfall = new rainfall();
String bName = bRainfall.rainAmount(bMonth);
Double bAmount = Double.parseDouble(bName);
double Avg = (aAmount + bAmount) / 2;
System.out.println("\nIn the month of " + aMonth + " it had "
+ aAmount + " inches of rain.");
System.out.println("In the month of " + bMonth + " it had "
+ bAmount + " inches of rain.");
System.out.println("The average rainfall between the two months is: " + Avg);
}
}
class rainfall {
private String Rain_Amount;
String rainAmount(String rainMonth) {
Rain_Amount = getAmount(rainMonth);
return Rain_Amount;
}
private String getAmount(String rainMonth) {
if (rainMonth.equals("Jan")) {
Rain_Amount = "3.3";
}
else if (rainMonth.equals("Feb")) {
Rain_Amount = "2.3";
}
else {
System.out.println("Not a valid month name");
}
return Rain_Amount;
}
}
our assignment objective is to implement 3 students grades in two different classes (Student, grades) and find the average
here is what i have so far,
public class Program01
{
public static void main(String[] args)
{
Student bob, john, matt;
Grades grades;
grades = new Grades();
double bobgrade, johngrade, mattgrade;
bob = new Student();
john = new Student();
matt = new Student();
bob.setup();
john.setup();
matt.setup();
bob.display();
john.display();
matt.display();
bobgrade = bob.overallGrade();
johngrade = john.overallGrade();
mattgrade = matt.overallGrade();
grades.average(bobgrade, johngrade, mattgrade);
System.out.println("The overall grade for the class is: " + grades.theSectionAverage);
}
public class Student
{
Grades grades;
String fullName, firstName, lastName, name;
int studentProgramGrade, studentExamGrade;
public void setup(){
setName();
setGrades();
}
public void setName()
{
System.out.print("Please, enter the student's name in the form of Doe, John or Smith, Jane:");
fullName = Keyboard.readString();
firstName = fullName.substring(fullName.indexOf(" ") + 1, fullName.length());
lastName = fullName.substring(0, fullName.indexOf(","));
name = firstName + " " + lastName;
}
public void setGrades()
{
studentExamGrade = grades.setupExam(name);
studentProgramGrade = grades.setupProgram(name);
}
public void display()
{
System.out.println(name + " " + grades.display());
}
public double overallGrade()
{
final double PROGRAM_WEIGHT = 0.40;
final double EXAM_WEIGHT = 1 - PROGRAM_WEIGHT;
double theOverallGrade;
theOverallGrade = studentProgramGrade * PROGRAM_WEIGHT + studentExamGrade * EXAM_WEIGHT;
return theOverallGrade;
}
}
public class Grades {
int programGrade, examGrade;
double theSectionAverage;
public int setupExam(String studentname)
{
System.out.print("Please, enter the exam grade for " + studentname + ":");
examGrade = Keyboard.readInt();
return examGrade;
}
public int setupProgram(String studentname)
{
System.out.print("Please, enter the program grade for " + studentname + ":");
programGrade = Keyboard.readInt();
return programGrade;
}
public String display()
{
return programGrade + " " + examGrade;
}
public double average(double bobgrade, double johngrade, double mattgrade)
{
theSectionAverage = bobgrade + johngrade + mattgrade / 3;
return theSectionAverage;
}
}
whenever i try to run this, i keep getting this error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Keyboard cannot be resolved
at Student.setName(Student.java:18)
at Student.setup(Student.java:10)
at Program01.main(Program01.java:19)
any help would be greatly appreciated.
I think by Keyboard you want to use Scanner in your program.
include the following line in your file
import java.util.Scanner;
and create an instance of it.
Scanner keyboard = new Scanner(System.in);
Or you have some other API that has a class called Keyboard then import that file into your program.
Note: And by the way Scanner doesn't have a readInt() method, it has only nextInt()
This error may happen when you use Eclipse as IDE and try to run code that doesn't even compile. Check your Problems view in Eclipse, and fix the compilation errors before executing the application.
You seem to be missing the import for the Keyboard utils. Java: Input strings with keyboard class
Try importing it, should do the trick.
P.S: This has already been discussed in another topic.
Use
Scanner sc = new Scanner(System.in);
instead of Keyboards
import java.util.Scanner;
public class cat{
public static void main(String args[]){
System.out.print("Enter a command = ");
double balance = 0;
String a;
//scanner input
Scanner in = new Scanner(System.in);
String command = in.nextLine();
while (in.hasNext()){
if (command.equals("penny")){
balance = balance + 0.01;
System.out.println("balance = " + balance);
}
if (command.equals("nickel")){
balance = balance + 0.05;
System.out.println("balance = " + balance);
}
else {
System.out.println("return" + balance + "to customer");
break;
}
balance++;
}
}
}
I'm trying to create an infinite loop that keeps reading new commands for a vending machine , that may halt only under certain condition - when the input is "return", it prints the current balance in the string "return $XX to customer". (otherwise it keeps adding/subtracting cash to the current balance).
First, I cannot seem to get the if and else part integrated together since both the string commands ("return ~ to customer "& "balance =") appears when I write 'penny'.
Second problem is that my intended infinite command loops just becomes a flow of infinite numbers in my terminal and I can't seem to figure out why.
Don'y know if it's what you're searching to do, but this loops unitil you send the break command:
import java.util.Scanner;
public class cat {
public static void main(String args[]) {
System.out.print("Enter a command = ");
double balance = 0;
String a;
// scanner input
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String command = in.nextLine();
if (command.equals("penny")) {
balance = balance + 0.01;
System.out.println("balance = " + balance);
} else if (command.equals("nickel")) {
balance = balance + 0.05;
System.out.println("balance = " + balance);
} else if (command.equals("break")) {
break;
} else {
System.out.println("return " + balance + " to customer");
}
balance++;
}
}
}
To fix your broken 'if' add yet another else:
else if (command.equals("nickel")){
With java7 you can use switch:
switch(command) {
case "nickel":
....
break;
case "penny":
.....
break;
default:
.....;
}