Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What changes do I need to make in the code If I remove the line "throws IOException"??
import java.io.*;
class Buffered_class{
public static void main(String[] args)
throws IOException // remove this line
{
char c;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter characters, 'q' to quit");
do{
c= (char)br.read();
System.out.println(" you entered : " + c );
}while(c !='q');
}
}
You need to catch the exception
import java.io.*;
class Buffered_class{
public static void main(String[] args)
{
char c;
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter characters, 'q' to quit");
do{
c= (char)br.read();
System.out.println(" you entered : " + c );
}while(c !='q');
}catch(IOException e){
// do something
}finally{
br.close();
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to store characters in an array of byte and write this byte array to a file and read the file back and output to the screen.
Here is my code ( I'm just starting), can anyone help me?
public static void main(String[] args) throws IOException
{
File f=new File("input.txt");
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);
int c = 0;
while((c = br.read()) != -1)
{
char character = (char) c;
System.out.println(character);
I don't see here Where do you write the character array to the file input.txt?
First you have to save the data to a file and then just read it.
public static void main(String[] args) {
char [] arr = new char[] {'a', 'b', 'c'};
byte[] bytes = String.valueOf(arr).getBytes();
try(BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("input.txt"))) {
bout.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
//then part of your code
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to write a progam to read a text file and compare its lines. I want to store them in an array, but I do not how to do this and how to compare, if they are equal.
package pantoum;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Pantoum {
public static void main(String[] args) throws FileNotFoundException {
//get filename input from user
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the full file name: ");
String fileName = keyboard.nextLine();
File inputFile = new File(fileName);
if (inputFile.exists()){
//create scanner to read file
Scanner input = new Scanner(inputFile);
//while (input.hasNext());{
String title = input.next();
System.out.println(title);
}
else
{
System.out.println("Sorry, that file does not exist.");
}
}
}
i personally prefer to use buffered reader when it comes to reading from a text file.
boolean equal=false;
String lines[] =new [10];
// or however long the array needs to be.
int count=0;
BufferedReader infile = new BufferedReader(new FileReader("<Name of file>"));
do {
lines[count] = infile.readLine();
count++;
} while (lines[count] != null);
for(int i=0;i<lines.length();i++) {
for(int j = i+1; j<lines.length()-1;j++){
if(lines[i].equals(lines[j])){
equal=true;
system.out.print(lines[i]+" and "+lines[j]+" are equal");
}}}
if(!equal){
system.out.print("Sorry your text did not equal any text from the text file");
}
i hope this helped and i hope i have explained everything well enough for you to understand, if not feel free to ask.
I personally would read each line into a String[] position and then in a loop check if String[i].equals(String[j]). If you need a rough idea how to code this let me know.
You can just use scanner's nextLine() method to get an entire line and then compare them using String.equals()
something like
String line1 = input.nextLine();
String line2 = input.nextLine();
if(line1.equals(line2){
doStuff();
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am really stuck up with this.
I am having an input file say input.txt.
content of input.txt is
Using a musical analogy, hardware is like a musical instrument and software is like the notes played on that instrument.
Now I want to search the text
like a musical instrument
How can I search the above content in input.txt in java. Any help???
Inorder to search a pattern in java, java provides contains() method in String. Try to use that, Following is the snippet of the code that serve the purpose,
public static void main(String[] args) throws IOException {
FileReader reader = new FileReader(new File("sat.txt"));
BufferedReader br = new BufferedReader(reader);
String s = null;
while((s = br.readLine()) != null) {
if(s.contains("like a musical instrument")) {
System.out.println("String found");
return;
}
}
System.out.println("String not found");
}
You can always use the String#contains() method to search for substrings. Here, we will read each line in the file one by one, and check for a string match. If a match is found, we will stop reading the file and print Match is found!
package com.adi.search.string;
import java.io.*;
import java.util.*;
public class SearchString {
public static void main(String[] args) {
String inputFileName = "input.txt";
String matchString = "like a musical instrument";
boolean matchFound = false;
try(Scanner scanner = new Scanner(new FileInputStream(inputFileName))) {
while(scanner.hasNextLine()) {
if(scanner.nextLine().contains(matchString)) {
matchFound = true;
break;
}
}
} catch(IOException exception) {
exception.printStackTrace();
}
if(matchFound)
System.out.println("Match is found!");
else
System.out.println("Match not found!");
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I've got this question and I'm not very sure how to answer it - I am able to read the file but unsure on how to display only the words with more than 10 characters
Although other answers are correct , I recommend using scanner class to read files, as it safely allows to detect end of file conditions and has simpler/more useful utility methods:-
Scanner input = new Scanner(new File("file.txt"));
while(input.hasNextLine())
{
String word = input.nextLine();
if(word.length()>10){
System.out.println(word)
}
}
This should work:
private static void readFile(File fin) throws IOException {
FileInputStream fis = new FileInputStream(fin);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while ((line = br.readLine()) != null) {
if(line.length()>10) System.out.println(line);
}
br.close();
}
Suposing you have the word in a String variable called word:
if (word.length() > 10)
System.out.println(word);
PS: google before asking!!
Here's some pseudo code to help you get started
create empty list //where we'll add all the >10char words
read file(split per newline) //see the apache commons api
for each line
split on space
for each word in splitted sentence
if wordLength > 10 add to empty list
print each entry in your filled list
Here is your logic.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) throws Exception{
FileInputStream fis = new FileInputStream(new File("your.txt")); // path for the text file
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while ((line = br.readLine()) != null) {
String st[] = line.split(" ");
for(int i=0; i<st.length; i++){
if(st[i].length()>10) System.out.println(st[i]);
}
}
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
File inputTXT = new File (fileName);
try{
Scanner in = new Scanner(inputTXT);
}catch(FileNotFoundException e){
System.out.println("");
}
while(in.hasNext()){
String line = in.nextLine();
It says in can't be resolved.How am I going to fix this problem?
I've tried ignored try catch block, but this file scanner has to be in the try catch block
In your code you have issue about variable scope.You defined in variable in try catch block and then you used it in while loop.It should be like below:
File inputTXT = new File (fileName);
Scanner in=null;
try{
in = new Scanner(inputTXT);
}catch(FileNotFoundException e){
System.out.println("");
}
while(in.hasNext()){
String line = in.nextLine();