Does anyone know why this method will not let me call it. I'm trying to make sure I can call a method with no parameters before I start writing my code. Is there some package or something I need or can someone explain what's going on. Thanks in advance. I'm getting an java: illegal start of expression and red line under () next to validate.
import java.util.Scanner;
import java.lang.Math;
/// Start Program
public class javamethods {
public static void main(String[] args) {
// Create Scanner object
Scanner input = new Scanner(System.in);
validate();
public static void validate() {
System.out.print("Hi World");
}
}
}
The method validate may not be defined inside the main method.
Instead, do it like this:
public static void main(String[] args) {
// Create Scanner object
Scanner input = new Scanner(System.in);
validate();
}
public static void validate() {
System.out.print("Hi World");
}
You have your curly brace order mixed up; and in doing so, have declared a new method within the main method.
import java.util.Scanner;
import java.lang.Math;
/// Start Program
public class javamethods {
public static void main(String[] args) {
// Create Scanner object
Scanner input = new Scanner(System.in);
validate();
public static void validate() {
System.out.print("Hi World");
}
}
}
You should declare your methods at the class level.
Change your code to this:
import java.util.Scanner;
import java.lang.Math;
/// Start Program
public class javaMethods {
public static void main(String[] args) {
// Create Scanner object
Scanner input = new Scanner(System.in);
validate();
}
public static void validate() {
System.out.print("Hi World");
}
}
Related
So why public static void main(String[] args) I got an error. what can i do to resolve it?
package linkedList;
public class HackerRank {
public class Solution {
// Complete the aVeryBigSum function below.
public long aVeryBigSum(long[] ar) {
long a=0;
for(int i=0;i<ar.length;i++){
a=ar[i]+a;
}
return a;
}
public static void main(String[] args) { ///why this line is not correct
Solution s= new Solution();
long[] ar= {10000,20000,30000};
System.out.println(s.aVeryBigSum(ar));
}
}
}
There is another possible solution, taking the nested Solution class out of the HackerRank class, as I see you are not doing anything with it at the moment.
public class Solution {
// Complete the aVeryBigSum function below.
public long aVeryBigSum(long[] ar) {
long a = 0;
for (int i = 0; i < ar.length; i++) {
a = ar[i] + a;
}
return a;
}
public static void main(String[] args) {
Solution s = new Solution();
long[] ar = { 10000, 20000, 30000 };
System.out.println(s.aVeryBigSum(ar));
}
}
This makes sure that your static main method works.
You can't access a static method in a non-static class. There are 2 possible solutions for this problem:
- 1. Make Solution static
public static class Solution {
public static void main(String[] args) {
//...
}
}
- 2. Remove the static from the main method
public class Solution {
public void main(String[] args) {
//...
}
}
This is super simplified code of what I was doing, but the results are the same. I can comment out in.close() in class 1 and it will fix the error. But then I'm left with an open Scanner for the rest of the project. And changing the variable names are not a fix.
class1:
package scannerDebug;
import java.util.Scanner;
public class Class1 {
private String name_;
public Class1(String name) {
name_ = name; }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Your name: ");
Class1 ex1 = new Class1(in.next());
System.out.println("eex1" + ex1.name_);
in.close();
}
}
class2
package scannerDebug;
import java.util.Scanner;
public class Class2 {
private String name_;
public Class2(String name) {
name_ = name; }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Your name: ");
Class2 ex2 = new Class2(in.next());
System.out.println("ex2" + ex2.name_);
in.close();
}
}
Driver class
package scannerDebug;
public class driver {
public static void main(String[] args) {
Class1.main(args);
Class2.main(args);
}
}
Closing a Scanner also closes the underlying stream. To solve this, simply use a single Scanner in your driver class and use it in both Class1 and Class2:
public class driver {
private static final SCANNER = new Scanner(System.in);
public static void main(String[] args) {
Class1.main(args);
Class2.main(args);
}
public static Scanner getScanner() {
return SCANNER;
}
}
public Class2(String name) {
name_ = name;
}
public static void main(String[] args) {
System.out.print("Your name: ");
Class2 ex2 = new Class2(driver.getScanner().next());
System.out.println("ex2" + ex2.name_);
}
I recommend you follow proper conventions and change your variable/class names.
You could also pass the Scanner to each class via their respective constructors, but I thought you might want to run Class1 or Class2 without running driver first.
I am trying to create a tester method to display a list.
How do I create a CongressStats object to exercise printPartyBreakdownInSenate method?
package congress;
import java.util.List;
public class CongressMain {
public static void main(String[] args){
}
}
public class CongressStats
{
private int congressNum;
/**
* Create a CongressStats object for the given congress number
*/
public CongressStats(int congressNum)
{
this.congressNum = congressNum;
}
/**
* Calculate and print the number of Democrats, Republicans, and Independents in this Senate
*/
public void printPartyBreakdownInSenate()
{
ok in your main function you would make an object of your class CongressStats.
public class CongressMain {
public static void main(String[] args){
CongressStats cs = new CongressStats(somenumhere);
cs.printPartyBreakdown(); //call your function
}
}
hopefully that helped.
This is my code.
I get an error saying:
variable ana of type Analyse
cannot find symbol: ana.lesUlovligeOrd
cannot find symbol: ana.sensurerTekst
Why?
import easyIO.*;
import java.util.*;
public class Eksamen {
public static void main (String[] args) {
Analyse ana = new Analyse();
ana.lesUlovligeOrd("sensurord.txt");
ana.sensurerTekst("roman.txt", "sensurert-roman.txt");
}
}
class Analyse {
In tast = new In();
Out skjerm = new Out();
void lesUloveligeOrd(String s){
}
void sensurerTekst(String s, String t){
}
}
you called ana.lesUlovligeOrd("sensurord.txt");
but actually void lesUloveligeOrd(String s) in Analyse class.
so you call like this ana.lesUloveligeOrd("sensurord.txt");
my code, being practically identical to the code given in BlackBerry's tutorial, has a syntax error in Eclipse. i'm sure there is some small but i'm just not seeing, but my coworker could not find it as well. any ideas would be greatly appreciated. thanks!
Code:
pushScreen(new ABCScreen());
Error:
Cannot make a static reference to the
non-static method pushScreen(Screen)
from the type UiApplication
here is the complete source:
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class AwesomeBBCalculator extends UiApplication {
public AwesomeBBCalculator() {
AwesomeBBCalculator app = new AwesomeBBCalculator();
app.enterEventDispatcher();
}
public static void main(String[] args) {
pushScreen(new ABCScreen()); // ERROR LINE
}
}
final class ABCScreen extends MainScreen {
public ABCScreen() {
super();
// add title
LabelField title = new LabelField("Awesome BlackBerry Calculator",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
}
public boolean onClose() {
Dialog.alert("Thanks for using the Awesome BlackBerry Calculator!\nGoodbye.");
System.exit(0);
return true;
}
}
The pushScreen method can only be called within an instance of UiApplication. You are trying to call it from a static main method. That does not work. Do this instead...
public void foo()
{
pushScreen(this);
}
public static void main(String[] args)
{
(new ABCScreen()).foo();
}
public void class1()
{
pushScreen(this);
}
public static void main(String[] args)
{
(new NewScreen()).class1();
}
try making an object for the ABCScreen class and then use it or u may try this also:
UiApplication.getUiApplication().pushScreen(new ABCScreen());