Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
|
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 21st, 2003, 10:18 AM
Registered User
 
Join Date: Oct 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Loevet
Default Java code problem

Hi there!

I am new to java programming, and I am trying to build a questionaire program in java. I am trying to do this with arrays, byt the problem I'm having is that I can't compare the variables of these arrays in different methods. How can I compare an array variable in one method with an array variable in another method?

Thank you in advance,
 - Andreas Eklöv

Freedom shall prevail
// OpenSource
 
Old October 21st, 2003, 11:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You don't say why you can't compare the values of the different arrays. Are the arrays of the same type? Are the arrays local to each method? Are you passing in the whole array or a specific element? What are you using to do the compare for equality? I could go on...

Post your code and provide a description of the error, as this will help in solving your problem.

cheers

Martyn
 
Old October 21st, 2003, 05:09 PM
Registered User
 
Join Date: Oct 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Loevet
Default

Quote:
quote:Originally posted by Martyn
 You don't say why you can't compare the values of the different arrays. Are the arrays of the same type? Are the arrays local to each method? Are you passing in the whole array or a specific element? What are you using to do the compare for equality? I could go on...

Post your code and provide a description of the error, as this will help in solving your problem.

cheers

Martyn
Ok, I will list the code below.

/////////////////////////////////////////////////////

import java.io.*;
import java.util.Arrays;
public class FrageSport
{
public static void main(String[]args) throws IOException
{
String tom_strang = "";
String slut_strang=jamFor(tom_strang);

System.out.println(slut_strang);

}

public static String lagraSvar(String utInfo) throws IOException

{

String[] frageKorrekt = new String[5];
frageKorrekt[0] = "Bill Gates";
frageKorrekt[1] = "Steve Ballmer";
frageKorrekt[2] = "Apple";
frageKorrekt[3] = "Steve Jobs";
frageKorrekt[4] = "Sun";

}

public static String skrivSvar(String inText, String utText) throws IOException
{

BufferedReader bf_lasare = new BufferedReader(new InputStreamReader(System.in));

String[] listaSvar = new String[5];

System.out.println("Fråga 1: Vem är styrelseordförande på Microsoft?");
String string_lasSvar = bf_lasare.readLine();
listaSvar[0] = string_lasSvar;

System.out.println("Fråga 2: Vem är VD på Microsoft?");
string_lasSvar = bf_lasare.readLine();
listaSvar[1] = string_lasSvar;

System.out.println("Fråga 3: Vilket företag var före Microsoft med ett grafiskt operativsystem på marknaden?");
string_lasSvar = bf_lasare.readLine();
listaSvar[2] = string_lasSvar;

System.out.println("Fråga 4: Vem är nuvarande VD på Apple?");
string_lasSvar = bf_lasare.readLine();
listaSvar[3] = string_lasSvar;

System.out.println("Fråga 5: Vilket företag var det som skapade programmeringsspråket Java?");
string_lasSvar = bf_lasare.readLine();
listaSvar[4] = string_lasSvar;

}

public static String jamFor(String utStrang) throws IOException
{
int i;
int Korrekt = 0;
for (i=0; i < 5; i++)
{
// This is where the error occurs, the arrays listaSvar and
// frageKorrekt can't be read.
    if (Arrays.equals(listaSvar[i],frageKorrekt[i]))
    {
        Korrekt =+ 1;
        utStrang = "Du hade " + Korrekt + " korrekta svar.";

    }
    return utStrang;
}


}

}

//////////////////////////////////////////////////////////////

This is the code, I hope you can make some sence of it.

Thank you in advance,

 - Andreas Eklöv

Freedom shall prevail
// OpenSource
 
Old October 22nd, 2003, 07:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

First the good news...

The error you are encountering is because Arrays.equals() does not have a method to accept two String values. Cahnge this code to use the String implementation of .equals() e.g.

if (listaSvar[i].equals(frageKorrekt[i]))

Now for the bad news...

You still have lots of errors in your code. I won't tell you where they are as that will spoil your fun and you won't learn as much if I tell you.

A couple of hints to help you...

Do you need are declaring a method as having a return type other than void then you need to return a value of that type. Have a look at your string declarations.

If you declare an object in a method that object is local to that method. Have a look at where you are declaring your arrays.

If you call a method and it uses an array that is not inititalised you are going to get a null pointter exception.

I hope this helps.

Martyn
 
Old October 22nd, 2003, 10:45 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Andreas

I spent my lunctime playing with your code and decided that it would be easier all round if I posted you a clean compile version as some of the mistakes you were making would still have allowed your program to compile and as a result you wouldn't have leant from them.

I hope you don't mind me doing this as I am only trying to help. Give me a shout if there is anything you don't underatand.

Cheers

Martyn

Code follows below...


import java.io.*;

public class FrageSport
{

    int items = 5;

    String[] question = {
        "FrÃ¥ga 1: Vem är styrelseordförande pÃ¥ Microsoft?",
        "FrÃ¥ga 2: Vem är VD pÃ¥ Microsoft?",
        "FrÃ¥ga 3: Vilket företag var före Microsoft med ett grafiskt operativsystem pÃ¥ marknaden?",
        "FrÃ¥ga 4: Vem är nuvarande VD pÃ¥ Apple?",
        "FrÃ¥ga 5: Vilket företag var det som skapade programmeringssprÃ¥ket Java?"};

    String[] frageKorrekt = {
        "Bill Gates",
        "Steve Ballmer",
        "Apple",
        "Steve Jobs",
        "Sun"};

    String[] listaSvar = new String[items];

    public static void main(String[]args)
    {
        FrageSport fs = new FrageSport();
        fs.skrivSvar();
        fs.jamFor();

    }

    public FrageSport()
    {

    }


    public void skrivSvar()
    {
           try
           {
               BufferedReader bf_lasare = new BufferedReader(
                   new InputStreamReader(System.in));

            for(int i = 0; i < items; i++)
            {
                System.out.println(question[i]);
                listaSvar[i] = bf_lasare.readLine();
            }
        }
        catch(IOException ioe)
        {
            System.out.println("An IO error has occurred:\n" + ioe.getMessage());
        }
    }

    public void jamFor()
    {
        int korrekt = 0;

        for (int i=0; i < items; i++)
        {
            if (listaSvar[i].equals(frageKorrekt[i]))
            {
                korrekt++;
            }
        }

        System.out.println("Du hade " + korrekt + " korrekta svar.");
    }

}
 
Old October 23rd, 2003, 08:42 AM
Registered User
 
Join Date: Oct 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Loevet
Default

Thank you Martyn, I was able to solve the problem using my old code, but with the pointers of your code. I had simply mixed up my own thought, I was on the right path but the method calls were incorrect. Thank you for your help, it was great of you to out such effort into helping me.

Cheers // Andreas

Freedom shall prevail
// OpenSource





Similar Threads
Thread Thread Starter Forum Replies Last Post
Java script code kethireddy435 Javascript 1 July 30th, 2008 01:50 PM
Java Script from code behind jagdishnc ASP.NET 1.0 and 1.1 Basics 0 January 22nd, 2008 06:37 AM
problem with code to "Professional Java Data superea_net Java Databases 0 August 1st, 2006 07:07 AM
Java Code - Wrox - Beginning JAVA - Ivor Horton ponguru Java Databases 3 May 18th, 2006 12:30 PM
the inherit problem in java code. benni J2EE 6 November 13th, 2003 06:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.