Results 1 to 3 of 3
  1. #1
    Ivvvvvan's Avatar
    Join Date
    Dec 2014
    Gender
    male
    Posts
    3
    Reputation
    10
    Thanks
    0

    [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!

  2. #2
    αяgуяσѕ's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    A Box
    Posts
    1,714
    Reputation
    64
    Thanks
    146
    My Mood
    Lurking
    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.
    Last edited by αяgуяσѕ; 12-20-2014 at 08:21 PM.
    Quote Originally Posted by Dave84311 View Post
    Valid keys, he gave me one himself.

    ____________________________
    Need help? Pm me
    ___________________________
    Please Press The Thanks If I Helped

  3. #3
    liquidsystem's Avatar
    Join Date
    Sep 2012
    Gender
    male
    Posts
    507
    Reputation
    135
    Thanks
    691
    My Mood
    Happy
    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.
    Last edited by liquidsystem; 12-28-2014 at 01:58 PM.
    If you wish to thank me, don't forget to click the button!

    Currently Playing: Osu, OldSchool Runescape (pm for username)


    Bitcoin Address: 1HUdLVM7DnT9gC1i5kNKzcKWekSbFoKNp2


    Steam
    Main Account
    Sales Account

    Feel free to PM me if you have any other questions relating Java, Python, or Physics, or general schoolwork


Similar Threads

  1. [Request] Some advance tutorial For Vb.net
    By LastOnInHell in forum Visual Basic Programming
    Replies: 2
    Last Post: 10-23-2013, 10:56 AM
  2. I need some glitches tutorials
    By Janitor in forum CrossFire Glitches
    Replies: 9
    Last Post: 09-16-2013, 11:57 AM
  3. [TuT]Here is some scanning tutorials!
    By jokuvaan11 in forum WarRock - International Hacks
    Replies: 10
    Last Post: 06-28-2007, 08:48 PM
  4. Just some HELLO to you from... eh santaclaus.. NO ME DUHH xD
    By hahagotyoulooking in forum WarRock - International Hacks
    Replies: 5
    Last Post: 05-25-2007, 06:17 AM
  5. just a quick question about weapons
    By depkillz in forum WarRock - International Hacks
    Replies: 1
    Last Post: 03-13-2007, 11:48 PM