#Im having issues using 2 different classes

1 messages · Page 1 of 1 (latest)

sweet skiff
#

Im having issues using 2 different classes, i have an assignement with a UML diagram to use 2 differnet classes

P4_2.java and CheckString.java
Im not sure how to link them together, seems like im having issues with that

sick masonBOT
#

<@&987246399047479336> please have a look, thanks.

sick masonBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

ow to use them together. Can you provide more information about the assignment and the specific issues you are facing?

sweet skiff
#

This is what ive written so far but idk how to connect them together

#

import java.util.*;

public class P4_2 {

    public static void main(String[] args) {
        CheckString aString = new CheckString("0");
        CheckString bString = new CheckString("1");
        CheckString cString = new CheckString("3");
        Scanner las = new Scanner(System.in);
        System.out.print("Skriv in tre ord");
        String a = las.nextLine();
        String b = las.nextLine();
        String c = las.nextLine();

        aString = new CheckString(a);
        bString = new CheckString(b);
        cString = new CheckString(c);

        System.out.println(aString.getString());
        System.out.println(bString.getString());
        System.out.println(aString.middle());
        System.out.println(aString.countVowels());
        System.out.println(bString.middle());
        System.out.println(bString.countVowels());

    }
}```
sick masonBOT
# sweet skiff ```package lab4; import java.util.*; public class P4_2 { public static vo...

Detected code, here are some useful tools:

Formatted code
package lab4;

import java.util. * ;

public class P4_2 {
  public static void main(String[] args) {
    CheckString aString = new CheckString("0");
    CheckString bString = new CheckString("1");
    CheckString cString = new CheckString("3");
    Scanner las = new Scanner(System.in);
    System.out.print("Skriv in tre ord");
    String a = las.nextLine();
    String b = las.nextLine();
    String c = las.nextLine();
    aString = new CheckString(a);
    bString = new CheckString(b);
    cString = new CheckString(c);
    System.out.println(aString.getString());
    System.out.println(bString.getString());
    System.out.println(aString.middle());
    System.out.println(aString.countVowels());
    System.out.println(bString.middle());
    System.out.println(bString.countVowels());
  }
}
sweet skiff
#

public class CheckString {
    private String str;
    public CheckString(String str) {
        this.str = str;
    }

    public String getString() {
        return str;
    }

    public String middle() {
        if (str.length() % 2 == 0) {

            int ena = str[(str.length / 2) - 1];
            int andra = str[(str.length / 2)];
            System.out.print(ena + andra);
            // even

        } else {
            // odd
            int udda = str[(str.length / 2)];
            System.out.print(udda);
        }
    }

    public int countVowels() {

    }
}
sick masonBOT
# sweet skiff ```package lab4; public class CheckString { private String str; public ...

Detected code, here are some useful tools:

Formatted code
package lab4;

public class CheckString {
  private String str;
  public CheckString(String str) {
    this .str = str;
  }
  public String getString() {
    return str;
  }
  public String middle() {
    if (str.length() % 2 == 0) {
      int ena = str[(str.length / 2) - 1] ;
      int andra = str[(str.length / 2) ] ;
      System.out.print(ena + andra);
      // even
    }
    else {
      // odd
      int udda = str[(str.length / 2) ] ;
      System.out.print(udda);
    }
  }
  public int countVowels() {
  }
}
feral escarp
#

@sweet skiff what is the problem?

sweet skiff
#

I cant seem to connect it to my CheckString.java

feral escarp
#

What does connect mean ?

#

And why can't you do it ?

#

Do you get an error or ?

sweet skiff
#

When I run I get this

#

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The type of the expression must be an array type but it resolved to String
length cannot be resolved or is not a field
The type of the expression must be an array type but it resolved to String
length cannot be resolved or is not a field
The type of the expression must be an array type but it resolved to String
length cannot be resolved or is not a field

at lab4/lab4.CheckString.middle(CheckString.java:16)
at lab4/lab4.P4_2.main(P4_2.java:23)
feral escarp
#

Are you not using an ide ?

#

You could have red underlines with nice errors

#

@sweet skiff

#

And for this error

#

A String is not an array, you can't use [] on a String

#

And it's length(), not length

sweet skiff
#

What im trying to do is find the middle letter in the word

feral escarp
#

It's length(), not length

#

You seem to mix String and arrays

#

And you can't use [] on String

#

That's for arrays

#

On String, you want to use .charAt(int) method

sweet skiff
#

I see

#

so my assignement is to do one where it sends the 3 words you sent back

#

and then the middle letters of the words you sent

#

and then the vocals

#

Like this uml diagram

#

This is what iv ewritten so far

#

import java.util.*;

public class P4_2 {

    public static void main(String[] args) {
        CheckString aString = new CheckString("0");
        CheckString bString = new CheckString("1");
        CheckString cString = new CheckString("3");
        Scanner las = new Scanner(System.in);
        System.out.print("Skriv in tre ord");
        String a = las.nextLine();
        String b = las.nextLine();
        String c = las.nextLine();

        aString = new CheckString(a);
        bString = new CheckString(b);
        cString = new CheckString(c);

        System.out.println(aString.getString());
        System.out.println(aString.middle());
        System.out.println(int.countVowels());
        System.out.println(bString.getString());
        System.out.println(bString.middle());
        System.out.println(bString.countVowels());
        System.out.println(cString.getString());
        System.out.println(cString.middle());
        System.out.println(cString.countVowels());

    }
}```
sick masonBOT
# sweet skiff ```package lab4; import java.util.*; public class P4_2 { public static vo...

Detected code, here are some useful tools:

Formatted code
package lab4;

import java.util. * ;

public class P4_2 {
  public static void main(String[] args) {
    CheckString aString = new CheckString("0");
    CheckString bString = new CheckString("1");
    CheckString cString = new CheckString("3");
    Scanner las = new Scanner(System.in);
    System.out.print("Skriv in tre ord");
    String a = las.nextLine();
    String b = las.nextLine();
    String c = las.nextLine();
    aString = new CheckString(a);
    bString = new CheckString(b);
    cString = new CheckString(c);
    System.out.println(aString.getString());
    System.out.println(aString.middle());
    System.out.println(int .countVowels());
    System.out.println(bString.getString());
    System.out.println(bString.middle());
    System.out.println(bString.countVowels());
    System.out.println(cString.getString());
    System.out.println(cString.middle());
    System.out.println(cString.countVowels());
  }
}
sweet skiff
#

public class CheckString {
    private String str;

    public CheckString(String str) {
        this.str = str;
    }

    public String getString() {
        return str;
    }

    public String middle() {
        if (str.length() % 2 == 0) {

            int ena = (str.length() / 2) - 1;
            int andra = str.length() / 2;
            // even

        } else {
            // odd
            int udda = str.length() / 2;
            System.out.print(udda);
        }
        return str;
    }
int vokal = 0;
    public int countVowels() {
         for (int i = 0; i < str.length(); i++) {
            vokal = i + vokal;    
        if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || 
                str.charAt(i) == 'ö' || str.charAt(i) == 'å' || str.charAt(i) == 'u'  || str.charAt(i) == 'ä'); {
                    
                    System.out.println("\n" + vokal);
            
                
                    
        }
                
        }

    }
}
sick masonBOT
# sweet skiff ```package lab4; public class CheckString { private String str; public...

Detected code, here are some useful tools:

Formatted code
package lab4;

public class CheckString {
  private String str;
  public CheckString(String str) {
    this .str = str;
  }
  public String getString() {
    return str;
  }
  public String middle() {
    if (str.length() % 2 == 0) {
      int ena = (str.length() / 2) - 1;
      int andra = str.length() / 2;
      // even
    }
    else {
      // odd
      int udda = str.length() / 2;
      System.out.print(udda);
    }
    return str;
  }
  int vokal = 0;
  public int countVowels() {
    for (int i = 0; i < str.length(); i++) {
      vokal = i + vokal;
      if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'ö' || str.charAt(i) == 'å' || str.charAt(i) == 'u' || str.charAt(i) == 'ä');
        {
        System.out.println("\n" + vokal);
      }
    }
  }
}
sweet skiff
#

