[TUT]Java; Splitting/Parsing Strings + their uses[TUT]
3 posts
Page 1 of 1
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:
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:
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:
what text the scanner has grabbed:
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:
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:
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:
need to add the code:
see:
Your final code should look like this:
(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.
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
and adding that to the beginning of the file, you should now have this:
import java.util.Scanner;
Code: Select all
Now you must create a Scanner to make it useable, by adding this code to the Document:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
}
}
Code: Select all
What this does:Scanner s = new Scanner(System.in);
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
Once the string is entered you must create a variable(String) and let the string carry System.out.println("Enter a string:");
what text the scanner has grabbed:
Code: Select all
What this does:String str;
str = s.nextLine();
System.out.println("You have entered: " + str);
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
Part 3, How to split a string and convert to integers to do math problems: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);
}
}
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
Next, we need to split the string 'str' into useable format by using this code:
System.out.println("Enter a set of numbers(x,x);"); //dont include the ( or )
Code: Select all
What this does:String[] str0 = str.split(",");
String num1 = str0[0];
String num2 = str0[1];
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
Now that we have the split string pieces into correct types to use for math problems we int num1_0;
int num2_0;
num1_0 = Integer.parseInt(num1);
num2_0 = Integer.parseInt(num2);
need to add the code:
Code: Select all
Now that we have the results for the math problems, we need to print them so the user can 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;
see:
Code: Select all
Now once you've added that you're all done!System.out.println("Addition: " + add);
System.out.println("Subtraction: " + subt);
System.out.println("Multiplication: " + mult);
System.out.println("Division: " + div);
Your final code should look like this:
Code: Select all
Your output should look something like this: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);
}
}
(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.
Java !=Javascript;
bump();
bump();
3 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023