How to calculate the value of average?

    int sum = 0;
    char c = 'O';
    for(int i=0;i<testScores.length;i++)
        sum = sum +testScores[i];
    int avg = sum/testScores.length;
    if(90<=avg&& avg>=100)
       return c; 
    else if(80<=avg&& avg>90)
         return c;

       else if(70<=avg&& avg>80)
        return c;

        else if(55<=avg&& avg>70)
           return c ;  

           else if(40<=avg&& avg>55){
                return c;

       return c; 

Above I have defined my calculate() which calculates the average of student but because of some error i am not getting value of average.

How to configure CPLEX on Netbeans?

I want to configure CPLEX on Netbeans using Java but when I run a test example,I get the following error message :

enter image description here

What I did : I added the CPLEX Jar library by a right click on Libraries and then Add JAR/Folder. Then I went in Run->Set Project Configuration -> Customize and in the arguments, I added

-Djava.library.path = C:Program FilesIBMILOGCPLEX_Studio1271cplexbinx64_win64cplex1271.dll

Could someone help me ?

Adding JPanel to JFrame

I have a program in which a JPanel is added to a JFrame:

public class Test{

    Test2 test = new Test2();
    JFrame frame = new JFrame();


    frame.setLayout(new BorderLayout());
    frame.add(test, BorderLayout.CENTER);





    public class Test2{

    JPanel test2 = new JPanel();





I get an error asking me to change type of ‘panel’ to ‘component’. I do I fix this error?
It wants me to do: Component panel = new Component();

How to relax autocommit in Java JDBC when updating a MySQL database online?

This statement works:

Connection m = DriverManager.getConnection("jdbc:mysql://localhost:3306/imageusers?autoReconnect=true&useSSL=false&relaxAutoCommit=true", "username", "password");

I uploaded my database to a free online website to use wherever and updated my code as follows:

Connection myConn = DriverManager.getConnection("jdbc:mysql://","serverName","serverPassword");

this works correctly but the message

java.sql.SQLException: Can't call commit when autocommit=true

Appears in red in the console. I tried to update the new statement with the relax autocommit statement but that gave me a connection error. How can I properly do it so the message no longer appears? The program works but the red exception in the console isn’t pretty.

modeling polymorphism in UML – dependency to abstract classes

I have searched many hours over the course of the last few semesters trying to make sure that I am presenting this correctly to my students. Nothing I have found shows this particular relationship. This semester I’d like KNOW that I’m doing it correctly.

We have an abstract class, Player and two concrete classes that extend it (Pitcher and PositionPlayer). Then we have a class, Team with a map declared to be

    <Integer, Player>  

To me it seems that Team is dependent on Player (the abstract class) but I suppose dependence could be shown for the subclasses. Please tell me which of these is correct, the top one or the bottom one? (my UML editor won’t do italics, hence the tags). Thank you in advance for your help. UML for Baseball player classes

Java : nested loops

I’m very new in Java Programming Language.

I asked to make something like this with nested loops method:


“Masukan Angka” is “Input Number” in Indonesian Language. So if we input 9, it will print out 9 lines of * and the amount of * decreased for each line.

I tried it with nested loops, this is what i made :picture2

The code is :

    public static void main(String args[]) {
        Scanner in = new Scanner(;
        System.out.print("Input your number: ");
        int x = in.nextInt();
        for (int y = x; y > 0; y--) {
            for (int z = 0; z < y; z++)

How can i make it doesn’t filled up with * in the line 2-7, but instead filled with blank space like the example in the first picture?

Thanks in advance.

Java download webpage with cookies

I want to download the html source code of a website. Is it possibly to “fake” cookies, so that the website sees for example that the cookie “loggedin” is set to “true”, even though you arent actually logged in. I’ve seen a few tutorials how to do this, but everywhere it was done diffrently. I tried a few method but nothing worked.

I tried using this method for example:

public static String getWebPage(URL url, HashMap<String, String> cookies) {
try {
    InputStreamReader isr = null;
    HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
    httpcon.addRequestProperty("User-Agent", "Chrome/61.0");
    if (cookies != null) {
        StringBuilder sb = new StringBuilder();
        int count = 0;
        for (Map.Entry<String, String> cookie : cookies.entrySet()) {
            if (count != 0)
            sb.append(cookie.getKey() + "=" + cookie.getValue());
        httpcon.addRequestProperty("Cookie", sb.toString());
    isr = new InputStreamReader(httpcon.getInputStream());
    BufferedReader br = new BufferedReader(isr);
    String line = br.readLine();
    StringBuilder sb = new StringBuilder();
    while (line != null) {
        line = br.readLine();

    return sb.toString();
} catch (Exception e) {
    return "";


What am i doing wrong?

Call MS Studio C dll from Java JNA – error msg

My hello.c file in MS Studio 2015 Win 10:

#include <stdio.h>

extern void hello();

void hello()
    printf("Hello from C");

My Java file in eclipse:

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;

public class PaMMTE {

       public interface CPaMMTE_testDLL extends Library {
           public void welcome();

       public static void main(String[] args) {
           CPaMMTE_testDLL lib = (CPaMMTE_testDLL) Native.loadLibrary("TestCallingFromJava", CPaMMTE_testDLL.class);
           System.out.println("Hello, World");

(The name of the dll is CPaMMTE_testDLL.)

Anyway, I’ve got the following error message:

> Exception in thread "main" java.lang.UnsatisfiedLinkError: %1 is not a
> valid Win32 application.
>   at Method)   at
> com.sun.jna.NativeLibrary.loadLibrary(     at
> com.sun.jna.NativeLibrary.getInstance(     at
> com.sun.jna.Library$Handler.<init>(  at
> com.sun.jna.Native.loadLibrary(   at
> com.sun.jna.Native.loadLibrary(   at
> PaMMTE.main(

I’m pretty sure that the configuration is wrong in MS Studio project.
I’ve tried several different configurations without any good result.

I’ve changed on the MS Studio Win 10 configuration:

> Exception in thread "main" java.lang.UnsatisfiedLinkError: Error
> looking up function 'hello': The specified procedure could not be
> found.
>   at com.sun.jna.Function.<init>(   at
> com.sun.jna.NativeLibrary.getFunction(     at
> com.sun.jna.NativeLibrary.getFunction(     at
> com.sun.jna.NativeLibrary.getFunction(     at
> com.sun.jna.Library$Handler.invoke(  at
> com.sun.proxy.$Proxy0.hello(Unknown Source)   at
> PaMMTE.main(

The second one looks better. 🙂

My first problem was that I used 32-bit compile instead of 64 bit. But I still don’t find the right MS config.

Thanks for your help!

config 1
config 3

Java: Troubles with creating an foreach loop.

I’m having troubles with creating a foreach-loop which is supposed to return an object in an ArrayList with the highest attribute. I’ve created a class named Person which has a attribute of friends. The method connectedFriend is supposed to return the objects friends who has the highest amount of friends. I’m sorry if this doesn’t make any sense. I’m new to this.