This content has been marked as final. Show 10 replies
firstName = scan.nextInt();Read the API for the Scanner class instead of just throwing random method calls and hoping for a miracle.
It should be obvious that you're not asking the user to type a number, so why you'd be tempted to invoke nextInt() is beyond me.
It also appears you don't know what the basic types are, if you're trying to declare firstName as an int. Please start with the tutorials.
I do know what they mean, I know int are for numbers but i've tried to declare them as strings but then scan.nextString isn't a method so what do I do ?
That was stupid indeed, but all recent projects have been with numbers and i'm still not so clear on using strings.
And I couldn't really understand the relevant method in your link...
I do know what they mean, I know int are for numberssigh
but i've tried to declare them as strings but then
scan.nextString isn't a method so what do I do ?
Scanner.next() returns a String
And I couldn't really understand the relevant methodIt isn't rocket science. On the left pane of the page if you follow the first link, is a list of classes. Find the Scanner class. Click it. On the right pane it shows the documentation of that class.
in your link...
Here's the complete soulution for you.
public class Names
public static void main (String args)
String firstName, lastName;
Random ran = new Random();
num = ran.nextInt(100);
Scanner scan = new Scanner (System.in);
System.out.print ("Please enter your first name. ");
firstName = scan.nextLine();
System.out.print ("Please enter your last name. ");
lastName = scan.nextLine();
lastName = lastName.substring(0, 5);
System.out.println("This is the output: " + firstName.subSequence(0,1) + lastName + num);