Getting ')' expected. No idea why - java

I have a small part of code where I get an error when I try to compile. Any pointers?
The code:
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.size(); j++) {
if(fra.linjeList[i].equals(til.linjeList[j])){
Linje aktuellLinje=new Linje(linjerMap.get(linjeList[i]));
retning=aktuellLinje.stasjonsNummer(startStasjon) - aktuellLinje.stasjonsNummer(sluttStasjon);
endeStasjonsNavn=aktuellLinje.endestasjon(retning).stasjonsNavn;
System.out.println("Ta T-bane linje " + aktuellLinje.linjeNummer + " fra " + startStasjon + " til " sluttStasjon + " i retning " + endeStasjonsNavn + ". Estimert reisetid: " + tid);
}
}
}
}
And the error:
oblig5.java:132: error: ')' expected
System.out.println("Ta T-bane linje " + aktuellLinje.linjeNummer + " fra " + startStasjon + " til "
sluttStasjon + " i r etning " + endeStasjonsNavn + ". Estimert
reisetid: " + tid);
^

This is the culprit :
" til " sluttStasjon
Make it
" til " + sluttStasjon

You are missing a plus in
" til " sluttStasjon
^ HERE

You are missing a + before sluttStasjon in your print statement

Related

Cannot Access Variable inside the for loop

I'm trying to concatenate string values from clientGroupList to make a string variable. I cannot access to the returned variable. I'm working on shareString_8 and shareString_9.
if (uib.getClientProviderType().equals(ParamConstant.CLIENT_TYPE)) {
// ClientUserの場合の処理
//SM4|
String shareString_8 = shareStringList + ":SM" + Constant.SHARE_MODE_CLIENT_GROUP + "|";
for (int idx = 0; idx < clientGroupList.size(); idx++) {
//SM4|SHARE_CLIENT_GROUP
shareString_8 = shareString_8 + clientGroupList.get(idx);
}
return shareString_8;
} else {
// ProviderUserの場合の処理
//SM-1|
String shareString_9 = shareStringList + ":SM" + Constant.SHARE_MODE_PUBLISH + "|";
for (int idx = 0; idx < entityListClientGroup.size(); idx++) {
//SM-1|CLIENT_GROUP_ID
shareString_9 = shareString_9 + clientGroupList.get(idx);
}
return shareString_9;
}
String shareString = providerId + " AND " + "(" + shareString_1 + " OR " + shareString_2 + " OR " + shareString_3
+ " OR " + shareString_4 + " OR " + shareString_5 + " OR " + shareString_6 + " OR " + shareString_7
+ " OR " + shareString_8 + " OR " + shareString_9 + ")";

Eclipse java ASTParser ArrayIndexOutOfBoundsException error

i'm trying to make AST of java code in eclipse.
i'm following example of https://help.eclipse.org/2019-12/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjdt%2Fcore%2Fdom%2FASTParser.html
As Example of the link, i write my code to create sample AST
But, after run the code, i have ArrayIndexOutOfBoundsException error.
full code and error message is attached below
Does anyone know why this error happened?
my code
import java.util.Map;
import org.eclipse.jface.text.Document;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
public class ASTtest {
static String src = "public class Sample {\r\n" +
" public static void main(String[] args){\r\n" +
" case1();\r\n" +
" case2();\r\n" +
" case3();\r\n" +
" }\r\n" +
" static void case1(){\r\n" +
" int a = 1000;\r\n" +
" int b = 10000;\r\n" +
"\r\n" +
" for (int i = 0; i <100 ; i++) {\r\n" +
" if(a<i){\r\n" +
" a=i;\r\n" +
" }else{\r\n" +
" b--;\r\n" +
" }\r\n" +
" }\r\n" +
" }\r\n" +
" static void case2(){\r\n" +
" int a = 1000;\r\n" +
" int b = 10000;\r\n" +
" for (int i = 0; i <1000 ; i++) {\r\n" +
" if(a<i){\r\n" +
" a=i;\r\n" +
" }else{\r\n" +
" b--;\r\n" +
" }\r\n" +
" }\r\n" +
" }\r\n" +
" static void case3(){\r\n" +
" int a=0;\r\n" +
" int b=120847;\r\n" +
" while(a==10000){\r\n" +
" a++;\r\n" +
" b--;\r\n" +
" b+=200;\r\n" +
" for (int i = 0; i <1000 ; i++) {\r\n" +
" a+=1;\r\n" +
" a-=1;\r\n" +
" int k =1;\r\n" +
" while(k <10000){\r\n" +
" k++;\r\n" +
" }\r\n" +
" }\r\n" +
" }\r\n" +
" for (int i = 0; i < 900; i++) {\r\n" +
" a++;\r\n" +
" for (int j = 0; j <100 ; j++) {\r\n" +
" a--;\r\n" +
" a++;\r\n" +
" }\r\n" +
" a--;\r\n" +
" }\r\n" +
" }\r\n" +
"}\r\n" +
"";
public static void main(String[] args) {
char[] source = src.toCharArray();
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(source);
Map options = JavaCore.getOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_5, options);
CompilationUnit result = (CompilationUnit) parser.createAST(null);
}
}
Error message
> Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:11671)
at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:11924)
at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:11881)
at org.eclipse.jdt.internal.compiler.parser.Parser.dietParse(Parser.java:10286)
at org.eclipse.jdt.core.dom.CompilationUnitResolver.parse(CompilationUnitResolver.java:535)
at org.eclipse.jdt.core.dom.ASTParser.internalCreateAST(ASTParser.java:1227)
at org.eclipse.jdt.core.dom.ASTParser.createAST(ASTParser.java:823)
at ASTtest.main(ASTtest.java:78)
in error message , the problem line (ASTtest.java:78) is this
CompilationUnit result = (CompilationUnit) parser.createAST(null);

