-3

Write a program that keeps reading strings until it reads the string “xyz”. If your first name is among the entered strings, the program prints “My name is there.” Otherwise, it prints “My name is not listed. Please add it.”

import java.util.*;
public class problem1 {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String name,name2="xyz";
        Scanner input=new Scanner(System.in);
        name=input.next();
        while(name!="xyz")
            name=input.next();
    }
    {
        if (name.equals("rania"))
            System.out.println("my name is there");
        else
            System.out.println("please enter your name");
    }
}
Kenster
  • 23,465
  • 21
  • 80
  • 106
sara chatila
  • 59
  • 1
  • 8

2 Answers2

1

Your problem is about understanding how block of code works. This is, you open a block of code using { and close it by using }. By doing this, all the code wrapped in { ... } will be a block, and this block will belong as a body for a statement. You can easily detect problems related to block of code if you indent the code properly. You can find more info about indentation here.

Also, you have a problem when comparing Strings. You should use equals method. There's a deeper explanation here: How do I compare strings in Java?

This is how your code should look like:

while(!name.equals("xyz")) {
    if (name.equals("rania")) {
        System.out.println("my name is there");
    } else {
        System.out.println("please enter your name");
    }
    name=input.next();
}

Note: This code is not meant to solve the text stated in your homework. It is only a guide to make your code able to compile and run with no problems. How to solve your exact homework... that's your work, not from us.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • um... why not just use while(!name.equals("rania")) and cut out all the weird xyz code? Also, your "my name is there" message will never print with your current code since the while loop will end on the line before it. – user1274820 Oct 24 '14 at 21:39
  • @user1274820 I remember, and the *offending* code worked as expected (and as explained in my now deleted comment). But still, if you want to be sure, just copy/paste it in any IDE you have and it runs as expected. Note that I removed the ! from the `while` condition. You can check the *offending* code in the history of the post. – Luiggi Mendoza Oct 24 '14 at 21:59
0

Actually if you had you tested out your code snippet (which was fairly close) you would have got it working in a couple of iterations.

Here is a fully tested code snippet that works as per problem statement:

    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        String name = "rania";
        String endString = "xyz";
        Scanner input= new Scanner(System.in);

        String aString =input.next();

        // Assume name will not be found6
        Boolean isNameFound = false;

        while(!aString.equals(endString))
        {   
            if (aString.equals(name))
            {
                isNameFound = true;
                // This is to ensure program does not quit when the name is found
                // break;  
            }

            aString = input.next();
        }


        if(isNameFound)
        {
            System.out.println("My name FOUND");
        }
        else
        {
            System.out.println("My name NOT FOUND");
        }
    }

Here is output for case 1: none of the string entered matched the desired name until the end string "xyz" was entered

sjhsa
sdkhfds
khds;kuf
xyz
My name NOT FOUND

Here is output for case 2: one of the string entered matched the desired name and the program ended.

afkhds
kjfdsgaks
fdgkjfd
gvkjfd
rania
My name FOUND
Chiseled
  • 2,280
  • 8
  • 33
  • 59
  • thank you but the program should only exit when printing xyz and not rania so if we printed rania the while loop should keep working until printing xyz and then at last print my name found . But i think this is impossible .... thank u – sara chatila Oct 25 '14 at 12:16
  • @sarachatila Nothing is impossible in programming. Look at the edited answer. Enjoy coding !! – Chiseled Oct 25 '14 at 20:23
  • you are welcome. Do upvote answers which you found were useful and do not forget the mark one as accepted which was the most satisfactory – Chiseled Oct 29 '14 at 16:52