No main method detected - java
The compiler keeps saying there is no main method but there is one:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class StarWarsFinal
{
final static Map<String, String> firstNameMap = new HashMap<>();
static {
firstNameMap.put("A", "Cho");
firstNameMap.put("B", "R2");
firstNameMap.put("C", "C-3po");
firstNameMap.put("D", "Yod");
firstNameMap.put("E", "Nas");
firstNameMap.put("F", "Slea");
firstNameMap.put("G", "Jan");
firstNameMap.put("H", "Zhur");
firstNameMap.put("I", "Boba");
firstNameMap.put("J", "Thre");
firstNameMap.put("K", "Bib");
firstNameMap.put("L", "Kit");
firstNameMap.put("M", "Kyp");
firstNameMap.put("N", "Gonk");
firstNameMap.put("O", "Zlung");
firstNameMap.put("P", "Adi");
firstNameMap.put("Q", "Nat");
firstNameMap.put("R", "Ru");
firstNameMap.put("S", "Cla");
firstNameMap.put("T", "Kir");
firstNameMap.put("U", "Obi");
firstNameMap.put("V", "Ken");
firstNameMap.put("W", "Ziro");
firstNameMap.put("X", "Zev");
firstNameMap.put("Y", "Tavion");
firstNameMap.put("Z", "Jar");
}
final static Map<String, String> lastNameMap = new HashMap<>();
static {
lastNameMap.put("A", "tzki");
lastNameMap.put("B", "hut");
lastNameMap.put("C", "der");
lastNameMap.put("D", "kzos");
lastNameMap.put("E", "vos");
lastNameMap.put("F", "vader");
lastNameMap.put("G", "thrawn");
lastNameMap.put("H", "mesk");
lastNameMap.put("I", "thuo");
lastNameMap.put("J", "skywalker");
lastNameMap.put("K", "D2");
lastNameMap.put("L", "maul");
lastNameMap.put("M", "sith");
lastNameMap.put("N", "muzzar");
lastNameMap.put("O", "jusik");
lastNameMap.put("P", "horn");
lastNameMap.put("Q", "phisto");
lastNameMap.put("R", "farlander");
lastNameMap.put("S", "dunhaussan");
lastNameMap.put("T", "jar");
lastNameMap.put("U", "binks");
lastNameMap.put("V", "lbis");
lastNameMap.put("W", "gnarzlo");
lastNameMap.put("X", "anakin");
lastNameMap.put("Y", "ackbur");
lastNameMap.put("Z", "axmis");
}
final static Map<String, String> jobMap = new HashMap<>();
static{
jobMap.put("Jan","Clone");
jobMap.put("Feb","Bounty Hunter");
jobMap.put("Mar","Droid");
jobMap.put("Apr","Jedi Knight");
jobMap.put("May","Gungan");
jobMap.put("Jun","Gangster");
jobMap.put("Jul","commander");
jobMap.put("Aug","ewok");
jobMap.put("Sep","Queen");
jobMap.put("Oct","Empirer");
jobMap.put("Nov","Darth");
jobMap.put("Dec","captain");
}
public static void main ( String[] args )
{
String[] planet = null, rank = null, rebbelion = null, letter1 = null, letter2= null , Map , HashMap;
Scanner input = new Scanner( System.in ); //scanner initilized
planet = new String[11]; //Planet options
planet[0] = "Alderaan";
planet[1] = "Bespin";
planet[2] = "Coruscant";
planet[3]= "Forest moon of Endor";
planet[4] = "Hoth";
planet[5] = "Kamino";
planet[6] = "Kashyyk";
planet[7] = "Mustafar";
planet[8] = "Yavin";
planet[9] = "DEATH STAR";//Planet options -END
System.out.println("Welcome to the Star Wars name generator");
System.out.println("What is your first name?"); //Name Generation
String firstName = input.nextLine();
String newFirst = firstNameMap.get(firstName.toUpperCase().substring(0,1)); //Name Generation (i want to take the first letter of there input and get the output from letter1 matching that # ie c = letter1 [2] a= letter1 [0])
System.out.println("What is your last name?"); //Name Generation
String lastName = input.nextLine(); //Name Generation (i want to take the first letter of there input and get the output from letter2 matching that # ie c = letter2 [2] a= letter2 [0])
String newLast = lastNameMap.get (lastName.toUpperCase().substring(0,1));
System.out.println("What is your Birth month(first 3 letters)?"); //Name Generation
String month = input.nextLine();
String job = jobMap.get(firstName.toUpperCase().substring(0,3));
System.out.println("If you had to choose 1)dark or 2)light side? please input the number"); //Selection of Dark or Light side
int side = input.nextInt();
//Selection of Dark or Light side
System.out.println("There are now several places you could live please choose one of the following by number:"); //Planet selections
System.out.println("1) Alderaan 2) Bespin 3) Coruscant 4) Forest moon of Endor 5) Hoth "); //Planet selections
System.out.println("6) Kamino 7) Kashyyk 8) Mustafar 9) Yavin 10)DEATHSTAR"); //Planet selections
String location =input.nextLine();
if (side == 1)
{
System.out.println("You Have chosen to be part of the dark side!");
System.out.println("You "+ newFirst + newLast +" now fight for the dark side As a proud "+ job +"of the"+ location +"Good luck against your enemy's and may the force be with you");
}
else
{
System.out.println("You are now part of the light side!");
System.out.println("You "+ newFirst + newLast +" now fight for the light side As a proud "+ job +"of the"+ location +"Good luck against your enemy's and may the force be with you");
}
System.out.println("Thank you for doing the starwars name generator!");
}
}
I don't know why it won't read it, because it was working earlier. I'mm using NetBeans as IDE.
There is nothing wrong with the program.
I did the following:
[steve#newbox tmp]$ cat > StarWarsFinal.java
<paste-program-from-question>
[steve#newbox tmp]$ javac StarWarsFinal.java
[steve#newbox tmp]$ java StarWarsFinal
Welcome to the Star Wars name generator
What is your first name?
^D
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at StarWarsFinal.main(StarWarsFinal.java:112)
[steve#newbox tmp]$
From this we must conclude that the problem you are experiencing is one of the following:
You are not compiling it properly.
You are not running it properly.
There is something non-standard in the way that NetBeans is deciding that a class is runnable. (This is highly unlikely ... IMO)
Maybe you are doing something else that you haven't mentioned ...
For the record:
It is NOT necessary for the class to be declared as public for it to be runnable, at least when you run it from the command line using the java command.
It is not a Java compilation error for there to be no main method. It is a runtime error. A class only needs a main method if you are going to attempt to run it. The compiler has no way of knowing if you are going to do that, so it cannot complain about it.
The "Hello World!" for the NetBeans IDE page runs you through the steps of compiling and running a simple program using NetBeans. Maybe this will give you some clues as to what you are doing wrong.
Related
Java Minecraft Plugin Issue - Not responding to if statement?
So I'm making a simple code redemption plugin for a Minecraft server. What's weird is when I type /redeem (the valid code), nothing happens, although it's supposed to... The valid code is the a code entered into the plugins configuration by the user. Here's my code... public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { //Assigns the commands chosen in config to strings String commandChosen1 = this.getConfig().getString("Command for code 1"); String commandChosen2 = this.getConfig().getString("Command for code 2"); String commandChosen3 = this.getConfig().getString("Command for code 3"); //Assigns the codes to strings String validCode1 = this.getConfig().getString("Valid Code 1"); String validCode2 = this.getConfig().getString("Valid Code 2"); String validCode3 = this.getConfig().getString("Valid Code 3"); //If the redeem command is sent from a player if(cmd.getName().equalsIgnoreCase("redeem") && sender instanceof Player) { //Casts the sender to a new player. Player player = (Player) sender; //Creates object hasUSed to store whether or not the player has already redeemed a code Object hasUsed = this.getConfig().get(player.getName()); //Gives an error message of the arguments don't equal 1. if(args.length != 1) { player.sendMessage(ChatColor.DARK_RED + "Please enter a valid promo code. Find them on our twitter!"); } if(args.length == 1) { //If the player hasn't used the code yet and the arguments given are equal to a code then give them the reward... if(args[0] == validCode1 && hasUsed == null) { this.getConfig().set(player.getName(), 1); player.sendMessage(ChatColor.GREEN + "Promo code successfully entered!"); if(commandChosen1 == "xp") { Bukkit.dispatchCommand(player, commandChosen1 + getConfig().getString("XP Given") + "L" + " " + player.getName()); } } } return true; } return false; } The problem occurs on "if (args[0] == validCode1 && hasUsed == null)". The code that's supposed to happen if both those things check out, doesn't happen and I have no clue why.
Make sure to use equals() when comparing Strings. Using commandChosen1 == "xp" compares string references not values; use commandChosen1.equals("xp") or if you prefer "xp".equals(commandChosen1). Also, While it is possible to use a this.getConfig().getString()with a key value that contains spaces, it can make configuration files hard to read and cluttered. Whenever I design plugins I'll design my config.yml as such VoteGUI: message: 'hello' and then run a this.getConfig().getString("VoteGUI.message"); For yours I'd suggest something like this Promo-Codes: validCode1: 'insert code here' validCode2: 'insert code here' validCode3: 'insert code here' and then put this in your onCommand method: String validCode1 = this.getConfig().getString("Promo-Codes.validCode1"); String validCode2 = this.getConfig().getString("Promo-Codes.validCode2"); String validCode3 = this.getConfig().getString("Promo-Codes.validCode3"); If this does not resolve the issue, copy and paste the exception being thrown from the console and I may be of further assistance
Creating a poll system using PircBot
I'm new to Java and I'm trying to create a poll system using PircBot. So far my code is this: if (message.startsWith("!poll")) { String polly = message.substring(6); String[] vote = polly.split(" "); String vote1 = vote[0]; String vote2 = vote[1]; } Which splits the strings so that someone can type !poll "option1 option2" for example and it will be split into vote1 = option1 and vote2 = option2. I'm kind of lost from here. Am I even heading in the right direction for creating a voting system? I figure that I'd have a separate statement as follows. if (message.equalsIgnoreCase("!vote " + option1)) But I'm not sure where to go with that either.
Netbeans error when trying to run java project
I am getting an error when trying to run my code in Java. I am trying to run an output to a file where the user uses dialog boxes to input name, pay rate, and hours worked. This is the code I have: package output.to.a.file.broc.east; import javax.swing.JOptionPane; public class OutputToAFileBrocEast { public static void main(String[] args) { OutputFile payFile; payFile = new OutputFile ("payroll.txt"); String name; String rate; String hours; String answer; do { int number1; int number2; name = JOptionPane.showInputDialog("Enter first and last name: "); rate = JOptionPane.showInputDialog("Enter hourly rate: "); hours = JOptionPane.showInputDialog("Enter hours for previous week: "); number1 = Integer.parseInt (rate); number2 = Integer.parseInt (hours); payFile.writeWord(name); payFile.writeWord(rate); payFile.writeWord(hours); payFile.writeEOL(); answer = JOptionPane.showInputDialog("Do you have another employee? [y/n"); } while (answer.equalsIgnoreCase ("yes")); } } But I get this error when trying to run the code: Error: Could not find or load main class output.to.a.file.broc.east.OutputToAFileBrocEast Java Result: 1 I am using Netbeans 7.4. and I have already attempted to delete the netbeans cache.
If you build a jar, open it with 7zip or so, and you should find: /output/to/a/file/broc/east/OutputToAFileBrocEast.class The same when the same holds without jar: in the dist or target folder under classes. Maybe you have an extra directory or such.
Having problems using a Switch statement in Java
I'm making a Character Creator for fun and I seem have run into yet another java problem! I tried googling around for a bit but I didn't seem to find a working solution... I'm trying to use a switch statement for the compiler to output certain information depending on what class (Knight, Archer, Mage) the user chose, but when inputting my code I get error messages My code so far (cleaned up a bit) is : String name; String className; int attPoints; System.out.println("Welcome to 'GameName's' Character Creator 2.0!\n"); Thread.sleep(500); System.out.print("First off, what do you want your characters name to be? \n\nName : "); name = Scan.nextLine(); Thread.sleep(500); System.out.print("\nYou are now to be known as "+ name + "!"); System.out.print("\n\n" + name + ", what class do you want to be? "); System.out.print("\n\nClasses available :\nKnight"); Thread.sleep(1500); System.out.print("\nMage"); Thread.sleep(300); System.out.print("\nDruid"); Thread.sleep(300); System.out.print("\nNinja"); Thread.sleep(300); System.out.print("\nArcher"); Thread.sleep(300); System.out.print("\nAdventurer"); Thread.sleep(300); System.out.print("\nBerserker"); Thread.sleep(300); System.out.print("\n\nClass : "); className = Scan.next(); Class userClass = Class.valueOf(className); Thread.sleep(500); System.out.println("\nCongratulations! Your class is now : "+ className + "!"); Thread.sleep(500); // This is where I get an error. // - Syntax error, insert "enum Identifier" to complete EnumHeaderName // - Syntax error, insert "EnumBody" to complete BlockStatement // - Syntax error on token "void", # expected public void setClass() switch (Class) { case Knight: System.out.println(" Various lore about knights "); break; } } I THINK I may be trying to create a class inside another class - but when I tried putting it outside it got another error... Also, I have an int called attPoints, and after I choose a class I want to add 10 to it, but unsure how.
You're switching on Class which is a Java type. Take a look at this tutorial: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html You probably want to do something like: switch (userClass) { case Class.KNIGHT: System.out.println(" Various lore about knights "); break; }
How do I get this Scanner to stop right before a blank line?
I am trying to get a file scanner to stop scanning when the next line is blank. public static void createAudioTypes(File list, Scanner mediaReader, String mediaType) { if (mediaType.equalsIgnoreCase("CD") || mediaType.equalsIgnoreCase("CASSETTE")) { String title = mediaReader.next(); String artist = mediaReader.next(); int year = 0; if (mediaReader.hasNextInt()) { year = mediaReader.nextInt(); } String lbl = mediaReader.next(); ArrayList<String> songs = new ArrayList<String>(); System.out.println(title); System.out.println(artist); System.out.println(year); System.out.println(lbl); mediaReader.useDelimiter(","); while(mediaReader.nextLine().equalsIgnoreCase(mediaType)) { songs.add(mediaReader.next()); } System.out.println(songs); } } Part of the text file I'm reading from: CD Immersion Pendulum 2011 Atlantic Genesis, Salt in the Wounds, Watercolour, Set Me on Fire, Crush, Under the Waves, Immunize (feat. Liam Howlett), The Island - pt. 1 - Dawn, The Island - pt. 2 - Dusk, Comprachicos, The Vulture, Witchcraft, Self vs Self (feat. In Flames), The Fountain (feat Steven Wilson), Encoder 16.99 CD Demon Days Gorillaz Notice the line with all the track titles does not word wrap. This line is to be read into an Array List. Notice just beneath the line of track titles, there's a price and then a blank line. I want this blank line to be the stopping point for the scanner, but I can't get the syntax. Thank you!
read whole line by readLine(), trim() it to see if its isEmpty()