I'm new to Java and I have to write a program to get user details which appear like this:
Author’s Details **************** Name: J. Beans YOB: 1969 Age: 41 Book Details ************ Title: *Wonderful Java* ISBN: *978 0 470 10554 9* Publisher: *Wiley*
This is what I've done but it does not work, can anyone help me to find out the problem ?
import java.util.Scanner ;
public class UserDetails
{
public static void main(String args[]开发者_运维知识库)
{
System scan = new Scanner(System.in);
input sname, fname, born, title, isbn, publisher;
System.out.print("Please enter author's surname:");
sname = input.nextLine();
System.out.print("Please the initial of author's first name:");
fname = input.nextLine();
System.out.print("Please enter the year the author was born:");
born = input.nextLine();
System.out.print("Please enter the author's book title:");
title = input.nextLine();
System.out.print("Please enter the book's ISBN:");
isbn = input.nextLine();
System.out.print("Please enter the publisher of the book:");
publisher = input.nextLine;
System.out.println("Author's detail");
System.out.println("**********************");
System.out.println("Name:" + fname + sname);
System.out.println("YOB:" + born);
System.out.println("Age" + born);
System.out.println("Book Details");
System.out.println("**********************");
System.out.println("Title:" + "*" + title + "*");
System.out.println("ISBN:" + "*" + isbn + "*");
System.out.println("Publisher:" + "*" + publisher + "*");
}
}
hi you just wrongly use classes.Your code shoul be
import java.util.Scanner;
public class UserDetails {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
String sname, fname, born, title, isbn, publisher;
System.out.print("Please enter author's surname:");
sname = scan.nextLine();
System.out.print("Please the initial of author's first name:");
fname = scan.nextLine();
System.out.print("Please enter the year the author was born:");
born = scan.nextLine();
System.out.print("Please enter the author's book title:");
title = scan.nextLine();
System.out.print("Please enter the book's ISBN:");
isbn = scan.nextLine();
System.out.print("Please enter the publisher of the book:");
publisher = scan.nextLine();
System.out.println("Author's detail");
System.out.println("**********************");
System.out.println("Name:" + fname + sname);
System.out.println("YOB:" + born);
System.out.println("Age" + born);
System.out.println("Book Details");
System.out.println("**********************");
System.out.println("Title:" + "*" + title + "*");
System.out.println("ISBN:" + "*" + isbn + "*");
System.out.println("Publisher:" + "*" + publisher + "*");
}
}
Among other things, change this:
System scan = new Scanner(System.in);
input sname, fname, born, title, isbn, publisher;
to this:
Scanner input = new Scanner(System.in);
String sname, fname, born, title, isbn, publisher;
See also:
Java language:
- How to declare variables
API:
java.lang.String
java.util.Scanner
If you are really serious about learning how to program, follow everyone's recommendation and:
- Read books.
- Study tutorials.
- Use an IDE (Eclipse is a good one, and it's free to download)
- Practice a lot.
- Ask questions a lot.
Your code is full of syntax and semantic errors. This works:
import java.util.Scanner ;
public class UserDetails {
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
String sname, fname, born, title, isbn, publisher;
System.out.print("Please enter author's surname:");
sname = scan.nextLine();
System.out.print("Please the initial of author's first name:");
fname = scan.nextLine();
System.out.print("Please enter the year the author was born:");
born = scan.nextLine();
System.out.print("Please enter the author's book title:");
title = scan.nextLine();
System.out.print("Please enter the book's ISBN:");
isbn = scan.nextLine();
System.out.print("Please enter the publisher of the book:");
publisher = scan.nextLine();
System.out.println("Author's detail");
System.out.println("**********************");
System.out.println("Name:" + fname + sname);
System.out.println("YOB:" + born);
System.out.println("Age" + born);
System.out.println("Book Details");
System.out.println("**********************");
System.out.println("Title:" + "*" + title + "*");
System.out.println("ISBN:" + "*" + isbn + "*");
System.out.println("Publisher:" + "*" + publisher + "*");
}
}
You are close, but you have a couple errors and a typo.
System scan = new Scanner(System.in); This should be: Scanner scan = new Scanner(System.in); What you are doing here is creating a new object reference of type scanner named scan -- not System.
Next problem: input sname, fname, born, title, isbn, publisher; This is again, a declaration issue with types. Each of these variables is a string reference -- should declare this as; String sname, fname, born, title, isbn, publisher;
Lastly, you have a typo: publisher = input.nextLine; You forgot the () that indicate this is a function call. Should be: publisher = input.nextLine();
There may be some other more subtle issues, but this should get your code to compile and run at least :).
精彩评论