Convert numbers in an array to another array

I'm trying to create another array but I want it to be the array double gainT[], which is the temperature in Fahrenheit, converted to Celcius to make another array called double gainTC[]. I am also trying to do the same thing but with the precipitation. I just can't seem to find a way to do it though.
import java.io.IOException;
import java.util.Scanner;
public class AnnualClimate1 {
public static void main(String [] args) throws IOException
{
Scanner in = new Scanner(System.in);
System.out.print("Choose the temperature Scale (F = Fahrenheit, C = Celsius): ");
String tFC = in.nextLine();
System.out.print("Choose the precipitation Scale (I = Inches, C = Centimeters): ");
String pIC = in.nextLine();
System.out.println("");
System.out.println("");
System.out.println(" Climate Data");
System.out.println(" Location: Gainesville, Florida");
System.out.println(" Temperature " + tFC + " Precipitation " + pIC);
System.out.println("=================================================");
double gainT[]={54.3, 57.0, 62.5, 67.6, 74.3, 79.2, 80.9, 80.4, 77.8, 70.1, 62.8, 56.3};
double gainTC[] = {(gainT[] - 32) / 1.8};
double gainP[]={3.5, 3.4, 4.3, 2.9, 3.2, 6.8, 6.1, 6.6, 4.4, 2.5, 2.2, 2.6};
double gainPC[] = {gainP[] / .3937};
if(tFC.equalsIgnoreCase("F") && pIC.equalsIgnoreCase("I")){
System.out.println("Jan. " + gainT[1] + " " + gainP[1]);
System.out.println("Feb. " + gainT[2] + " " + gainP[2]);
System.out.println("Mar. " + gainT[3] + " " + gainP[3]);
System.out.println("Apr. " + gainT[4] + " " + gainP[4]);
System.out.println("May " + gainT[5] + " " + gainP[5]);
System.out.println("Jun. " + gainT[6] + " " + gainP[6]);
System.out.println("Jul. " + gainT[7] + " " + gainP[7]);
System.out.println("Aug. " + gainT[8] + " " + gainP[8]);
System.out.println("Sep. " + gainT[9] + " " + gainP[9]);
System.out.println("Oct. " + gainT[10] + " " + gainP[10]);
System.out.println("Nov. " + gainT[11] + " " + gainP[11]);
System.out.println("Dec. " + gainT[12] + " " + gainP[12]);
}
else if(tFC.equalsIgnoreCase("C") && pIC.equalsIgnoreCase("C")){
System.out.println("Jan. " + gainTC[1] + " " + gainPC[1]);
System.out.println("Feb. " + gainTC[2] + " " + gainPC[2]);
System.out.println("Mar. " + gainTC[3] + " " + gainPC[3]);
System.out.println("Apr. " + gainTC[4] + " " + gainPC[4]);
System.out.println("May " + gainTC[5] + " " + gainPC[5]);
System.out.println("Jun. " + gainTC[6] + " " + gainPC[6]);
System.out.println("Jul. " + gainTC[7] + " " + gainPC[7]);
System.out.println("Aug. " + gainTC[8] + " " + gainPC[8]);
System.out.println("Sep. " + gainTC[9] + " " + gainPC[9]);
System.out.println("Oct. " + gainTC[10] + " " + gainPC[10]);
System.out.println("Nov. " + gainTC[11] + " " + gainPC[11]);
System.out.println("Dec. " + gainTC[12] + " " + gainPC[12]);
}
}
}
Here is an incomplete example so you can fill in the blanks yourself:
double gainT[]={54.3, 57.0, 62.5, 67.6, 74.3, 79.2, 80.9, 80.4, 77.8, 70.1, 62.8, 56.3};
double gainTC[] = new double[gainT.length]; //create array which matches the size of gainT
//array.length is a property value returning the 'size' or length or the array
//Now just iterate through all the gainT[] values you populated above and read each one
for(int i = 0; i < gainT.length; i++){
double math = //use your conversion math here, and store the value
gainTC[i] = math; //assign the results of your math to the same spot in the new array
}
If you'd like more information on Loops and Arrays, check here:
For Loops: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
Arrays: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

