Hey guys and finaly im posting the 17th Java tutorial,
In this tutorial i will teach you how to make a name variable in one class, and set it to a user input in the other class,
All together this should allow you to type your name and then print "Hello <Your name>"
So to start off, make two classes, test1 and test2
In test 2, keep the "class public test2 {" and the other "}"
But delete everything in test1
Then in test1 import the Scanner
import java.util.Scanner;
and then under that type in the class and the method so it looks like this -
import java.util.Scanner;
class test1{
public static void main(String args[]){
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
In test two which should look like this -
Type the method exept with a argument called name, like so -
public class test2 {
static void simpleMessage(String name){
}
The enter the Hello and the variable "name" (The argument, this also works as a variable for that class)
and to do so start off with the text output for Hello "System.out.println("Hello");"
Then add in the variable for the name by typing " + name" after the hello,
If you dont get it look over this again and look at what it should look like -
public class test2 {
static void simpleMessage(String name){
System.out.println("Hello " + name);
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Now to finish up with the main class "test1"
If you looked at the user input tutorial you will recognise this
Scanner input = new Scanner(System.in);
test2 test2Object = new test2();
If you dont recognise this i would recommend you go and have a look at it, or recap your memory
I wont explain this just to shorten the tutorial a bit.
So now we have to make a text output for the person to see so they know what to do, this will print on the screen "Please enter your name: "
So for this just do basic text output "System.out.println("Please enter your name: ");"
And under that type "String name = input.nextLine();"
So that should look like -
import java.util.Scanner;
class test1{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
test testObject = new test();
System.out.println("Please enter your name: ");
String name = input.nextLine();
}
}
And FINALY you need java to find the class as an object, find the method and fully execute it by typing the following
import java.util.Scanner;
class fish{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
test testObject = new test();
System.out.println("Please enter your name: ");
String name = input.nextLine();
testObject.simpleMessage(name);
}
}
And that is the finished script!
If you dont understand this look throughout the java tutorials for an answer or PM me
Thanks for viewing this tutorial.
Java tut 17.
Your Admin: Hondaman.