import java.io.Console;
public class Introductions{
public static void main(String[] args){
Console console = System.console();
console.printf("Hello!My name is ---");
}
}
Output :-
Compiling Introductions.java.......
-----------OUTPUT-----------
Exception in thread "main" java.lang.NullPointerException
at Introductions.main(Introductions.java:5)
[Finished in 2.2s with exit code 1]
I am new to java and still learning. I am trying to execute the code below which compiles successfully, but getting the following error:"Exception in thread "main" java.lang.NoSuchMethodError: main"
Code:
class Dog{ int size; String name; String breed; void bark(){ System.out.println("woof woof"); } } class DogTestDrive{
public class Emp { private String name; private String jobTitle; public void setName(String nameIn){ name=nameIn;
[Code] ....
<terminated> TestClass Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at TestClass.payOneEmp(TestClass.java:21) at TestClass.main(TestClass.java:13)
I am working on a program that will allow a user to input grades for a class of four students who have taken two tests. Based on the grades entered, the program will calculate the averages of the two tests for each student and display it along with their respected letter grades.
Now I can get the program to compile successfully, but after inputting the grades in, I get the error message saying that it cannot format given object as a number. I am using 4 arrays to execute this program and maybe that's why I'm having the trouble? I'm not sure because I am still fairly new at this stuff and can't sen to resolve it.
I was having problems for a while and then finally got excited when I got it to compile without any errors and now I'm getting an error inside my program. All I need to do is format the numbers of the grades into something like: 000, and each of the averages as 000.0. I understand how to do it because I have done it in another program I've done in the past. I just don't know how to fix this error that is coming up.
here is my code:
import java.util.Scanner; import java.text.DecimalFormat; public class TestAverage { /** * A program that will store and process 2 test scores for a class of 4 students. * The program will prompt for the test scores as shown above in the sample run. * After all the data is entered,the program will display the score for test 1, test 2 . * The average of the 2 tests and the letter grade for the class for each student in a tabular format. * *
My program is user input 20 char and the program will print the most common. So I use another int arr which count the number appears in the original array. i know its not so Effective but I don't know why it run but it stop in the middle. I got this code :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20 at Ex2.common(Ex2.java:39) at Ex2.main(Ex2.java:23)
import java.util.Scanner; class Ex2 { public static void main(String[] arg) { Scanner reader = new Scanner (System.in); char[] arr=new char[20]; System.out.println("Please enter 20 chars:"); for (int i=0;i<20;i++)
I'm currently learning about Swing but I can't get my head round this piece of the code. Here is a simplified gui (not interested in the gui part but the execution)
public class SwingDemo implements ActionListener { SwingDemo(){ JFrame jfrm = new JFrame("Simple gui pro"); //rest of code public static void main(String[] args) { new SwingDemo(); }
I get the above, create a new instance of SwingDemo in the main thread which starts up the gui through the constructor. However, then the tutorial says that I should avoid doing the above but do this instead:
public class SwingDemo implements ActionListener { SwingDemo(){ JFrame jfrm = new JFrame("Simple gui pro"); //rest of code public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { //why do this instead? public void run(){ new SwingDemo(); } }); } }
Reading, it talks about an event-dispatching thread which has completely lost me... Why not just instantiate the object directly instead of creating another thread?
I have two classes, where main class is simple with only main function in it. Another class extends Thread and there's couple of functions I want to execute from main class.
Problem: I try to get other.shut() to be run in main class catch() block, after I have stopped the other class's running thread (e.g. by ctrl+c).
I think I need to somehow "pass" the exception from other class to main class so it goes to the catch block?
Code for main class:
Java Code:
public class MainClass { public static void main(String[] arguments) { OtherClass other = new OtherClass(); try { other.exec(); } catch (Exception e) { other.shut();
I'm working on an assignment for my java class and I am currently stuck with an error/exception,
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at BabyNames.search(BabyNames.java:45) at BabyNames.main(BabyNames.java:20)
I input my name, which is able to get my name from the names.txt, however after that, it hits an exception and does not go further on.Also please use the names,
JIMMY JIMMIE JIMMU JIMI (All can be lowered case), because I am unable to upload the full file for it is very big.
//Jimmy //Programming Assignment #6: Baby Names //This program is going to scan a file with data //about names and their popularity starting from the 1890's //and will display their popularity over the years in 10 intervals.
For this program, I have to run a slot machine. It runs until right before the do while loop and then I receive the error: Exception in thread "main" java.util.InputMismatch.Exception. Searching around, someone said it was a scanner error and adding a call keyboard.nextLine(); fixes the problem- however with that I receive the same error.
Code : import java.util.Scanner; import java.util.Random; import java.io.*; public class SlotMachineSimulation { public static void main(String[] args) throws IOException { int number;
Exception in thread "main" java.lang.NullPointerExceptionat DogTestDrive.main(DogTestDrive.java:19)
Here is the source
class Dog { int size; String name; void bark () { if (size < 60) { System.out.println("Woof woof");
[code]....
Some background: I'm reading "Head first Java 2nd edition" and I'm going through the examples which is showing me how to change the state of an object. The original code looks like the code below, however the previous chapter went over creating array's of an object, so I created an array of the object "Dog" and wanted to re-write it this way. To my understanding, it should work but it's giving me that error when I execute it. The error itself isn't very clear, if I could get a line number pointed to, that would work.
class Dog { int size; String name; void bark() { if (size > 60) { System.out.println(“Wooof! Wooof!”); } else if (size > 14) { System.out.println(“Ruff! Ruff!”); } else { System.out.println(“Yip! Yip!”);
Right, so I got this program. It takes input from the user and assigns it to fields on an object. But, it's meant to check the users input. If the user enters bad input, it's supposed to throw this exception. For each of these exceptions, theres a class specifically for it.
public class PayrollDemo { public static void main(String[] args) { Payroll pr = new Payroll ("Test Name", 234); System.out.println("Current Employee Information. "); System.out.println("Name: " + pr.getName()); System.out.println("ID: " + pr.getID()); System.out.println("Hourly Pay Rate: " + pr.getHourlyPayRate());
[Code] ....
And this is the exception class.
public class InvalidNameException extends Exception { /** No-arg constructor */ public InvalidNameException() { super("Invalid name"); } }
PayrollDemo.java:43: error: cannot find symbol InvalidNameException.InvalidNameException(); ^ symbol: method InvalidNameException() location: class InvalidNameException 1 error
It's just meant to tell the user that they entered an invalid value, which would mean if they entered an empty string instead of a name.
public class Sphere { public double diameter; public double volume; public double area; public double fourThirds = 4/3; public Sphere(double someDiameter){ someDiameter = diameter;
[Code] ....
I am trying to get this code so that I only enter the diameter once in the sphere object1 = new Sphere(4); but I can't get it to work right. I can get the diameter to work with the calculate volume and area methods but that's it.
im having an issue with the 3rd thread that are supposed to merge the two sorted sub arrays , i pass the 2 subarrays to my runnable function sortlist and they are renamed IntSortList 1 and 2 and th1.start() and th1.join() are called and it works fine, but then i have another runnable constructor that takes IntSortList 1 and 2 but it does take a runnable. below is the code in my main,
Runnable InSortlist1 = new sortList(data2p1); Runnable InSortlist1 = new sortList(data2p1); Thread th1 = new Thread (IntSortlist1); Thread th2 = new Thread (IntSortlist2); try { th1.start(); th1.join();
Im writing a simple program to understand classes and objects. Basically what I have is a file called Program.java where I have my main method.I have another file called Person.java which I want to use to create Person objects. That person can have a name, email adress, phone number, etc.I put both these files in the same folder.in Program.java my first statement is:
My problem is that when I compile Program.java i get an error message saying that the package Person.java does not exist.So my question is, when you create a class that you want to use for objects, how do you import that class into your class with the main method so that you can use instances of your other class?
// Add range to Vehicle. class Vehicle { int passengers; // number of passengers int fuelcap; // fuel capacity in gallons int mpg; // fuel consumption in miles per gallon
// Display the range. void range() { System.out.println("Range is " + fuelcap * mpg);
[Code] ....
I'm compiling it in Eclipse and this continues to show in the console display
Minivan can carry 7. Exception in thread "main" java.lang.NoSuchMethodError: Vehicle.range()V at AddMeth.main(AddMeth.java:34)
As web server has multiple threads to serve client requests in Thread Pool & to ensure Thread Safety we should not use any variables or Objects at Instance/Class level.But in case of Session Variable which one is the Best Practice as the Session object is used by all the requests to have the same Session ID.
My Code :
public class MyServlet extends HttpServlet { private static Logger log = Logger.getLogger(ClientRegistrationServlet.class); private HttpSession session; /* This is used at Instance Level*/ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Need to use a ComboBox from another class (Beginning Java forum at JavaRanch)
When my file is saved it has the values off the main gui's comboBox in variable 'env' which I then write to file using. I just need to access the comboBox in my second class so I can use it in main as I'm using the wrong one atm- if that makes sense?
I have a program with 4 classes, all of them in the same package, one of them is the Main class, and in that class I declared a variable named "port" of type int. One of the 3 another ones is the class Connection class, which it requires the port variable. I want to use this variable in the Connection class. How can I do it?Both classes are shown below:
Main.java package server; /* Imports */ /* Another variables */ int port; /* <-- IS THIS ONE */
import java.io.FileNotFoundException; public class PrimaClasse { public static void main(String[] args) throws FileNotFoundException { SecondaClasse oggettoSeconda = new SecondaClasse(); oggettoSeconda.controlloNomi();
[code]....
Now it's working and from the main class i can controll the second class BUT. i want that is the main class that ask the user name, and i want pass that value to the second class. what code i must change?why eclipse wants me insert this import java.io.FileNotFoundException; and this throws FileNotFoundException for not give me an error?