Can anyone tell me where I am going wrong with the last two lines to convert age
ID: 3638178 • Letter: C
Question
Can anyone tell me where I am going wrong with the last two lines to convert age to age in one year and age in 5 years?public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String name;
System.out.println("Please enter your first name:");
String fname = in.nextLine();
System.out.println("Please enter your last name:");
String lname = in.nextLine();
System.out.println("Please enter your age:");
int age;
String fullname=fname + lname;
System.out.println("Hello" + fname + lname);
if(fname.equals(lname))
System.out.println("Hello" + fullname +", your first and last name are the same");
else
System.out.println("Hello" + fullname +", your first and last name are different");
System.out.println("The length of your first name is:"+ (fname.length()));
System.out.println("The length of your full name is:"+ (fname.length()+lname.length()));
System.out.println("Inital in first name is" + fname.charAt(0) +" Ascii value is" + (int) fname.charAt(0));
System.out.println("Initial in last name is" + lname.charAt(0) +" Ascii value is" + (int) lname.charAt(0));
System.out.println("Your current age is" + (int) age);
System.out.println("In one year you will be" + age);
System.out.println("In five years you will be" + age+4);
}
}
Explanation / Answer
Below is corrected code public static void main(String[] args) { Scanner in = new Scanner(System.in); String name; System.out.println("Please enter your first name:"); String fname = in.nextLine(); System.out.println("Please enter your last name:"); String lname = in.nextLine(); System.out.println("Please enter your age:"); int age= in.nextInt(); String fullname=fname + lname; System.out.println("Hello" + fname + lname); if(fname.equals(lname)) System.out.println("Hello" + fullname +", your first and last name are the same"); else System.out.println("Hello" + fullname +", your first and last name are different"); System.out.println("The length of your first name is:"+ (fname.length())); System.out.println("The length of your full name is:"+ (fname.length()+lname.length())); System.out.println("Inital in first name is" + fname.charAt(0) +" Ascii value is" + (int) fname.charAt(0)); System.out.println("Initial in last name is" + lname.charAt(0) +" Ascii value is" + (int) lname.charAt(0)); System.out.println("Your current age is" + (int) age); System.out.println("In one year you will be" + (age+1)); System.out.println("In five years you will be" + (age+5)); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.