1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
/* Read File in String Using Java BufferedInputStream Example. This example shows how to read a file content into a Sting object using available and read methods of Java BufferedInputStream. */ import java.io.*; public class ReadFileInToString { public static void main(String[] args) { //create file object File file = new File("C://FileIO//ReadFile.txt"); BufferedInputStream bin = null; try { //create FileInputStream object FileInputStream fin = new FileInputStream(file); //create object of BufferedInputStream bin = new BufferedInputStream(fin); //create a byte array byte[] contents = new byte[1024]; int bytesRead=0; String strFileContents; while( (bytesRead = bin.read(contents)) != -1){ strFileContents = new String(contents, 0, bytesRead); System.out.print(strFileContents); } } catch(FileNotFoundException e) { System.out.println("File not found" + e); } catch(IOException ioe) { System.out.println("Exception while reading the file " + ioe); } finally { //close the BufferedInputStream using close method try{ if(bin != null) bin.close(); }catch(IOException ioe) { System.out.println("Error while closing the stream :" + ioe); } } } } /* Output would be This file is for demonstration of how to read a file into String using Java BufferedInputStream. */ |
In a test result are declared as follows
if % marks >=80 then the result declared is “PASSED WITH STAR”
if %marks <80 but >=60 then the candidate is awarded “FIRST DIVISION”
if %marks <60 but >=40 then the candidate is awarded “SECOND DIVISION”
if % marks <40 then the result is declaredas “FAILED”
write a program in java to input the %marks and print the result
[code]
import java.io.*;
class Marks
{
double percentage;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
void main()throws IOException
{
System.out.print(“Enter Percentage Marks:”);
percentage=Double.parsedouble(br.readLine());
if(percentage>=80)
{
System.out.println(“PASSED WITH STAR”);
}
else if(percentage<80 || >=60)
{
System.out.println(“FIRST DIVISION”);
}
else if(percentage<60 || >=40)
{
System.out.println(“SECOND DIVISION”);
}
else if(percentage<40)
{
System.out.println(“FAILED”);
}
else()
{
System.out.println(“ENTER CORRECT PERCENT MARKS”);
}
}
}
[/code]
too small a requiremend for using buffers !!
for reading a single mark at a time and writing on screen not neccesary to read using buffers
am not sure how are you taking input or are you reading from the user input
wanted to now if reading from file and commenting there
How can i read this variable in other method? for example i wrote a method which reads the file in to a variable. Now i want use the variable in other methods to search the text file for different values.
plz attach files about those two classes
Create a class it has following data members:-
-name
-account id
-balance
initialize the data members through constructors & define a print function & also define a function to compare names. It returns true if the name is equal or else returns false. Initialize an array of objects. Pass a name from the
command line & search for the name in the array of objects , if it is existing print the information of the object.