[TUT]Java; Splitting/Parsing Strings + their uses[TUT]

3 posts Page 1 of 1
Contributors
User avatar
Zulf
Serious Programmer
Serious Programmer
Posts: 441
Joined: Fri Jun 11, 2010 7:46 am

This is my like, first ever tutorial I've ever written, so it might be horrible. :P

Well, this tutorial is going to be on splitting strings and getting the data and use it

for multiple things, the tutorial is very short and I will expand it soon.

TABLE OF CONTENTS:
• What splitting(parsing) can be used for
• Where to start - Getting user input
• How to split a string and convert to integers to do math problems

Part 1, What splitting(parsing) can be used for:

You can use this for using 2 numbers in a string to add together, or multiple

numbers.
You can use this if you make a 2d game, such as a maze to make a 'custom maze-

making system.'

Part 2, Where to start - Getting user input:

You should start by having your blank Java document:
Code: Select all
public class Main {

    public static void main(String[] args) {

    }

}

What this does:
The 'main' method is static and it tells the Java engine which file to

initialize first.

You then need to import the scanner from 'java.util.Scanner' by using this code:
Code: Select all
import java.util.Scanner;
and adding that to the beginning of the file, you should now have this:
Code: Select all
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

    }

}
Now you must create a Scanner to make it useable, by adding this code to the Document:
Code: Select all
Scanner s = new Scanner(System.in);
What this does:
s = Scanner name
System.in = allows the scanner to grab the text entered in console.

To start you need to add a message to let the user know he/she should enter some text into

the console:
Code: Select all
System.out.println("Enter a string:");
Once the string is entered you must create a variable(String) and let the string carry

what text the scanner has grabbed:
Code: Select all
String str;
str = s.nextLine();
System.out.println("You have entered: " + str);
What this does:
str = String variable
type: String (allows you to hold text data)
println = Just lets the user know what they entered by printing the

string, 'str' to the console.
s.nextLine = grabs the text and starts a new line.

We are done with this section of the tutorial. Your code should look similar to this:
Code: Select all
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
	Scanner s = new Scanner(System.in);
	System.out.println("Enter a string:");
	String str;
	str = s.nextLine();
	System.out.println("You have entered: " + str);
    }

}
Part 3, How to split a string and convert to integers to do math problems:

Now you have your code from the last section of this tutorial so we're ready to use that

tutorial to allow users to do math problems and such.

To start, you should change the text to notify users to type something used in part 2 with

this:
Code: Select all
System.out.println("Enter a set of numbers(x,x);"); //dont include the ( or )
Next, we need to split the string 'str' into useable format by using this code:
Code: Select all
String[] str0 = str.split(",");
String num1 = str0[0];
String num2 = str0[1];
What this does:
str0 = an array used to keep entered text, minus the split, ','.
num1 = a string used to keep the first number BEFORE the comma
num2 = a string used to keep the first number AFTER the comma

Why to split by a comma?:
We split by a comma because the format text should be entered is (x,x)

where x is an integer(I hope so).

Now that we have the numbers from the text, we need to convert them into useable types,

such as Integers for math problems:
Code: Select all
int num1_0;
int num2_0;
num1_0 = Integer.parseInt(num1);
num2_0 = Integer.parseInt(num2);
Now that we have the split string pieces into correct types to use for math problems we

need to add the code:
Code: Select all
int add, subt, mult, div;
add = num1_0 + num2_0;
subt = num1_0 - num2_0;
mult = num1_0 * num2_0;
div = num1_0 / num2_0;
Now that we have the results for the math problems, we need to print them so the user can

see:
Code: Select all
System.out.println("Addition: " + add);
System.out.println("Subtraction: " + subt);
System.out.println("Multiplication: " + mult);
System.out.println("Division: " + div);
Now once you've added that you're all done!

Your final code should look like this:
Code: Select all
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
	Scanner s = new Scanner(System.in);
	System.out.println("Enter a set of numbers(x,x);"); //dont include the ( or )
	String str;
	str = s.nextLine();
	System.out.println("You have entered: " + str);
	String[] str0 = str.split(",");
	String num1 = str0[0];
	String num2 = str0[1];
	int num1_0;
	int num2_0;
	num1_0 = Integer.parseInt(num1);
	num2_0 = Integer.parseInt(num2);
	int add, subt, mult, div;
	add = num1_0 + num2_0;
	subt = num1_0 - num2_0;
	mult = num1_0 * num2_0;
	div = num1_0 / num2_0;
	System.out.println("Addition: " + add);
	System.out.println("Subtraction: " + subt);
	System.out.println("Multiplication: " + mult);
	System.out.println("Division: " + div);
    }

}
Your output should look something like this:
Image
(The reason division is 0 is because it would come out as a decimal(double in Java) and int's only hold whole numbers, so to get the decimal of the math problem, you would need to convert it to a double, not an int. :)

Thank you for reading my tutorial, if anything is unclear, or you think I did not explain a bit well then please post so I can help you learn more. :D
Image
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

Java !=Javascript;
bump();
http://vagex.com/?ref=25000
User avatar
mikethedj4
VIP - Site Partner
VIP - Site Partner
Posts: 2592
Joined: Thu Mar 25, 2010 4:36 am

3 posts Page 1 of 1
Return to “Tutorials”