I need to do IRC client and I have a problem with login. I am trying to loop through server input to find the massage 004 or 433 which states for success login or for login in use. This is my code: `
import ecs100.*;
import java.io.*;
import java.net.Socket;
import java.util.*;
public class ChatClient {
private String server = "irc.ecs.vuw.ac.nz";
private static final int IRC_PORT = 6667;
private static String nick;
private static String name;
private Socket socket;
private Scanner serverIn;
private PrintStream serverOut;
private String line;
private String channel;
// Constructor
public ChatClient (){
this.setupGUI();
}
public void setupGUI(){
UI.addButton("Connect", this::connect);
}
public void connect(){
try {
socket = new Socket(server,IRC_PORT);
serverIn = new Scanner(socket.getInputStream());
serverOut = new PrintStream(socket.getOutputStream());
UI.println("login");
this.login();
UI.println("listening");
this.listenToServer();
}catch(IOException e){UI.println("IO failure "+ e);}
}
private boolean login(){
String username = UI.askToken("Enter your usercode: ");
String realname = UI.askString("Enter your real name: ");
this.nick = username;
this.name = realname;
send("NICK " + username);
send("USER " + username + " 0 unused :" + realname);
UI.println("listening to server");
while(this.serverIn.hasNext()){
this.line = serverIn.nextLine();
UI.println("SERVER" + line);
if (line.indexOf("004") >= 0){
UI.println("true");
return true;
} else if(line.indexOf("433") >= 0){
UI.println("failed to connect, username already in use");
return false;
} else {
UI.println("else");
return false;
}
}
}
private void send(String msg){
serverOut.print(msg + "\r\n");
serverOut.flush();
}
private void listenToServer() {
while(this.serverIn.hasNext()){
String line = serverIn.nextLine();
UI.println("SERVER" + line);
if(line.equals("SQUIT")){
this.closeConnection();
}
if(line.equals("PING")){
String pingMsg = line.split(" ",2)[1];
this.send("PONG " + pingMsg);
}
}
}
public void closeConnection(){
try {
this.socket.close();
this.serverIn.close();
this.serverOut.close();
UI.println("Closed");
} catch(IOException e){UI.println("IO failure "+ e);}
}
public void joinChannel(){
String channel = UI.askString("Enter channel name: ");
send("JOIN " + channel);
}
public void leaveChannel(){
send("PART " + channel);
}
public static void main(String[] args) {
new ChatClient();
}
}
The problem is that it keeps trowing missing return statement in login method. Can anyone please look and my code and see what might be wrong here ?
EDIT: ok so when i change the if statement to two if's and put return false outside of the loop, program works. Can someone explain why the if/else statement inside of the while loop is not working ?
private boolean login(){
String username = UI.askToken("Enter your usercode: ");
String realname = UI.askString("Enter your real name: ");
this.nick = username;
this.name = realname;
send("NICK " + username);
send("USER " + username + " 0 unused :" + realname);
UI.println("listening to server");
while(this.serverIn.hasNext()){
this.line = serverIn.nextLine();
UI.println("SERVER" + line);
if (line.indexOf("004") >= 0){
UI.println("true");
return true;
}
if(line.indexOf("433") >= 0){
UI.println("failed to connect, username already in use");
return false;
}
}
UI.println("else");
return false;
}
enter code here