for some reason im still getting issues even tho it seems correct

feral escarp
#

And please add variables

#

Instead of calling charAt several times, call it once

#

And countVowels doesn't return anything that's not normal

sweet skiff
#

if(str.charAt(i) == 'a' || 'e' || 'i' || 'o' ||

#

doesnt like this doesnt work tho

#

if(str.charAt(i) == 'a' || 'e' || 'i' || 'o' ||

sweet skiff
#

also

#
        if (str.length() % 2 == 0) {

            int forsta = (str.length() / 2) - 1;
             int andra = str.length() / 2;
             str = forsta + andra;
             String bada = forsta + andra;
            return bada;
            // even

        } else {
            // odd
            int udda = str.length() / 2;
            return udda;
        }
    }```
sick masonBOT
# sweet skiff ```public String middle() { if (str.length() % 2 == 0) { in...

Detected code, here are some useful tools:

Formatted code
public String middle() {
  if (str.length() % 2 == 0) {
    int forsta = (str.length() / 2) - 1;
    int andra = str.length() / 2;
    str = forsta + andra;
    String bada = forsta + andra;
    return bada;
    // even
  }
  else {
    // odd
    int udda = str.length() / 2;
    return udda;
  }
}
sweet skiff
#

this doesnt work

#

if my input is Hejsan
Hejsan
baloo

I get the output

#

2baloo
farjee
farjee

#

when im supposed to get
l
rj
rj

#

Right now in the code i have some missmathces tho aparrently

feral escarp
feral escarp
sweet skiff
#

To return with the numbers inside

sweet skiff
#

figured that out

sick masonBOT
#

At the end of the code snippet, there seems to be a syntax error. The code is incomplete and missing the closing parenthesis and semicolon. Here's the corrected version:

public int countVowels() {
    int vokal = 0;
    for (int i = 0; i < str.length(); i++) {
        if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') {
            vokal++;
        }
    }
    return vokal;
}

This code counts the number of vowels in a given string str. It initializes a variable vokal to 0 and then iterates over each character in the string. If the character is a vowel (either 'a', 'e', 'i', 'o', or 'u'), it increments vokal by 1. Finally, it returns the total count of vowels found in the string.

sweet skiff
#

i want it to show all vocales

sick masonBOT
#

Sure! Here are all the vowels: a, e, i, o, u.

civic wyvern
sweet skiff
#
    public int countVowels() {
         for (int i = 0; i < str.length(); i++) {
             int vokal = 0;
             vokal = i + vokal;    
        if(str.charAt(i) == 'a' ||
                str.charAt(i) ==  'e'||
                str.charAt(i) == 'i' ||
                str.charAt(i) == 'o' || 
                str.charAt(i) == 'ö' ||
                str.charAt(i) == 'å' || 
                str.charAt(i) == 'a' || 
                str.charAt(i) == 'u' ||
                str.charAt(i) == 'ä'); {
                    
                    
            
                
                    return vokal;
                    
    }
         }
    }
}```
sick masonBOT
# sweet skiff ``` public int countVowels() { for (int i = 0; i < str.length(); i+...

Detected code, here are some useful tools:

Formatted code
public int countVowels() {
  for (int i = 0; i < str.length(); i++) {
    int vokal = 0;
    vokal = i + vokal;
    if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'ö' || str.charAt(i) == 'å' || str.charAt(i) == 'a' || str.charAt(i) == 'u' || str.charAt(i) == 'ä');
      {
      return vokal;
    }
  }
}
}
sweet skiff
#

This is what ive written so far

civic wyvern
#

What you want to achieve?

sweet skiff
#

To write all the vocales in a word

civic wyvern
sweet skiff
#

didnt work

civic wyvern
#

Ok, so let's say your returning a string, so first you want to store it in a string,....
So, let's say initial string = ''
...empty

Then if you get a vowel while interacting, you need to add that into strong, not return...

When finish iterating whole input, u can return the value

sweet skiff
#

I did vokal+i = vokal

#

to get all the vokals

#

together'

civic wyvern
#

Vokal is int, don't you think, make it a string first?

sweet skiff
#

i get this error otherwise

#

still doesnt work'

civic wyvern
#

Because u need to do some more changes which are basic to java

#

see vocal is not initialised, first initialize it to empty string

#

Strong vocal = "";

#

Like that

#

And we want to vowels from whole input, right? So put vocal above for liip

#

Loop*

sweet skiff
#

like this?

civic wyvern
#

Yes, now why there is ; after if condition? There should not be any

#

Because it breaks there, not going inside the if

sweet skiff
#

ah

civic wyvern
#

Now let's say if a character is vowel, it does not need to return vocal.... it need to add char to vocal...
So inside if

vocal = vocal + str.charAt(i);

#

Right ??

sweet skiff
#

right but wouldnt it be the same thing

#

because i = str.charAt(i)

civic wyvern
#

Actually what u doing is, if I get a vowel return the ans, but I need to check the remaining indexes of i after current, so can't return without checking others

civic wyvern
#

At that memory

sweet skiff
#

am I not supposed to be returning smth with str?

#

thats what i did with the previous assignements

#

(this is to get the middle letter)

karmic island
#

int

#

its supposed to return the count

#

a number

#

for example 5 for "i found 5 vocals"

sweet skiff
#

ah

#

wait hold up

#

maybe i misunderstood the assignement

#

ill send u what it says

civic wyvern
#

What is str, input right?? So we need input to come with func calling

#

So pass str inside countVowels function

#

I am on a subway lol, when it goes underground, network lost cat_thumbs_up

civic wyvern
civic wyvern
sweet skiff
#

this is the uml diagram

#

ive done everything except for the last part

#

it says its supposed to be int

#

The countVwels() method counts the number of vowels in the stored string and returns the value.
As in the previous task, you need to create another class that creates and tests the functionality that
CheckString has

#

maybe you are right

#

maybe its supposed to be just the amount

civic wyvern
#

It only needs to count, you told above to write ....

sweet skiff
#

i thought i needed to write

#

im so sorry

civic wyvern
#

No, I don't mean that, I mean u need to revert changes I made u do

sweet skiff
#

here?

civic wyvern
#

So let's start again, return is int, so let's count the vowels

#

First before for loop, like before checking string, we have 0 count of vowels
So initialize
->. int count = 0;

#

Then inside for loop, let's i = 0, first and then checking if Char is vowel, inside if, we can increase count by 1, cause we got one more vowel..

So
count = count +1;

#

After for loop, return count which has count of all vowels in str..

return count;

sweet skiff
#

like this?

civic wyvern
#

Read it again

sweet skiff
#

oh like this?

civic wyvern
#

Right, now read what I said for inside for-loop

sweet skiff
#

isnt it what im doing tho

#

checks if its vowel

#

if then i++

feral escarp
civic wyvern
sweet skiff
#
        int vokal = 0;

        for (int i = 0; i < str.length(); i++) {

            if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o'
                    || str.charAt(i) == 'ö' || str.charAt(i) == 'å' || str.charAt(i) == 'a' || str.charAt(i) == 'u'
                    || str.charAt(i) == 'ä')
                ;
            {
                vokal++;

            }
        }
        return vokal;```
sick masonBOT
# sweet skiff ```public int countVowels() { int vokal = 0; for (int i = 0; i ...

Detected code, here are some useful tools:

Formatted code
public int countVowels() {
  int vokal = 0;
  for (int i = 0; i < str.length(); i++) {
    if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'ö' || str.charAt(i) == 'å' || str.charAt(i) == 'a' || str.charAt(i) == 'u' || str.charAt(i) == 'ä');
      {
      vokal++;
    }
  }
  return vokal;
sweet skiff
#

this is what ive done and it gives me the amount of numbers in the word now

#

like for instance if i give it

ABB

Its gonna give me 3

#

oh

#

i think i solved it, i think it was what u told me before

#

the ; in the if

#

Yes that was it

#

thank you so much its all solved

civic wyvern
#

Hmm great

sweet skiff
#

Apologies for being a mess

#

just started coding, seems exciting tho! 😄

civic wyvern
#

My subway journey ends here

#

Just got to the last station hue