Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8727369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:25:27+00:00 2026-06-13T08:25:27+00:00

public static void main(String[] args) throws IOException{ if (args.length == 1) { BufferedReader bf

  • 0
public static void main(String[] args) throws IOException{
if (args.length == 1)
{
    BufferedReader bf = new BufferedReader (new FileReader("fruit.txt"));
    int linecount = 0;
    String line;
    //run thur the txt file to check if input exist
    while (( line = bf.readLine()) != null)
    {
        linecount++;
        int indexfound = line.indexOf(args[0]);
        if (indexfound > -1) {
            System.out.println("fruit exist on line " + linecount);
            System.out.println("add another fruit");    
            System.exit(0);
        } else {
            BufferedWriter bw = new BufferedWriter(new FileWriter("fruit.txt", true));
            String fruit = "";
            fruit = args[0];
            bw.write("\r\n" + fruit);
            System.out.println(fruit+ "added"); 
        }
    }
    f.close();
    bw.close();
}

I want to have the program to search in a text file fruit.txt
check if a fruit already exist in it.

if fruit exist then prompt user to input another 1

else
add in to the nextline of the text file

This is what I got so far.
but I am not sure why it isnt what I wanted.

in my text file it started with 3 fruit

apple
orange
pear

after i add in berry

apple
orange
pear
berry
berry

after i add in melon

apple
orange
pear
berry
berry
melon
melon
melon
melon
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:25:27+00:00Added an answer on June 13, 2026 at 8:25 am

    You are just checking for the fruit in the first line, and if it is not found, you are continuing to add it.

    You need to read your file completely first, one for checking each line, that it contains your fruit or not, and then if it does not contain, just dump that fruit in it. And if it contains, reject it.

    So, in your while, you need to move that else part outside. And rather than doing System.exit() when fruit is found, you can set a boolean variable to true, and then later on, based on the value of boolean variable, you can decide whether to add the fruit or not.

    boolean found = false;
    while (( line = bf.readLine()) != null) {
    
        linecount++;
        int indexfound = line.indexOf(args[0]);
        if (indexfound > -1) {
            System.out.println("fruit exist on line " + linecount);
            System.out.println("add another fruit");   
            found = true;
            break;
        }
    }
    
    if (!found) {
    
        BufferedWriter bw = new BufferedWriter(new FileWriter("fruit.txt", true));
        String fruit = "";
        fruit = args[0];
    
        bw.write("\r\n" + fruit);
        System.out.println(fruit+ "added"); 
    
        bw.close();  // You need to close it here only. 
    }
    
    bf.close();                     
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class ReplaceVowels { public static void main(String args[]) throws IOException { BufferedReader bf
public static void main(String[] args) throws IOException, URISyntaxException,TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); String
import java.io.*; import java.util.StringTokenizer; class FileCopy { public static void main(String[] args) throws IOException
class Test { public static void main(String[] args) throws Exception { Test t =
Following is my consumer : public static void main(String[] args) throws JMSException { //
as the title. public static void main(String[] args) throws InterruptedException { Thread thread =
import javax.swing.*; public class test { public static void main(String[] args) throws Exception {
import java.io.*; import java.util.*; public class Readfilm { public static void main(String[] args) throws
public static void main(String[] args) { int i = 0; while(i==0){ System.out.println(Possibilities: R, T,
public static void main(String[] args) { // TODO Auto-generated method stub BufferedReader br1 =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.