2

Hello I am trying to figure out how arrays work and now i am created a class that looks for a integer in a class but i think i've done something work.. especially the method convert is wrong i think.. it has to search in the array if the array index contains that value and if so print that out but it doesn't work can someone explain the logics and how to solve it? ( with an example if possible )

thanks in advance! below you can see my code:

package h05UitgeschrevenGetal;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class GetalConverter extends JPanel implements ActionListener {
private JLabel getal;
private JTextField invoer, uitvoer;
private int invoerWaarde;
private JButton converteer;
String[] klein = new String[20];
String[] groot = new String[10];
public GetalConverter() {
 getal = new JLabel("geef een geheel getal");
 add(getal);
 invoer = new JTextField(7);
 invoer.addActionListener(this);
 add(invoer);
 converteer = new JButton("In woorden");
 converteer.addActionListener(this);
 add(converteer);
 uitvoer = new JTextField(7);
 uitvoer.setEditable(false);
 add(uitvoer);
 klein[0] = "nul";
 klein[1] = "een";
 klein[2] = "twee";
 klein[3] = "drie";
 klein[4] = "vier";
 klein[5] = "vijf";
 klein[6] = "zes";
 klein[7] = "zeven";
 klein[8] = "acht";
 klein[9] = "negen";
 klein[10] = "tien";
 klein[11] = "elf";
 klein[12] = "twaalf";
 klein[13] = "dertien";
 klein[14] = "veertien";
 klein[15] = "vijftien";
 klein[16] = "zestien";
 klein[17] = "zeventien";
 klein[18] = "achtien";
 klein[19] = "negentien";
 groot[1] = "tien";
 groot[2] = "twintig";
 groot[3] = "dertig";
 groot[4] = "veertig";
 groot[5] = "vijftig";
 groot[6] = "zestig";
 groot[7] = "zeventig";
 groot[8] = "tachtig";
 groot[9] = "negentig";
}
public void setValue(int val) {
 invoerWaarde = val;
 //System.out.println(invoerWaarde);
}
public int getValue() {
 return invoerWaarde;
}
public void convert() {
 // here comes the converting code
}
@Override
public void actionPerformed(ActionEvent e) {
 setValue(Integer.parseInt(invoer.getText()));
 convert();
 //System.out.println(invoerWaarde);
}
}
asked Oct 13, 2012 at 19:26

3 Answers 3

4

You have defined your array as: -

String[] strings = new String[klein];

Now, when you are invoking contains method: -

Arrays.asList(strings).contains(waar)

You are actually checking for the containment of an integer in an ArrayList of type String. Since, waar is an integer you passed in your convert method. How do you expect it to return a true value?

it has to search in the array if the array index contains that value

For that case, you need to pass that value to your convert method. You are rather passing index.

I guess you want something like this: -

public void convert(String value) {
 for (int i = 0; i < strings.length; i++) {
 if (strings[i].equals(value)) {
 System.out.println("Value: " + strings[i]);
 System.out.println("Index: " + i);
 }
 }
}
answered Oct 13, 2012 at 19:29
9
  • ye i tought of something like that already ... but how can i look to the "index" ? Commented Oct 13, 2012 at 19:31
  • @Reshad. But you are passing an integer value. Whose index you want to fetch? Commented Oct 13, 2012 at 19:33
  • no that is not possible since the value for convert is an integer.. not a string i am trying to create something that converts 1 into one and 200 into two hundred for example Commented Oct 13, 2012 at 19:51
  • @Reshad. OK. Then how would you compare 1 and one? Commented Oct 13, 2012 at 19:53
  • Well that is my problem.. but another problem I just noticed is my array has to be like written out ( atleast the values ) because now i have this loop that goes up by 1 each time.. but nothing goes into the value like value[1] = "one" so i have to write it out right? Commented Oct 13, 2012 at 20:12
1
String findValue = ...;
for(String str: strs){
 if(str.equals(findValue)){
 return true;
 }
}

if you are looking for index -

public void convert(Integer waar) {
 for(int index=0;index<strs.length;index++){
 if(strs[index].equals(String.valueOf(waar))){
 System.out.println("index -> "+index);
 }
 }
}
answered Oct 13, 2012 at 19:31
2
  • ah the second example is what i needed! could you know by accident also how to print the value of the index when found? Commented Oct 13, 2012 at 19:39
  • print the value of the index you can put just into Sysout(i); Commented Oct 13, 2012 at 19:46
1

I know for a fact, that from the code given above, your program will only work in the range of 0 to 99. Try this code out:

public void Convert()
{
 int grootVal, kleinVal;
 if(invoerWaarde < 100){
 //ok, possibility of finding it in the array.
 grootVal = (int)Math.floor(invoerWaarde / 10.0)
 kleinVal = invoerWaarde - (grootVal * 10);
 try{
 String valueString = groot[grootVal] + " " + klein[kleinVal];
 System.out.println(valueString);
 }catch(ArrayOutOfBoundsException aoobe){
 System.out.println("");
 }
 }
}
answered Oct 14, 2012 at 14:03
3
  • it works for the most values but not for every one unfortunately but with a little bit modification it will i hope! Commented Oct 14, 2012 at 14:19
  • 10 and below 10, i think it needs some if statements because it says the 0 even if the 0 does not exist in that number for example 5 is becoming zero five instead of just five etc.. Commented Oct 14, 2012 at 20:58
  • what is becoming 0 when 0 doesn't exist? Commented Oct 14, 2012 at 21:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.