User Input 20 Char And Program Will Print Most Common - Exception In Thread Main
Dec 17, 2014
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 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{
I have been struggling with this program for weeks. This program is supposed to take a user's inputted odd number and then print out all prime numbers lower than that number.
public class PrimeNumber { Scanner scan = new Scanner(System.in); int userNum; String neg;
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]
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. * *
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 to do a small program about parking. In the 2-d array (parking lot), contains different kinds of cars, the user have to :
1)enter which car he wanna move, 2)what direction (w,a,s,d) and after that, 3) how many moves( not out of bound) he wants to make, and finally 4) we print out the new parking lot for his next move
And i am stuck at how to move the car to corresponding position and then prompt user for the next move?
For example:
I want to move A to the right by 1 (d)
擷取.PNG print result: 擷取1.PNG
How do I do that? The code that i have right now
public class CarParkGame { public static void main( String [] args) { String carPark[][] = new String [6][6] ; carPark[0] = new String[] {"A","A","A","0","0","0"}; carPark[1] = new String[] {"b","0","0","0","0","c"}; carPark[2] = new String[] {"b","X","X","0","0","c","<exit>"};
I want to write a program that accepts user input and then prints its average in a serial way.
Suppose i enter 5. I should get result 5 first time then i input 10 then it should return the average of (5 and 10) and then i enter say 20 it should return average of (5,10 and 20) and so on.
Write a java program that will ask a user to input grades until the user inputs the sentinel value -1. The program should test each input to determine whether 0<=grade<=100. If the grade entered is within this range, the program should print "Grade accepted;" if not, the program should print "Invalid input".
So I'm learning java from a website and I was tasked with creating a simple program which allows the user to enter a series of integers, then finally when they decide to input a non-integer the program will print the maximum and minimum values of the integers they entered. So for example if they entered 5, 4, 3 and 2 then enter a non-integer the program would output 5 (maximum value), then 2 on a new line (minimum value).
Here is my code:
import java.util.Scanner; public class MaxMinPrinter { public static void main(String[] args) { Scanner in = new Scanner(System.in); int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE;
[Code] ....
And this is what the output looks like:
Actual output ------------------------------------------- Enter an integer: 5 - 10 - - Enter an integer: -4 - 8 - -6 - - Enter an integer: 11 - -1 - - Enter an integer: q - -1 - -6
When it's supposed to look like this:
Expected output ------------------------------------------- Enter an integer: 5 Enter an integer: 10 Enter an integer: -4 Enter an integer: 8 Enter an integer: -6 Enter an integer: 11 Enter an integer: -1 Enter an integer: q 11 -6
I had to make a program that allowed the user to enter the number of students and the students names and grades and then print out the name with the grade in descending order. right now I only have it where it prints the names out in descending order. how do I get it the print out with the grade?
Here is my code
import java.util.*; public class Grades { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of students: "); int numofstudents = input.nextInt();
I am trying to read contents from a file and display them to the user. However, when I enter the file into the program I get the following error: "exception in thread 'main' java.util.MismatchException. What am i doing wrong?
import java.util.Scanner; import java.io.*; public class Project1{ public static void main(String[] args) throws FileNotFoundException { double balance; double item1Price;
In my cs class, we have to write a program that throws an exception if the user enters a negative number, the program should prompt the user to enter only positive numbers and then let them retype the number. But everytime I throw an exception, my program gets suspended.
What am I doing wrong, or is there no way to continue a program if an exception is thrown? Does the program automatically get suspended when an exception is thrown?
try{ do { N = kb.nextDouble(); if(N<0) { throw new NegativeArraySizeException(); } else j++; } while(j==0); fill.size(N); } catch(NegativeArraySizeException e) { System.out.println("The number you entered must be positive. Please try again."); }
I've got a nasty nullpointer that I have tried to resolve to no avail as of yet. The program should prompt for a listings.txt file and take its info and write to a report file. Here's the stacktrace:
run:
Input file: listings Exception in thread "main" java.lang.NullPointerException at java.io.Writer.<init>(Writer.java:88) at java.io.PrintWriter.<init>(PrintWriter.java:113) at java.io.PrintWriter.<init>(PrintWriter.java:100) at kettask2b.PropertyListingsReport.main(PropertyListingsReport.java:34) Java Result: 1
Some adjustments that I have attempted are:
BufferedWriter pwfo = null; for (int i = 0; i < args.length; i++) { String string = args[i]; pwfo = null;
I am attempting to make a heart rate calculator program. Here is a little overview of what I am trying to do. It must use a constructor and get and set methods. It must print heart rate ranges and the user's information(age).
// Use scanner to get inputs import java.util.Scanner;
// Declare class public class HeartRates { private int Age; private int DayOfBirth; private int MonthOfBirth; private int YearOfBirth; private double MaxHeartRate; private double MinTargetHeartRate;
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();
So i'm just toying around with making a simple inventory system, and when i'm trying to get the user's input (first the ID number, then the item name, then the item price) it will take the ID number, output asking for the name (but not take any input for it) and then ask for the item's price. I'm really not sure why it's never happened to me before when getting user input.
This is the input part of my code:
public void getInfoAdd() { int id; String name; double price; System.out.print("Enter ID: "); id = sc.nextInt(); System.out.print("Enter item name:"); name = sc.nextLine(); System.out.print("Enter desired price: "); price = sc.nextInt(); addItem(id, name, price); }
And this is what it outputs:
Quote Enter ID: 2 Enter item name:Enter desired price: 40 Successfully added: 2 $40.0
Writing a program to take input from user. This input must be done with numbers only. If the input is a string I would like the program to loop and ask for input again.
double depositCheckingAmount; do { System.out.println("Please insert amount to deposit into checking.");
[Code]....
As you can see my attempt is to use the ! expresssion incase the input does not equal a double. Is there another method I should be using?
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.