Monday, April 1, 2013

Quick question: how to run my Java code, what should I do?

Challenge courtesy of thomas: Quick Question: How do I get my Java code to run?

The below code won’t run. It is the skeleton of a program that will eventually draw flags for prompted countries. When this code is run, it should show a list of coutries to choose from and prompt the user for a selection and size for a flag. This will be followed by a blank grid. How do I get this to run?


package studentCode;

import java.awt.Color;

import java.util.Scanner;


import GridTools.MyGrid;


public class FlagMaker {


/* Draws a single flag as indicated by the second parameter */

/* into the grid passed as the first parameter */

public static void drawFlag(MyGrid grid, int countryCode) {


Scanner scanner = new Scanner(System.in);


/* Get input from user about what flag to draw */

System.out.println(“Type the number corresponding to the name: “);

System.out.println(“1 Indonesia”);

System.out.println(“2 Lithuania”);

System.out.println(“3 Rwanda”);

System.out.println(“4 Malta”);

System.out.println(“5 Afghanistan”);

System.out.println(“6 Eritrea”);

System.out.println(“7 Macedonia”);

System.out.println(“8 The Bahamas”);

System.out.println(“9 Zimbabwe”);

System.out.print(“Your choice here:”);

int choice = scanner.nextInt();

System.out.print(“Choose a size (4 or larger): “);

int size = scanner.nextInt();

if (size 30){

size = 4;

choice = 99;

}


/* Create drawing grid of the height requested */

MyGrid grid = new MyGrid(size);


/* Draw the letter on the grid */

FlagMaker.drawFlag(grid, choice);

}

}


The error message states:

java.lang.NoSuchMethodError: main

Exception in thread “main”


And I have been running projects on this so it is not the compiler. I don’t know how to get drawFlag and main method together.

Also for my assignment, I can’t change up this method (must stay with current code and build from it). And to the first answer, thanks for the help, but this doesn’t solve the error for no main method.


Truly remarkable facts about Quick Question: How do I get my Java code to run? that you might really need to solve dilemmas alone. Hopefully this assists in lots of ways: and also make your life considerably better. Needing remarkable facts about Quick Question: How do I get my Java code to run? might possibly be a resolution next time.

Solution:


Answer by Imspazzy

I suggest using enumerated types.

enum Flag{Indonesia, Lithuania, Rwanda, Malta, Afghanistan, Eritrea, Macedonia, The Bahamas, Zimbabwe}

choice = scanner.next() //imports as a string

Flag flag1, flag2, flag3, flag4, flag5, flag6, flag7, flag8, flag9;

flag1 = Flag.Indonesia;

flag2 = Flag.Rwanda

//…fill the rest in following the pattern

flag9 = Flag.Zimbabwe;

if (choice = 1){

choice = flag1;

}

//can be condensed easily into a couple statements using loops, just dont feelin like trying that, you seem capable enough of making it

//This will organize your code better and should fix your error


Answer by modulo_function

Where’s you main(..) method? It’s not here in this class so it must be in some other class. You need to have the all the classes that work togerher (a package) in the same directory. How are you trying to run your program? Are you compiling the source files with javac and then running it with java?


Give me some more infomation and I’ll help you. Email the code if you want.


Answer by Meklar

Your class is missing a main() method. All you have is a drawFlag() method with nothing to call it. You need to add a main() method, or make another class with a main() method that calls FlagMaker.drawFlag() at some point. A Java program can’t run without a main() method.


Learn far better?

Provide your resolution to this inquiry below!


Indonesia: Acacia in shallow waters


Graphic in Indonesia: Acacia in shallow waters


Indonesia, Nussa Tengara, Flores Island. Labuhan Bajo and surroundings. Indonesia is an interesting, biodiversed, geologically active country composed by over 13 thousands islands. An archipelago of multiple possibilities and landscapes. Sometimes the combination of beautiful beaches, unpolluted sand and vegetation gives us the opportunity to come across beautiful images like the one in the photo. A close-up of acacia branches and flowers on shallow waters


Quick question: how to run my Java code, what should I do?

No comments:

Post a Comment

Blog Archive