0

I want to take input from keyboard in my java program until the user type abc

Here is the code which i have written but it doesn't work. the program continues to take input from keyboard even after i have typed abc and lastly I have to close the program by myself. It takes in input from keyboard and write it on a file named file1.txt

import java.io.*;
import java.util.*;

public class io {

    public static void main(String args[]) {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter("file1.txt", true));
            Scanner keyboard = new Scanner(System.in);
            do {
                writer.write(keyboard.next());
                writer.newLine();
            } while (keyboard.next() != "abc");
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
1
  • 1
    Two problems - one is calling keyboard.next() twice (instead of reading once, and then using the value twice), and the other is stackoverflow.com/questions/513832/… Commented Mar 29, 2014 at 10:40

3 Answers 3

2

Try this , it works, Just use while loop instead of do while

Code:

 import java.io.*;
 import java.util.*;

 public class io{

    public static void main(String args[]) {
    try {
     BufferedWriter writer = new BufferedWriter(new FileWriter("file2.txt", true));
    Scanner keyboard = new Scanner(System.in);

    while(!keyboard.next().equals("abc"))
    {
            writer.write(keyboard.next());
            writer.newLine();
    }
         writer.close();
      } 
 catch (IOException e) 
     {
        e.printStackTrace();
     }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1
try {
  BufferedWriter writer = new BufferedWriter(new FileWriter("file1.txt", true));
  Scanner keyboard = new Scanner(System.in);

  writer.keyboard.nextLine(); // if need write 1st string alltime

  while (keyboard.hasNext()){
  String buffer = keyboard.nextLine();
  if (!"abc".equals(buffer)) //  !!! using equals on object not null ("abc" - is not null)
  {  
       // do if entered "abc", use break for ending while
  }
      // do if entered others, use variable name buffer
  }
} catch (IOException e) {
            e.printStackTrace();
}

Comments

0

As @JonSkeet said - you're calling keyboard.next() twice instead of storing the result in a local variable like you should. Secondly you're comparing strings with the != operator, but this only checks whether you are pointing to the exact same String object. There may be thousands of String objects in memory with the string "abc" in them, and none of them would be equal to each other using ==. Instead, you need to call the method equals.

try {
    BufferedWriter writer = new BufferedWriter(new FileWriter("file1.txt", true));
    Scanner keyboard = new Scanner(System.in);
    String next;
    do {
        next = keyboard.next();
        writer.write(next);
        writer.newLine();
    } while (!next.equals("abc"));
    writer.close();
} catch (IOException e) {
    e.printStackTrace();
}

1 Comment

Thanks mate...i just forgot about the equals method and stupidly using != operator...well thanks a lot...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.