File Handling in JAVA processing file data JAVA by Ravinder Nath Rajotiya - April 16, 2021April 16, 20210 Reading integer from file This program explains how we can read numerical data from a file and process it. Remember we need following steps to read from a file: step-1: create File object and connect the file containing data by passing fileName in object as: File file = new File("outputFile.txt"); Step-2: Test whether the file exists, if it exists step-3: Create object to Scanner class and connect it to the file containing data pointed by File object "file" as shown below: Scanner inputFile = new Scanner(file); Step-4: Check if the line being pointed by nextLine() has the data, this is done asL: while(inputFile.hasNext()) Step-5 read from thefile sum += inputFile.nextInt(); Now see the complete program, of reading integer values from the file and count the number values in file and
File Handling in JAVA reading writing text JAVA by Ravinder Nath Rajotiya - April 16, 2021April 16, 20210 File Handling In this lecture we are going to understand how to write to a file and how to read from a file. Writing to a File: writing to a file is a three step process: 1. Step-1: Create a file - use PrintWrite class outputFile is an object to printWriter class and we initialize it by passing the file name as argument to new printWriter(), see below PrintWriter outputFile=new printWriter(fileName “outFile.txt”); 2. Step-2 Write to file - We can now use the objectname.println() method to write to the file as: outputFile.println("This is first line"); 3. Close the file- After writing to the file, you need to close the file as outputFile.close(); class PrintWriter belongs to java.io.PrintWriter; package. In Intellij it is automatically imported as we write it. we also need
LOOP in JAVA cont… JAVA by Ravinder Nath Rajotiya - April 16, 2021April 16, 20210 Loop Controls Here is an example of range validation, but has no control as how many times the user can take to input the correct value Example-1: Input Validation package com.example; import javax.swing.*; public class loopContinued { public static void main(String[] args) { //input validation String input; int number; input= JOptionPane.showInputDialog("Please enter a number" + "between 1 and 100"); number=Integer.parseInt(input); // now let us validate while(number <1 || number >100) // OR operator to check outside range { JOptionPane.showMessageDialog(null, "Not a valid number"); input=JOptionPane.showInputDialog("Please enter a number between 1 and 100"); number = Integer.parseInt(input); } } } Example-2: user control of Loop In the example below, we use control the loop variable package com.example; import javax.swing.*; public class loopContinued { public static void main(String[] args) { //input validation String input; int Maxnumber; input=JOptionPane.showInputDialog("How high should I go to square the number"); Maxnumber = Integer.parseInt(input); System.out.println("number Squareed "); for(int i=1; i<= Maxnumber; i++) { System.out.println( i
Introduction to Loop in JAVA JAVA by Ravinder Nath Rajotiya - April 16, 2021April 16, 20210 Loops in JAVA There are three types f loop structures in JAVA What is a Loop? A Loop is a squance of instructions that are continually repeated until a certain condition is reached or met. We have following three types of loop in JAVA. while() do- while() for loop Requirements of loop We require to follow the following steps for writing a loop: for every loop we need a loop control variable with some initializatio value give final test value to this variable inside the loop e.g. while(x<10) Write the body of the loop containing the task to be repeated and inside the loop modify or change the control variable to test for changed value next time the loop executes. Let us start with the loop This
Switch Case in JAVA JAVA by Ravinder Nath Rajotiya - April 15, 2021April 16, 20210 Decision Making using switch case statement Well you have seen the decision making in previos posts on if, if-else statements. switch case is yet another pwerful decision making concept. We first ask the user to input some number, alphabet or some string to be used in taking decision so that control can be transferred at some relevant place in the program. //JOptionPane.showInputDialog() gets only inputs string values, to make them numbers we need to convert it to integer as: input = JOptionPane.showInputDialog("Please enter 1, 2, or 3"); //convert to integer number=Integer.parseInt(input) //for for inputting single character, we don't convert string to letters, but read only first letter of string at position '0'. This is becaues strings are constructed using characters input = JOptionPane.showInputDialog("Please enter a, b, or