I'm completely new to programming and don't have a complete understanding of the syntax of java though I am slowly grasping it. I was wondering how if, else, and else if statements need to be written and formatted in java ? If people could post some simple code to demonstrate as well. Also how to get input in java?
Thanks![]()
is equivalent toCode:public static void main(String[] args) { // Our code starts here. int i = 0; // Declare variable "i" as 0. if (i == 0) { // if "i" is equal to 0 } else if (i == 1) { // if "i" is equal to 1 } else { // if none of the other conditions are met } }
The first being far more legible making else if statements more advantageous.Code:public static void main(String[] args) { // Our code starts here. int i = 0; // Declare variable "i" as 0. if (i == 0) { // if "i" is equal to 0 } else { if (i == 1) { // if "i" is equal to 1 } else { // if none of the other conditions are met } } }
input through command line programs is with for example the java.util.Scanner class, instanciated with System.in as parameter
Code:java.util.Scanner scn = new java.util.Scanner(System.in); String myStringVar = scn.nextLine();
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
Cool. Thanks. I figured the if statements out but I'm still working on getting the hang of the input statements. Thank you![]()
There are two ways (that I use) to read input with Scanner, by reading one word at a time or one line at a time. The Scanner object help you do this.
When you want to read an input, you first "declare" so:
To read a line you use: scan.nextLine() which will return a string. A line is indicated by the character '\n' (aka the enter key).Code:java.util.Scanner scan = new java.util.Scanner(System.in);
If you want to read a word, then you use scan.next() which will also return a string. Word can be separated by a space or new line character.Code:String line = scan.nextLine();
To read more than one word or more than one line, just repeat the code. Example:Code:String word = scan.next();
Same with line. Hopefully that explained it a bit more. Good luck!Code:String word1 = scan.next(); String word2 = scan.next(); String word3 = scan.next();
Ya, thanks. If I wanted to read an int as opposed to a string would I simply put "scan.nextInt();" ?
that's true. but, to make sure it's an int coming, you should check it with "scan.hasNextInt();" which returns a boolean if there is an in int scanned.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
Ima just do both, even though you have one already.
And the input is depressed to the to .inCode:int number = 0; if(number==0) { System.out.println("The number is equal to zero"); { else { System.out.println("The number is not equal to zero"); }
For ex. if you are reading a text file, and you need to capture some data from the text file and make it input for java...
Or you can declare something with the input statementCode:import java.util.Scanner; import java.io.*; public class insertnamehere { public static void main (String[] args) throws IOException { Scanner in = new Scanner (new File("whatever your file name is here")); in.NextLine(); <------ whatever was on the first line of that data file, has now been saved as data in java...
hope that helps =)Code:String line = in.nextLine();
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks