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();
        }
    }
}
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
Vaibhav
  • 131
  • 1
  • 9
  • 1
    Two problems - one is calling `keyboard.next()` twice (instead of reading once, and then using the value twice), and the other is http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Jon Skeet Mar 29 '14 at 10:40

3 Answers3

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();
     }
   }
}
Benjamin
  • 2,257
  • 1
  • 15
  • 24
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();
}
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();
}
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • Thanks mate...i just forgot about the equals method and stupidly using != operator...well thanks a lot... – Vaibhav Mar 31 '14 at 04:50