My java Program is creating 27 invalid errors

When I run these lines of my java code it gives me 27 errors that arn't actual errors
for(int counter = 0;counter<teamName.size();counter++)
{
System.out.print((counter + 1) + " " + teamName.get(counter) + " " + (aLeaderboard.get(counter)).getGamesPlayed() + " " + (aLeaderboard.get(counter)).getHomeWins() + " " + (aLeaderboard.get(counter)).getHomeDraws() + " ");
System.out.println();
}
aLeaderboard is an ArrayList with a custom class Leaderboard.It is 20 long, teamName is also an ArrayList which is 20 longs.
ArrayList<Leaderboard> aLeaderboard = new ArrayList<Leaderboard>();
it is producing these errors:
D:\>javac Project4.java
Project4.java:216: error: illegal start of type
for(int counter = 0;counter<teamName.size();counter++)
^
Project4.java:216: error: ')' expected
for(int counter = 0;counter<teamName.size();counter++)
^
Project4.java:216: error: illegal start of type
for(int counter = 0;counter<teamName.size();counter++)
^
Project4.java:216: error: <identifier> expected
for(int counter = 0;counter<teamName.size();counter++)
^
Project4.java:216: error: ';' expected
for(int counter = 0;counter<teamName.size();counter++)
^
Project4.java:216: error: > expected
for(int counter = 0;counter<teamName.size();counter++)
^
Project4.java:216: error: <identifier> expected
for(int counter = 0;counter<teamName.size();counter++)
^
Project4.java:216: error: illegal start of type
for(int counter = 0;counter<teamName.size();counter++)
^
Project4.java:216: error: <identifier> expected
for(int counter = 0;counter<teamName.size();counter++)
^
Project4.java:217: error: ';' expected
{
^
Project4.java:218: error: illegal start of type
System.out.print((counter + 1) + " " + teamName.get(counter) + " " + (aLeaderboard.get(counter)).getGamesPlayed() + " " + (aLeaderboard.get(counter)).getHomeWins() + " " + (aLeaderboard.get(counter)).getHomeDraws() + " ");
^
Project4.java:218: error: ';' expected
System.out.print((counter + 1) + " " + teamName.get(counter) + " " + (aLeaderboard.get(counter)).getGamesPlayed() + " " + (aLeaderboard.get(counter)).getHomeWins() + " " + (aLeaderboard.get(counter)).getHomeDraws() + " ");
^
Project4.java:218: error: invalid method declaration; return type required
System.out.print((counter + 1) + " " + teamName.get(counter) + " " + (aLeaderboard.get(counter)).getGamesPlayed() + " " + (aLeaderboard.get(counter)).getHomeWins() + " " + (aLeaderboard.get(counter)).getHomeDraws() + " ");
^
Project4.java:218: error: illegal start of type
System.out.print((counter + 1) + " " + teamName.get(counter) + " " + (aLeaderboard.get(counter)).getGamesPlayed() + " " + (aLeaderboard.get(counter)).getHomeWins() + " " + (aLeaderboard.get(counter)).getHomeDraws() + " ");
^
Project4.java:218: error: ')' expected
System.out.print((counter + 1) + " " + teamName.get(counter) + " " + (aLeaderboard.get(counter)).getGamesPlayed() + " " + (aLeaderboard.get(counter)).getHomeWins() + " " + (aLeaderboard.get(counter)).getHomeDraws() + " ");
^
Project4.java:218: error: ';' expected
System.out.print((counter + 1) + " " + teamName.get(counter) + " " + (aLeaderboard.get(counter)).getGamesPlayed() + " " + (aLeaderboard.get(counter)).getHomeWins() + " " + (aLeaderboard.get(counter)).getHomeDraws() + " ");
^
Project4.java:218: error: illegal start of type
System.out.print((counter + 1) + " " + teamName.get(counter) + " " + (aLeaderboard.get(counter)).getGamesPlayed() + " " + (aLeaderboard.get(counter)).getHomeWins() + " " + (aLeaderboard.get(counter)).getHomeDraws() + " ");
^
Project4.java:218: error: <identifier> expected
System.out.print((counter + 1) + " " + teamName.get(counter) + " " + (aLeaderboard.get(counter)).getGamesPlayed() + " " + (aLeaderboard.get(counter)).getHomeWins() + " " + (aLeaderboard.get(counter)).getHomeDraws() + " ");
^
Project4.java:218: error: ';' expected
System.out.print((counter + 1) + " " + teamName.get(counter) + " " + (aLeaderboard.get(counter)).getGamesPlayed() + " " + (aLeaderboard.get(counter)).getHomeWins() + " " + (aLeaderboard.get(counter)).getHomeDraws() + " ");
^
Project4.java:218: error: illegal start of type
System.out.print((counter + 1) + " " + teamName.get(counter) + " " + (aLeaderboard.get(counter)).getGamesPlayed() + " " + (aLeaderboard.get(counter)).getHomeWins() + " " + (aLeaderboard.get(counter)).getHomeDraws() + " ");
^
Project4.java:218: error: <identifier> expected
System.out.print((counter + 1) + " " + teamName.get(counter) + " " + (aLeaderboard.get(counter)).getGamesPlayed() + " " + (aLeaderboard.get(counter)).getHomeWins() + " " + (aLeaderboard.get(counter)).getHomeDraws() + " ");
^
Project4.java:218: error: ';' expected
System.out.print((counter + 1) + " " + teamName.get(counter) + " " + (aLeaderboard.get(counter)).getGamesPlayed() + " " + (aLeaderboard.get(counter)).getHomeWins() + " " + (aLeaderboard.get(counter)).getHomeDraws() + " ");
^
Project4.java:218: error: illegal start of type
System.out.print((counter + 1) + " " + teamName.get(counter) + " " + (aLeaderboard.get(counter)).getGamesPlayed() + " " + (aLeaderboard.get(counter)).getHomeWins() + " " + (aLeaderboard.get(counter)).getHomeDraws() + " ");
^
Project4.java:218: error: <identifier> expected
System.out.print((counter + 1) + " " + teamName.get(counter) + " " + (aLeaderboard.get(counter)).getGamesPlayed() + " " + (aLeaderboard.get(counter)).getHomeWins() + " " + (aLeaderboard.get(counter)).getHomeDraws() + " ");
^
Project4.java:218: error: ';' expected
System.out.print((counter + 1) + " " + teamName.get(counter) + " " + (aLeaderboard.get(counter)).getGamesPlayed() + " " + (aLeaderboard.get(counter)).getHomeWins() + " " + (aLeaderboard.get(counter)).getHomeDraws() + " ");
^
Project4.java:219: error: <identifier> expected
System.out.println();
^
Project4.java:221: error: class, interface, or enum expected
}
^
27 errors
D:\>
teamName contains 20 Strings that are all single Words.
Is this code outside of a method?
Try replacing the current code with this:
public static test() {
for(int counter = 0;counter<teamName.size();counter++)
{
System.out.print((counter + 1) + " " + teamName.get(counter) + " " + (aLeaderboard.get(counter)).getGamesPlayed() + " " + (aLeaderboard.get(counter)).getHomeWins() + " " + (aLeaderboard.get(counter)).getHomeDraws() + " ");
System.out.println();
}
}

