0

I am writing a program that will change java code. It changes the next-line bracket system into the end-of-line bracket system. I am using String to do this. Here is my code:

public static void main(String[] args) throws Exception
{   
    File file = new File("test.text");
    //PrintWriter output = new PrintWriter(file);
    Scanner input = new Scanner(file);

    System.out.println("Does it exist? " + file.exists());
    while(input.hasNext())
    {
        String first = input.next();
        String second = input.next();

        if (first == "{")
        {
        second = first.replace("{", "\n{");
        System.out.println(second);
        }
        else
        {
        System.out.println(first);
        }
    }
}

My test.txt looks like:

"hello{  how are you{"

So far it just takes out the word before the bracket. I am trying to get it to put the bracket on a new line.

5
  • 2
    Is this as an academic exercise? In any case, you compare strings with String.equals(), not ==. And you don't want to check for "{" anyway, you want to check for a "{" with nothing before it except whitespace. If it's not just for fun, use Jalopy/etc. Commented Jan 17, 2012 at 20:53
  • As a first step, I would focus on detecting the opening braces. Try to write a program that prints the file exactly as it is, except that it would print some marker after each line containing an opening brace. For the purposes of this exercise, please use just one variable instead of first/second (I think at this stage they're confusing more than they're helping). Commented Jan 17, 2012 at 20:55
  • This is what I am trying to do. In theory it should be printing "hello" and then the bracket on the next line. Commented Jan 17, 2012 at 20:58
  • I have changed the code with contains and it worked. I then optimized it with Luchian's answer. Now should I just print all the code to the file and then do the while loop? Commented Jan 17, 2012 at 21:04
  • Why don't you just try it (after saving a back-up) and see if it works. If it doesn't come back with a new question and a specific problem. We can't test the code for you. (Well, we can... but so can you). Commented Jan 17, 2012 at 21:15

2 Answers 2

1

You need to compare strings using .equals().

But you don't need comparison, you need to see if the first string contains {, so you should be using contains.

EDIT: Come to think of it, there's no need for contains either, or for two variables. One should do it:

while(input.hasNext())
{
    String first = input.next();
    String second = first.replace("{", "\n{");
    System.out.println(second);
}
Sign up to request clarification or add additional context in comments.

Comments

0

first .equals( "{"); when you compare strings you need to use equals not ==

Comments

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.