Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Returning

Posted on 2004-10-08
13
182 Views
Last Modified: 2010-03-31
ok heres another interesting situation....

ok i figured out the problem wit my while loop but now i hav returnin to.

for example...

the user enters "find"
    ....
    } else if (userIn.startsWith("find")) {
        clt.finding(userIn);
    ....

this part of the while loop is ran which calls the finding method

which is:

public void finding (String findWhat) {
    if (findWhat.equals("find")) {
        System.out.println("Unknown command");
    } else {
         System.out.println("Finding");
    }
}            

now, after the method is called it prints out fine but i then need the program to go back to the while loop so that the user can keep usin the program... for example... if they want to quit they type "quit"

Thanks again..
0
Comment
Question by:AnthonyCosenza
  • 6
  • 6
13 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12265240
boolean first = true;
   while (userIn != null) {        
        if (userIn.startsWith("quit")) {
            break;
        }
else if (userIn.startsWith("find")) {
        clt.finding(userIn);
}
    else {
            if( first ) {
                .... extra statement ....
                eg. System.out.println("testing");
                first = false;
            }
           
        }
         //read userIn value
          userIn=bySomeMeansGetValue();    
    }

0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12265248
for example bySomeMeansGetValue method should be something like this




public String bySomeMeansGetValue()
{

 try {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String str = "";
        while (str != null) {

            str = in.readLine();
              return str;
        }
    } catch (IOException e) {
    }
    return null;
}
0
 

Author Comment

by:AnthonyCosenza
ID: 12265277
the problem with the if statement has been solved already... the problem is now tryin to get back from the other methods, for example.. finding().. so those two comments dont really help sorry

0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12265285
This should definately solve ur problem

If userIn starts with find then compulsory it calls finding method.

Could u elaborate ur problem??


 boolean first = true;
   while (userIn != null) {        
//read userIn value
          userIn=bySomeMeansGetValue();    
        if (userIn.startsWith("quit")) {
            break;
        }
else if (userIn.startsWith("find")) {
        clt.finding(userIn);
}
}
0
 

Author Comment

by:AnthonyCosenza
ID: 12265295
ok, this is wat i now have for the while loop... this seems to solve the problem that i was having before:

InputStreamReader in = new InputStreamReader(System.in);
BufferedReader readIn = new BufferedReader(in);
            
String userIn;
            
while ((userIn = readIn.readLine()) != null) {
    .....
    } else if (userIn.startsWith("find")) {
        clt.finding(userIn);
    } ....
}

and the finding() method is:

public void finding (String findWhat) {
    if (findWhat.equals("find")) {
        System.out.println("Unknown command");
    } else {
         System.out.println("Finding");
    }
}  

the problem i am havin now is if the "find" command is entered by the user it goes into the while loop, runs the method and then it isnt possible to run any other commands eg "quit"

do u understand my problem now??

   
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12265307
This is working perfectly at my end
public static void finding(String findWhat) {
        if (findWhat.equals("find")) {
            System.out.println("Unknown command");
        } else {
            System.out.println("Finding");
        }
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception{
        // TODO code application logic here
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader readIn = new BufferedReader(in);
        String userIn="";
        while ((userIn = readIn.readLine()) != null) {
            if(userIn.startsWith("quit")) {
                break;                
               
            } else if (userIn.startsWith("find")) {
                finding(userIn);
            }
        }
    }
0
 

Author Comment

by:AnthonyCosenza
ID: 12265339
can u do a quit command after a find??
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12265348
yes, what is the problem you are getting??
0
 

Author Comment

by:AnthonyCosenza
ID: 12265369
thats strange.......

once i do the find command i cant run a quit command or any of the other commands..

very puzzled.. :S
0
 

Author Comment

by:AnthonyCosenza
ID: 12265379
Like i enter them but there is no result..

eg.. i can enter "quit" but it doesnt go and quit the program usin the method i hav created for it... it just goes onto the next line of the user input
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 500 total points
ID: 12265384
for following program


/*
 * BufferRead.java
 *
 * Created on October 9, 2004, 12:19 PM
 */
import java.io.*;
/**
 *
 * @author  comp8
 */
public class BufferRead {
   
    /** Creates a new instance of BufferRead */
    public BufferRead() {
    }
   
    public static void finding(String findWhat) {
        if (!findWhat.equals("find")) {
            System.out.println("Command : Unknown command");
        } else {
            System.out.println("Command : Finding");
        }
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception{
        // TODO code application logic here
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader readIn = new BufferedReader(in);
        String userIn="";
        while ((userIn = readIn.readLine()) != null) {
            if(userIn.startsWith("quit")) {
                break;                
               
            } else if (userIn.startsWith("find")) {
                finding(userIn);
            }
                  else
                  {
                        System.err.println("Unkown Command : "+userIn);
                  }
                  userIn="";
        }
    }    
}


the results are like this - take care it is case sensitive

C:\>java BufferRead
Command1
Unkown Command : Command1
Command
Unkown Command : Command
Find
Unkown Command : Find
find
Command : Finding
Quit
Unkown Command : Quit
QUIT
Unkown Command : QUIT
quit
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12265517
Make sure you don't have unwanted whitespace


while ((userIn = readIn.readLine()) != null) {
    userIn = userIn.trim();
    if (userIn.startsWith("quit")) {
        break;
    }
    else if userIn.startsWith("find")) {
        finding(userIn);
    }
    else {
        System.out.println("Invalid command");
    }
}

}
0
 

Author Comment

by:AnthonyCosenza
ID: 12268663
I actually found the problem a little later last nite..

It had to do with my close method which "quit" command calls not closing the socket properly.

Thanks for all ur help guys
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Desingning Refactoring existing code 2 26
How  can  i  resolve  HTTP Status 404 -? 8 41
jsp insert to database example 2 43
Java Restore security prompts not working 10 53
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question