1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Java: If a line is exactly equal to the previous line

Discussion in 'Software Development' started by javahello, Jan 30, 2012.

Thread Status:
Not open for further replies.
  1. javahello

    javahello Thread Starter

    Joined:
    Jan 30, 2012
    Messages:
    2
    can anyone help, i can't seem to be able to print repeated when the line entered is exactly the same as the one before, or print farewell when a blank line is entered, this is my code so far:
    String word, word1;


    Scanner scan = new Scanner (System.in);
    System.out.println ("Enter continuous lines of text: ");

    while (scan.hasNextLine()){
    word = scan.nextLine();
    word1 = scan.nextLine();
    }


    if (word.equals(word1)){
    System.out.print ("Repeated:" + word);
    }
    else if
    (word.isEmpty()){
    System.out.print("Farewell");



    }
    }
    }
     
  2. sepala

    sepala

    Joined:
    May 20, 2010
    Messages:
    4,038
    Hi, Welcome to TSG

    OK, lets have a look at the ISSUES in your code

    1. If you have JUST TWO WORDS, then no need of the loop
    2. The way you checked for the blank line is wrong
    3. If you want to check two strings, don't use .equals(). Because it will check the STRING OBJECT. For an example, if the both lines are blank, it will still print "Repeated"
    4. You have to check whether any of the lines are blank or not, but you are checking only the first line

    Have a look at the following edited code

    Code:
    import java.util.*;
    
    public class Test
    {
       public static void main(String[] args)
       {
           
           String word=null;
           String word1 = null;
       
           
           
           Scanner scan = new Scanner (System.in);
            System.out.println ("Enter continuous lines of text: ");
    
            
                word = scan.nextLine();
                word1 = scan.nextLine();
            
    
    
            if (word==word1)
            {
                System.out.print ("Repeated:" + word);
            }
            else if((word==null || word.length()==0) || (word1==null || word1.length()==0))
            {
                System.out.print("Farewell");
            }
            
       }
    }
    
     
  3. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    Hi,
    If I can just jump in here. The reason your code is not working is because it is never escaping that while loop. If you move the if and if else statements to inside the loop as below you'll find your code works.

    About String comparisons - the .equals() method should be used instead of == because the == operator directly compares the objects. The objects may be different but contain the same string however the == will still return false.

    Code:
    String word = null, word1 = null;
    
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Enter continuous lines of text: ");
    
    		while (scan.hasNextLine()) {
    			word = scan.nextLine();
    			word1 = scan.nextLine();
    			
    			if (word.equals(word1)) {
    				System.out.print("Repeated:" + word);
    			} else if (word.isEmpty() || word1.isEmpty()) {
    				System.out.print("Farewell");
    			}
    		}
    
    I have just changed youre if else statement to allow the second word to be empty and it will still say farewell. It is also good to Initialise your variables when you declare them, as I have done in the code as String word = null;.

    Andy
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1038829

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice