java - Array, Inheritance, Method? -
why have error?
import java.util.*; public class labwork1 { public static scanner input = new scanner(system.in); public static int lengthoflist = 10; public static int [] gradelist = new int[lengthoflist]; public static int search; class getgrades{//error here for(int = 0 ; < lengthoflist ; i++){ system.out.print("enter grade: "); gradelist[i] = input.nextint(); } } class outputlist{//error here for(int = 0 ; < lengthoflist ; i++){ system.out.print(gradelist[i] + " "); } } class searchforgrade{//error here for(int = 0 ; < lengthoflist ; i++){ if(gradelist[i] == search){ system.out.println(gradelist[i] + " located in index " + i); } else if(gradelist[i] != search){ system.out.println("grade not in index " + i); } } } class replacegrade{//error here for(int = 0 ; < lengthoflist ; i++){ if(gradelist[i] == search){ system.out.println("enter grade: "); gradelist[i] = input.nextint(); } else if(gradelist[i] != search){ system.out.println("grade not in index " + i); } } } public static void main(string[] args){ system.out.println("enter length of list: "); lengthoflist = input.nextint(); new getgrades(); new outputlist(); system.out.println("enter grade want search: "); search = input.nextint(); new searchforgrade(); new outputlist(); system.out.println("enter grade want replace: "); search = input.nextint(); new replacegrade(); new outputlist(); } }//error here this program asks user input length of array(numbers of grades) asks grades, search grade inputed(ex. user enters 10 program looks 10 in array list), ask user grade replace, replace grade new grade inputed user. list of array(grades) should shown every other method.
after doing changes still got error
import java.util.*; public class labwork1 { public static scanner input = new scanner(system.in); public static int lengthoflist = 10; public static int [] gradelist = new int[lengthoflist]; public static int search; public void getgrades{ for(int = 0 ; < lengthoflist ; i++){ system.out.print("enter grade: "); gradelist[i] = input.nextint(); } } public void outputlist{ for(int = 0 ; < lengthoflist ; i++){ system.out.print(gradelist[i] + " "); } } public void searchforgrade{ for(int = 0 ; < lengthoflist ; i++){ if(gradelist[i] == search){ system.out.println(gradelist[i] + " located in index " + i); } else if(gradelist[i] != search){ system.out.println("grade not in index " + i); } } } public void replacegrade{ for(int = 0 ; < lengthoflist ; i++){ if(gradelist[i] == search){ system.out.println("enter grade: "); gradelist[i] = input.nextint(); } else if(gradelist[i] != search){ system.out.println("grade not in index " + i); } } } public static void main(string[] args){ system.out.println("enter length of list: "); lengthoflist = input.nextint(); labwork1 labwork = new labwork1(); labwork.getgrades(); labwork.outputlist(); system.out.println("enter grade want search: "); search = input.nextint(); labwork.searchforgrade(); labwork.outputlist(); system.out.println("enter grade want replace: "); search = input.nextint(); labwork.replacegrade(); labwork.outputlist(); } }
thank guys added () every method , working!
import java.util.*; public class labwork1 { public static scanner input = new scanner(system.in); public static int lengthoflist = 10; public static int [] gradelist = new int[lengthoflist]; public static int search; public void getgrades(){ for(int = 0 ; < lengthoflist ; i++){ system.out.print("enter grade: "); gradelist[i] = input.nextint(); } } public void outputlist(){ for(int = 0 ; < lengthoflist ; i++){ system.out.print(gradelist[i] + " "); } } public void searchforgrade(){ for(int = 0 ; < lengthoflist ; i++){ if(gradelist[i] == search){ system.out.println(gradelist[i] + " located in index " + i); } else if(gradelist[i] != search){ system.out.println("grade not in index " + i); } } } public void replacegrade(){ for(int = 0 ; < lengthoflist ; i++){ if(gradelist[i] == search){ system.out.println("enter grade: "); gradelist[i] = input.nextint(); } else if(gradelist[i] != search){ system.out.println("grade not in index " + i); } } } public static void main(string[] args){ system.out.println("enter length of list: "); lengthoflist = input.nextint(); labwork1 labwork = new labwork1(); labwork.getgrades(); labwork.outputlist(); system.out.println("enter grade want search: "); search = input.nextint(); labwork.searchforgrade(); labwork.outputlist(); system.out.println("enter grade want replace: "); search = input.nextint(); labwork.replacegrade(); labwork.outputlist(); } }
you're declaring new class instead of method
try change them one:
public void getgrades() { //your code. } instead of
class getgrades{//error here //your code. } also need remove new keyword calling methods, can call them thes:
labwork1 labwork = new labwork1(); labwork.getgrades();
Comments
Post a Comment