Adding random arrays to random arrays in java to make it equal another array

I am learning java, and from what I can tell, what I am looking to do is a rare situation.
I am trying to use an API (kindof) to randomly generate musical notes. I want it to generate 20 times so i have it in a for loop. I realize that i could have used a list for this I just dont know how I could have implemented it. The question I have is, when I try to compile this code, the first part runs. It lets me make the seed. However after that it gives me
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at co.kbcomp.music.Main.main(Main.java:64)
What could I do to prevent this? I know that what I am doing is wrong. That much I dont need to be told. What I want to know is where am I going wrong.
package co.kbcomp.music;
import java.util.*;
import org.jfugue.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Please enter a number for the seed of the song");
long seed = in.nextLong();
Calendar cal = Calendar.getInstance();
Random rand;
//rand = new Random(cal.getTime());
rand = new Random(seed);
int NoteNumber = 0;
int NoteLength = 0;
int OctiveNumber = 0;
int ChordNumber = 0;
int InversionNumber = 0;
//int Duration = rand.nextInt(100 - 5) + 5;
//This keeps track of the iteration of the for loop.
String[] NN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] NL = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] IN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] CN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] ON = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
//This is what is being played
String[] note = { "A", "B", "C", "D", "E", "F", "G" };
String[] noteLength = {"", "w", "", "h", "", "q","", "i"};
String[] octive = { "","1","", "2", "", "3", "", "4", "", "5", "", "6", "", "7", "", "8", "", "9"};
String[] chord = { "", "maj", "", "min"};
String[] inversion = {"", "^", "", "^^", "", "^^^", "", "^^^^", "", "^^^^^"};
String[] key = {"",""};
String keys= " ";
String randstr = " ";
//this is the loop that defines the music legnth
for (int i = 0; i < 21; i++) {
NoteNumber = rand.nextInt(7);
NoteLength = rand.nextInt(8);
OctiveNumber = rand.nextInt(18);
ChordNumber = rand.nextInt(4);
InversionNumber = rand.nextInt(10);
NN[i] = note[NoteNumber]; // This randomly generates the note to be played.
NL[i] = noteLength[NoteLength]; // This randomly generates the length of the note.
ON[i] = octive[OctiveNumber]; // This defines the octive to be played in.
CN[i] = chord[ChordNumber]; // This is defines the major or the minor
IN[i] = inversion[InversionNumber]; // IN[i] = inversion[InversionNumber];
key[i] = NN[i] + NL[i] + ON[i] + CN[i] + IN[i];
//randstr = c[0] + " " + c[1] + " " + c[2] + " " + c[3] + " " + c[4] + " " + c[5] + " " + c[6] + " " + c[7] + " " + c[8] + " " + c[9] + " " + c[10] + " " + c[11] + " " + c[12] + " " + c[13] + " " + c[14] + " " + c[15] + " " + c[16];
keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] + " " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] + " " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " + key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] + " " + key[19] + " " + key[20]);
}
System.out.println(key);
Player player = new Player();
Pattern pattern = new Pattern(key[0]);
player.play(pattern);
}
}
You declare your key array with a length of two:
String[] key = {"",""};
But then later in your for loop, you try to access elements beyond the length of your array:
keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] +
" " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] +
" " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " +
key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] +
" " + key[19] + " " + key[20]);
Since your array has only a length of two, when you try to access the third element (at array index 2), you get an ArrayIndexOutOfBoundsException.
String[] key = {"",""};
...
for (int i = 0; i < 21; i++) {
...
key[i] = ....
Do you see the problem?
As you array in only 20 in length
then this
for (int i = 0; i < 21; i++) {
is going to cause an overflow
It should be < 20
Plus this code is meaningless as your key is only an array of 2
keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] + " " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] + " " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " + key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] + " " + key[19] + " " + key[20]);

Categories