6 Replies - 371 Views - Last Post: 10 October 2012 - 12:29 PM Rate Topic: -----

#1 javalotus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 12

Can not compile this code ;(

Posted 10 October 2012 - 11:57 AM

can not run this code......He is telling me about MISSING return statement ;(
Goal of this code: the user types words such as "save" etc. The program gives save and save command. On "exit" the LOOP WHILE has to stop..... ;( I tries this code...but unfortunetaly it doesnt work ;( need some help please ;(

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scvprocessor.another.version;
import java.util.Scanner;
/**
 *
 * @author c2rsaldi
 */
public class SCVProcessorAnotherVersion {
static Scanner message = new Scanner(System.in);
  
/**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
         System.out.println("Type a command");
         String inputMessage = message.nextLine();
          inputText (inputMessage) ;
         System.out.println(inputText (inputMessage));
        // TODO code application logic here
    }
    public static String inputText (String inputMessage) {
   
        while (!(inputMessage.equals(' '))) {
         
        if  (inputMessage.contains("print")) {
        return inputMessage +"\n"+"Print Command";
}  else if (inputMessage.contains("save")) {
        return  inputMessage + "\n"+"Save Command";
       }
         else if (inputMessage.contains("=")) {
           return inputMessage+"\n"+"Assignment command";
       } else if (inputMessage.contains("exit")) {
           return inputMessage+"\n"+"Exit command"; 
            break;
    } else  
       {return (inputMessage + "\n" + "Unknown command");
        
}
    }
}
}


This post has been edited by no2pencil: 10 October 2012 - 11:58 AM
Reason for edit:: Added Code Tags


Is This A Good Question/Topic? 0
  • +

Replies To: Can not compile this code ;(

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Can not compile this code ;(

Posted 10 October 2012 - 12:03 PM

You get this error because every return statement you have in the "inputText" method is inside the while loop. If the code never drops into the while loop, the statement doesn't have a return, and therefore is not allowed. You will need to put a return statement at the end of the method outside of the while loop in order to make it compilable.
Was This Post Helpful? 0
  • +
  • -

#3 javalotus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 12

Re: Can not compile this code ;(

Posted 10 October 2012 - 12:11 PM

View PostKilorn, on 10 October 2012 - 12:03 PM, said:

You get this error because every return statement you have in the "inputText" method is inside the while loop. If the code never drops into the while loop, the statement doesn't have a return, and therefore is not allowed. You will need to put a return statement at the end of the method outside of the while loop in order to make it compilable.


Oh, got it ;) thanks. How about stop a loop after "EXIT" is entered?

and my loop doesnt work ;(
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Can not compile this code ;(

Posted 10 October 2012 - 12:13 PM

View Postjavalotus, on 10 October 2012 - 03:11 PM, said:

and my loop doesnt work ;(

what does that mean ?
Was This Post Helpful? 0
  • +
  • -

#5 javalotus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 12

Re: Can not compile this code ;(

Posted 10 October 2012 - 12:16 PM

I maen that user has to type until he types "EXIT". on "EXIT" loop WHILE has to stop and close the program.....sorry ...i am new in java.....
Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Can not compile this code ;(

Posted 10 October 2012 - 12:20 PM

Here is your looping condition at the moment -

while (!(inputMessage.equals(" ")))


which essentially reads loop while the input does not equal a space. What bit would a need to change to get it to loop whilst the input is not "EXIT"?
Was This Post Helpful? 0
  • +
  • -

#7 javalotus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 12

Re: Can not compile this code ;(

Posted 10 October 2012 - 12:29 PM

View PostRyano121, on 10 October 2012 - 12:20 PM, said:

Here is your looping condition at the moment -

while (!(inputMessage.equals(" ")))


which essentially reads loop while the input does not equal a space. What bit would a need to change to get it to loop whilst the input is not "EXIT"?

the program has to wait user. imaen for example:

user:
save
program:
save
save command
After that the program should wait for user's typing.
Then user type smth else.
Program: answer him. And finally user will tired and type: EXIT.
The program should stop ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1