Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 118,880 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,973 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



program to display the given output

 
Reply to this topicStart new topic

program to display the given output

Adiv
post 1 Jul, 2008 - 10:21 AM
Post #1


New D.I.C Head

*
Joined: 10 Jun, 2008
Posts: 8


My Contributions


/*[b]This is a program which does the following:
Input:
Takes upto 20 test cases of names:
if name begins with professor then display "Welcome professor"
if name begins wid 'mr' or 'ms' display 'welcome visitor'
if there is only the name display'welcome student'.
(In a case insensitive way)
The last line after inputing all names should be " ".
*/

/*Problem: my program seems to be logically correct but the compiler is ignoring the else statement & i dont understand why...*/





import java.io.*;
public class Welcome
{

public static void main(String args[])throws IOException
{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name");

String s1[]=new String[20];
int count=0;

String s3 = "professor";
String s4 = "mr";
String s5 = "ms";

for(int i=0;i<20;i++)
{
String s = buf.readLine();

if(s.equals(" "))
{
break;
}
s1[i]=s.toLowerCase();
count++;
}

for(int i=0;i<count;i++)
{
if(s1[i].startsWith(s3))
{
System.out.println("Welcome professor");
}
else if(s1[i].startsWith(s4))
{
System.out.println("Welcome visitor");
}
else if(s1[i].startsWith(s5))
{
System.out.println("Welcome visitor");
}
else
{
System.out.println("Welcome student");
}
}
}


}

SAMPLE OUTPUT of the above program:
Enter name
Professor albus
MR weasly
Ms Granger
Harry Potter

Welcome professor
Welcome student
Welcome visitor
Welcome student
Welcome visitor
Welcome student
Welcome student
Welcome student

IDEAL OUTPUT:

Professor Albus Percival Wulfric Brian Dumbledore
pRoFeSsOr Minerva McGonagall
ProfessorSnape
Professo

.
Sample output
Welcome Professor
Welcome Professor
Welcome Professor
Welcome Student
User is offlineProfile CardPM

Go to the top of the page


gl3thr0
post 1 Jul, 2008 - 11:20 AM
Post #2


D.I.C Head

**
Joined: 27 Oct, 2007
Posts: 177


My Contributions


next time please use code tags
java

import java.io.*;
public class Welcome
{

public static void main(String args[])throws IOException
{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name");

String s1[]=new String[20];
int count=0;

String s3 = "professor";
String s4 = "mr";
String s5 = "ms";

for(int i=0;i<20;i++)
{
String s = buf.readLine();

if(s.equals(" ")){
break;
}
s1[i]=s.toLowerCase();
count++;
}

for(int i=0;i<count;i++){
if(s1[i].startsWith(s3))
{
System.out.println("Welcome professor");
}
else if(s1[i].startsWith(s4)){
System.out.println("Welcome visitor");
}
else if(s1[i].startsWith(s5)){
System.out.println("Welcome visitor");
}
else {
System.out.println("Welcome student");
}
}
}
}
User is offlineProfile CardPM

Go to the top of the page

gl3thr0
post 1 Jul, 2008 - 11:31 AM
Post #3


D.I.C Head

**
Joined: 27 Oct, 2007
Posts: 177


My Contributions


nd it seems u may be mistaken.. i just ran ur program and everything seems 2 work fine.

Entered
Professor albus
MR weasly
Ms Granger
Harry Potter
Professor Albus Percival Wulfric Brian Dumbledore
pRoFeSsOr Minerva McGonagall
ProfessorSnape
Professo

Welcome professor
Welcome visitor
Welcome visitor
Welcome student
Welcome professor
Welcome professor
Welcome professor
Welcome student

it all seems to run fine... however one thing i noticed.. if u hit enter a blank line.. then the program return student.. maybe thats the problem u where having? it looks like u hit enter twice after every entry

------------
Professor albus
MR weasly
Ms Granger
Harry Potter

Welcome professor
Welcome student ---
Welcome visitor
Welcome student ---
Welcome visitor
Welcome student ---
Welcome student
Welcome student ----


sooo if we want to fix the program a simple edit 2 the for loop is needed (i think this should work ---->
CODE

for(int i=0;i<20;i++)
    {
        String s = buf.readLine();
        if(s.equals(" ")){
            break;
        }
        else if(s.equals("")){ //a simple check..
            i--;
        }
        else{
                   s1[i]=s.toLowerCase();
            count++;
                         }    
    }


now another little thing that could be improved in ur program:
the if statments that decide what welcome message 2 print could be a shortened a little..
CODE

for(int i=0;i<count;i++){
    if(s1[i].startsWith(s3)){
        System.out.println("Welcome professor");
    }
    else if((s1[i].startsWith(s4))||(s1[i].startsWith(s5))){
        System.out.println("Welcome visitor");
    }
    else {
        System.out.println("Welcome student");
    }
}


This post has been edited by gl3thr0: 1 Jul, 2008 - 11:40 AM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/13/08 02:46AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month