[Tutorial] Just some lame Tutorial about accepting User Input[Very newbie]

Posts 1–3 of 3 · Page 1 of 1
[Tutorial] Just some lame Tutorial about accepting User Input[Very newbie]
And yes I know most of you know this.
To receive user input,
You need to import something and it is-
import java.util.Scanner
Let's do a simple program.
Tip: I use NetBeans, and Eclipse is also recommended.
package userinput;

import java.util.Scanner;

public class UserInput {

public static void main(String[] args) {

That will be the basic.
Scanner user_input = new Scanner(System.in);
A piece of code we need to create a Scanner to take user inputs.
String first_name;
We start creating a string
System.out.print("Enter your first name: ");
Now we need the user to type, and note that we are using System.out.print instead of System.out.println so that the user will type on the same line.
Now we create the code to receive input from user, which is
first_name = user_input.next();
Note: Without Scanner user_input = new Scanner(System.in), the code above would not work.
Now we proceed, requesting the user to input the second part.
String family_name;
We create the string, again.
System.out.print("Enter your family name: ");
Now we change the first name to family name.
Proceeding, we create the code to receive input from user, needed for everything String.
family_name = user_input.next();
Note that we are using family_name instead of first_name as the one we type must be similar to the one in the String.
Now we are ready to combine the names into one. Similarly, we use String.
String full_name
Now we combine them.
full_name = first_name + " " + family_name;
Tip: After every line of code, there must be a ;(Semi-Colon).
Now we are ready to show the full name, by using
System.out.println("You are: " + full_name);
Thanks for your attention! Hope this helped!
Bonus:
The full piece of code:
package userinput;

import java.util.Scanner;

public class UserInput {

public static void main(String[] args) {

Scanner user_input = new Scanner(System.in);

String first_name;
System.out.print("Enter your first name: ");
first_name = user_input.next();

String family_name;
System.out.print("Enter your family name: ");
family_name = user_input.next();

String full_name;
full_name = first_name + " " + family_name;

System.out.println("You are: " + full_name);
}

}

Note: I'm sorry but it seems like that I cannot put appropriate spaces for the bonus.
Do inform me of any errors!
I think you should include what each piece of code does for others who do not what they do.
Also you don't need the package in the code either.
You can use scanner, but you can also use javax.swing.JOptionPane...
Code:
import javax.swing.*;
public class gravity_project {

	public static void main(String[] args) {
		//Declare variables of gravity on the 9 planets
		String planet, input;
		int choice=1;
		int weight=0;
		double result=0;
		double gravity=1;
		
		//Prompt user to enter a number between 1 and 9 to choose which planet to use later for multiplication
		planet = JOptionPane.showInputDialog(null,"Choose a planet from this list:\n"
				+ "1. Earth\n"
				+ "2. Jupiter\n"
				+ "3. Mars\n"
				+ "4. Mercury\n"
				+ "5. Neptune\n"
				+ "6. Pluto\n"
				+ "7. Saturn\n"
				+ "8. Uranus\n"
				+ "9. Venus");
		try{
			choice = Integer.parseInt(planet);
		}
		catch(NumberFormatException error)
		{
			JOptionPane.showMessageDialog(null,"An illegal input was detected! You must use an integer");
		}
		if(choice>9 || choice<1) //If statement to check to see if they entered a number between 1 and 9, if it's outside of these bounds
		{ 						 //assume they chose Earth
			JOptionPane.showMessageDialog(null, "You didn't enter a number between 1 and 9, the program will now calculate your weight"
					+ "as if you were on Earth.");
			input = JOptionPane.showInputDialog(null,"Enter your weight in pounds (lbs.)");
			try{
				weight = Integer.parseInt(input);
			}
			catch(NumberFormatException error)
			{
				JOptionPane.showMessageDialog(null,"You must enter an integer for your weight");
			}
			result = weight*gravity;
			JOptionPane.showMessageDialog(null,"Your weight is: "+result+" on Earth");
			System.exit(0);
		}
		else
		{
			input = JOptionPane.showInputDialog(null,"Enter your weight in pounds (lbs.)");
			try{
				weight = Integer.parseInt(input);
			}
			catch(NumberFormatException error)
			{
				JOptionPane.showMessageDialog(null,"You must enter an integer for your weight");
			}
			JOptionPane.showMessageDialog(null,"The program will now calculate your weight by the gravity of the planet you chose.");
		}

		switch(choice){ //Switch statement for the choice of planets
			case 1: gravity=1;
				planet="Earth";
				break;
			case 2: gravity=2.65;
				planet="Jupiter";
				break;
			case 3: gravity=0.39;
				planet="Mars";
				break;
			case 4: gravity=0.38;
				planet="Mercury";
				break;
			case 5: gravity=1.23;
				planet="Neptune";
				break;
			case 6: gravity=0.05;
				planet="Pluto";
				break;
			case 7: gravity=1.17;
				planet="Saturn";
				break;
			case 8:	gravity=1.05;
				planet="Uranus";
				break;
			case 9: gravity=0.78;
				planet="Venus";
				break;
			default: gravity=1;
				break;
		}
		
		result=weight*gravity;
		result=(result*100.0)/100.0;
		JOptionPane.showMessageDialog(null,"Your weight is: "+result+"lbs. on "+planet);
	}

}
EDIT: This is a program I wrote in my intro to Java class, use it as you wish...

EDIT2: Also, just noticed, if you were to compile your code to a .java, it wouldn't open a display box for the user to enter anything.
Posts 1–3 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us