I have this assignment in school, where I have to import a scanner and write a method. What am I doing wrong?
public static void main(String[] args)
{
applicationDate();
}
public static void applicationDate()
{
Scanner input = new Scanner(System.in);
System.out.println("On what day of the month you applied?");
int day = input.nextInt();
System.out.println("What is the name of the month in wich you applied?");
String month = input.nextLine();
System.out.println("During wich year you applied?");
int year = input.nextInt();
System.out.print("Your application date is" + month + " ", + year + "!");
}
It comes with this error when I compile the thing,
EX20.java:27: cannot find symbol
print only takes one String argument - move the comma inside the String
System.out.print("Your application date is" + month + " ," + year + "!");
^
Remove comma and add it in between double quotes like below.
System.out.print("Your application date is" + month + " ,"+ year + "!");
First add import java.util.Scanner; to your file.
As this line is missing you must be getting error as
error: cannot find symbol
Scanner input = new Scanner(System.in);
^
then
Remove extra comma from the last print statement.
System.out.print("Your application date is" + month + " ,"+ year + "!");
Related
This is my first week of coding and I am unsure of why when I go to ask "Do you enjoy life", it forces me to type the answer on the next line.
import java.util.Scanner;
public class messing {
public static void main(String[] args) {
Scanner keyboardInput = new Scanner(System.in);
System.out.print("Enter Your Name: ");
String name = keyboardInput.nextLine();
System.out.print("How old are you: ");
int age = keyboardInput.nextInt();
System.out.println(name + " you are " + age + "?" + " Do you enjoy life: " );
String enjoy = keyboardInput.next();
System.out.print(enjoy + "?");
}
}
This is because the line of code that displays the question (line 13) uses the System.out.println() function, which, in addition to the text displayed, appends a newline character. This sends any further text onto the next line in the console. Replacing the function with the System.out.print() function removes this behavior.
That's because you're using System.out.println when asking the user if they enjoy life.
Simply change the function to System.out.print like the rest so you can input your answer in the same line.
System.out.print(name + " you are " + age + "?" + " Do you enjoy life: " );
I am stuck in the U of Helsinki Java MOOC:
Create a program that can be used to add two integers together. In the beginning, the user is asked to give two integers that are to be summed. The program then prints the formula that describes the addition of the numbers.
Example output:
Give the first number:
5
Give the second number:
4
5 + 4 = 9
I am trying to get the system to print " "first" + "second" is "result". For some reason I am stumped on this otherwise easy question. My code is always throwing an error. What am I doing wrong in the last line?
import java.util.Scanner;
public class AdditionFormula {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// write your program here
System.out.println("Give the first number: ");
int first = Integer.valueOf(scanner.nextLine());
System.out.println("Give the second number: ");
int second = Integer.valueOf(scanner.nextLine());
//System.out.println("first" " + " Integer.valueOf(first) + Integer.valueOf(second));
System.out.println(first + " + " + second " = " + (first + second));
}
The code you provided does not compile
Change to
System.out.println(first + " + " + second + " = " + (first + second));
i have a question. When i run the code i get an error on the inputting the conversion rates. Could someone please help me in the right direction? I've copied part of the code and the error message.
Code:
public static void travelTimeAndBudget() {
Scanner input = new Scanner(System.in);
System.out.print("How many days are you going to spend travelling? ");
int days = input.nextInt();
System.out.print("How much money, in USD, are you planning to spend on your trip? ");
int usd = input.nextInt();
System.out.print("What is the three letter currency symbol for your travel destination? ");
String currency = input.next();
double currencyConvert;
System.out.print("How many " + currency + " are there in 1 USD? ");
currencyConvert = input.nextDouble();
double currencyTotal = (currencyConvert * (double)usd);
double currencyDay = (currencyTotal / (double)days);
System.out.println("If you are travelling for " + days + " days that is the same as" + (days * 24) + " or " + (days * 360) + " minutes");
System.out.println("If you are going to spend $" + usd + " USD that means per day you can spend up to $" + ((double)usd / days) + " USD");
System.out.println("Your total budget in " + currency + " is " + currencyTotal + " " + currency + ", which per day is " + currencyDay + currency);
}
Output + error:
How many days are you going to spend travelling? 14
How much money, in USD, are you planning to spend on your trip? 2300
What is the three letter currency symbol for your travel destination? MXC
How many MXC are there in 1 USD? 19.8
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at TripPlanner.travelTimeAndBudget(TripPlanner.java:33)
at TripPlanner.main(TripPlanner.java:8)
Any help is very much appreciated.
try this :
Scanner scanner = new Scanner(System.in).useLocale(Locale.US);
A useful tip for tracing bugs is to check the call stack. At the bottom of the error message you have:
at TripPlanner.travelTimeAndBudget(TripPlanner.java:33)
at TripPlanner.main(TripPlanner.java:8)
This tells you lines where your exception is being caught. Line 33 of TripPlanner.java is where to start debugging your code
I'm trying to find out why the %.2f declaration when outputting a decimal isn't working in my code, I've checked other similar questions but I can't seem to locate the issue in the specific logic error I'm receiving. When I go to compile my program it compiles fine, I go to run it and everything outputs fine until I get to the final cost where I'm trying to only display that decimal value with 2 decimal places.
I get an exception in thread "main"
Java.util.illegalformatconversionexception f! = Java.lang.string
At java.util.Formatter$formatspecifier.failconversion(Unknown Source)
At java.util.Formatter$formatspecifier.printFloat(Unknown Source)
At java.util.Formatter.format(Unknown Source)
At java.io.printstream.format(Unknown Source)
At java.io.printstream.printf(Unknown Source)
At Cars.main(Cars.java:27)
Here is my code:
import java.util.Scanner;
public class Cars
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
int carYear, currentYear, carAge;
double costOfCar, salesTaxRate;
double totalCost;
String carModel;
System.out.println("Please enter your favorite car model.");
carModel = input.nextLine();
System.out.println("Please enter the year of the car");
carYear = input.nextInt();
System.out.println("Please enter the current year.");
currentYear = input.nextInt();
carAge = currentYear - carYear;
System.out.println("How much does the car cost?");
costOfCar = input.nextDouble();
System.out.println("What is the sales tax rate?");
salesTaxRate = input.nextDouble();
totalCost = (costOfCar + (costOfCar * salesTaxRate));
System.out.printf("The model of your favorite car is" + carModel + ", the car is" + " " + carAge + " " + " years old, the total of the car is" + " " + "%.2f",totalCost + " " + " dollars.");
}
}
I'm not exactly sure what's causing the issue.
Try:
System.out.printf("The model of your favorite car is %s, the car is %d years old, the total of the car is %.2f dollars.", carModel, carAge, totalCost);
Or the more readable:
System.out.printf("The model of your favorite car is %s," +
" the car is %d years old," +
" the total of the car is %.2f dollars.",
carModel, carAge, totalCost);
It's because %.2f is replaced with the entire second argument in that method call. The problem is that by specifying f in %.2f, you are saying that the second argument is a float or double. The second argument in this case is totalCost + " " + " dollars." which evaluates to a string.
To fix this problem, you need to make the second argument be a float or double. This can be achieved by moving + " " + " dollars." from the end of the second argument to the end of the first argument, like so:
System.out.printf("The model of your favorite car is" + carModel + ", the car is" + " " + carAge + " " + " years old, the total of the car is" + " " + "%.2f" + " " + " dollars.",totalCost);
You can also remove many of the unnecessary concatenations from that line, resulting in this:
System.out.printf("The model of your favorite car is" + carModel + ", the car is " + carAge + " years old, the total of the car is %.2f dollars.", totalCost);
The variable has to go as a parameter to the System.out.printf() function. The "%.2f" will be replaced by the double value that is passed as the second parameter.
For Example:
System.out.printf("The value is %.2f", value);
The same thing is true for other variable types and for multiple variables,
String str = "The value is: ";
double value = .568;
System.out.printf("%s %.2f", str, value);
This will output: "The value is: .57"
I get this error; bad operand types for binary operator '-'
All other operations work...when I leave out the subtraction via comments like // and /* */; can someone help?
This is the code by the way; the exception is on the subtraction line.
public class Calculator {
/*
*use 'javac Calculator.java' to compile;
*use 'jar cvf Calculator.jar Calculator.class' for jar;
*use 'java Calculator' to run;
*/
public static void main(String []args) {
String NewName
Scanner user_input = new Scanner( System.in );
System.out.println("Type your name please.");
NewName = user_input.next();
System.out.println("");
System.out.println("Hello " + NewName + ".");
System.out.println("I am Hunter's java calculator program.");
System.out.println("");
//mathematical input
String operator;
float cal1, cal2;
System.out.println("Type a Number...");
cal1 = user_input.nextFloat();
System.out.println("");
System.out.println("Type another Number...");
cal2 = user_input.nextFloat();
System.out.println("");
Scanner opt = new Scanner(System.in);
System.out.println("Enter an operator");
operator = opt.next();
//operation decisions
if (operator.equals("+")){
System.out.println("The answer is " + cal1+cal2 + ".");
}
if (operator.equals("-")){
System.out.println("The answer is " + cal1-cal2 + ".");
}
if (operator.equals("/")){
System.out.println("The answer is " + cal1/cal2 + ".");
}
if (operator.equals("*")){
System.out.println("The answer is " + cal1*cal2 + ".");
}
}
}
You need parenthesis:
System.out.println("The answer is " + (cal1-cal2) + ".");
Otherwise what you have is treated as
System.out.println(("The answer is " + cal1) - (cal2 + "."));
which is invalid since you can't subtract strings.
Why don't you have an error with the other operators? Well, * and / have higher precedences, so those are working as expected. +, on the other hand, is overloaded to concatenate strings:
System.out.println("The answer is " + cal1+cal2 + "."); // concatenates, doesn't add
For example, if call1 is 1 and call2 is 2, the result will be:
The answer is 12.
which isn't what you want. Again, this can be solved with parenthesis.