#Im having issues using 2 different classes
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
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?
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());
}
}```
Detected code, here are some useful tools:
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());
}
}
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() {
}
}
Detected code, here are some useful tools:
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() {
}
}
@sweet skiff what is the problem?
I cant seem to connect it to my CheckString.java
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)
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
How do I do length like I did?
What im trying to do is find the middle letter in the word
Wdym
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
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());
}
}```
Detected code, here are some useful tools:
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());
}
}
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);
}
}
}
}
Detected code, here are some useful tools:
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);
}
}
}
}
for some reason im still getting issues even tho it seems correct
Why is vokal declared outside the method ? It should be inside
And please add variables
Instead of calling charAt several times, call it once
And countVowels doesn't return anything that's not normal
if(str.charAt(i) == 'a' || 'e' || 'i' || 'o' ||
doesnt like this doesnt work tho
if(str.charAt(i) == 'a' || 'e' || 'i' || 'o' ||
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;
}
}```
Detected code, here are some useful tools:
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;
}
}
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
No, keep your previous code
You can't return an int when it except a string
So how can I do it
To return with the numbers inside
figured that out
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.
i want it to show all vocales
Sure! Here are all the vowels: a, e, i, o, u.
Is problem solved?
No
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;
}
}
}
}```
Detected code, here are some useful tools:
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;
}
}
}
}
This is what ive written so far
What you want to achieve?
To write all the vocales in a word
If you want a word.. i consider word as strong, so first of all, why returning a int?
i tried returning str
didnt work
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
Vokal is int, don't you think, make it a string first?
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*
Yes, now why there is ; after if condition? There should not be any
Because it breaks there, not going inside the if
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 ??
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
i is index pointed to a memory, but str.charAt(i) gives you the character
At that memory
ah
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)
int
its supposed to return the count
a number
for example 5 for "i found 5 vocals"
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 
@karmic island his problem is this i guess
Hmmm 
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
It only needs to count, you told above to write ....
No, I don't mean that, I mean u need to revert changes I made u do
This
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;
Read it again
Before for loop, i state
Inside for loop
After for loop
oh like this?
Right, now read what I said for inside for-loop
Now replace all the charAt by a variable
Bot already given you outcome @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;```
Detected code, here are some useful tools:
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;
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
Hmm great
