0

Hey I'm trying to call a string from my server class to check if the numbers of an array in my client are in order but i'm having difficulties getting the value of the return of the string in my class here's what I have so far...

import java.util.Scanner;


public class Main {
    public static void main(String[] args){
        System.out.println("Insert 10 numbers in order from least to greatest");
        Scanner input = new Scanner(System.in);
        int[] array = new int[10];
        for(int i=0; i<array.length; i++){
            int x = input.nextInt();
        }
    }
}

Client Above ^^^^

Server Below VVVV

public class Server {
    public String checkFalse(int[] array){
        String checker = "Error, the numbers are not in order";
        int number = 0;
        for(int i =0; i<array.length-1; i++){
            number = array[i];
            if(array[i+1] < number){
                return checker;
            }
    }
        return null;

}
}

"number" in the server is merely a placeholder of the value at the index "i" to see if the value at the next index is less than that number at i

so basically how would I get the return of checkFalse in the server class? Any help is appreciated thanks! (Also first question so don't kill me for errors in typing question etc)

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Adam Calcanes
  • 89
  • 1
  • 3
  • 8
  • Do the client and server run in separate threads within the same VM? Or different VMs on the same machine? How about different machines? It'll be easier to offer some help if we know the constants you need to live with. – Jason Braucht Dec 16 '13 at 01:47
  • There are two different classes running side-by-side on Eclpise – Adam Calcanes Dec 16 '13 at 01:50

2 Answers2

0

You could simply create and call Server from your Main class.

import java.util.Scanner;

public class Main {
public static void main(String[] args){
    System.out.println("Insert 10 numbers in order from least to greatest");
    Scanner input = new Scanner(System.in);
    int[] array = new int[10];
    for(int i=0; i<array.length; i++){
        int x = input.nextInt();
        array[i] = x;
    }

    Server server = new Server();
    String response = server.checkFalse(array);
    System.out.println(response);
}

}

EDIT It's worth pointing you aren't storing the input from Scanner into your array (take a look at the edits I made in my answer)

To make checkFalse() static, you could do the following

public class Main {
    public static void main(String[] args){
        System.out.println("Insert 10 numbers in order from least to greatest");
        Scanner input = new Scanner(System.in);
        int[] array = new int[10];
        for(int i=0; i<array.length; i++){
            int x = input.nextInt();
            array[i] = x;
        }

        String response = Server.checkFalse(array);
        System.out.println(response);
    }
}

(Note that we don't need to explicitly create an instance of Server to call checkFalse())

public class Server {
    public static String checkFalse(int[] array){
        String checker = "Error, the numbers are not in order";
        int number = 0;

        for(int i =0; i<array.length-1; i++){
            number = array[i];
            if(array[i+1] < number){
                return checker;
            }
        }
        return "The numbers are in order!";  
    }
}
Jason Braucht
  • 2,358
  • 19
  • 31
  • This works, thanks a lot but is there a way to do this without creating a new server? My teacher would most definitely take off points for using this method as we haven't learned this yet. – Adam Calcanes Dec 16 '13 at 02:02
  • Yes, you could make `checkFalse()` a static method. I'll update the answer with an example. – Jason Braucht Dec 16 '13 at 02:06
  • Since this looks like a homework assignment you might want to read up on the [Difference between Static methods and Instance methods](http://stackoverflow.com/questions/11993077/difference-between-static-methods-and-instance-methods) :) – Jason Braucht Dec 16 '13 at 02:13
  • Well it's part of a project for school I just couldn't fix this one area I wrote the code in class forgot to put it on my USB and just created this jumbled mess at home I understand how static works I just kept messing with both classes and copied the version without static because my static version had a plethora of errors. Lol – Adam Calcanes Dec 16 '13 at 02:34
0

To execute these with different Threads you can use DatagramSocket and DatagramPacket to send and receive a data. Send integer from client to choosen port of server and listen port from server to receive data.

karakale
  • 126
  • 11