import static java.lang.System.out;
import java.util.Scanner;
import javax.swing.JFrame;
public class Main {
public static void main (String args[]) {
Scanner read = new Scanner (System.in);
out.print ("> ");
double num = read.nextDouble();
out.println (num);
}
}
When I compile the code it gives me no errors; however when I run the program and write a floating point number it gives me the following message:
java.util.InputMismatchException: null (in java.util.Scanner)
I used to use an online compiler and the nextDouble() method worked perfectly there.
For some reason, the below code is not working out for me. I thought I had the scanner right and Eclipse is only throwing an error on the last line that states "else(ernie == 0)". What am I missing here?
Basically just trying to take user input, incorporate some if and else if statements to return a line based on if the condition is met.
Java Code:
import java.util.Scanner; //Importing the scanner now for user input public class Ernie { public static void main(String[] args) { double ernie;
I am supposed to write a program on PuTTY using UNIX (I have Windows 8), but I am not comfortable with it yet, so I am using Java through NetBeans (IDE 7.4).
The program has to follow these instructions (ignore the Linux part of the instructions, the rest is in bold):
Write a program on the Linux system using the putty utility. The program should get strings of data from the command line (that is, look for the data in the "args" array of strings). Use a loop to convert each of the strings in the array into a double and add the number to a total. Print the total after all of the strings have been processed.
The program will use try-catch to catch any error that occurs. If an error occurs, the program will print a message saying that the error occurred. The program can end at that point.
You should create the Java program using the nano editor. The input data should be a list of numbers on the line that runs the program.
The problem so far is that I keep getting an error when converting a String value into a new double value. I have yet to code the try-catch method in my program
Java Code:
import java.util.*; public class Program13Strings { public static void main(String[] args) { Scanner scan = new Scanner (System.in); System.out.println("How many lines of data do you wish to enter?"); int size = scan.nextInt();
[Code] ....
*NOTE: right where my code says double newDouble = Double.valueOf(newResponse); is where the error is occurring.
*ERROR: Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1241) at java.lang.Double.valueOf(Double.java:504) at program13strings.Program13Strings.main(Program13St rings.java:21)
I am trying to make a program that calculates the change due in dollars and cents. The user inputs both the amount due and the amount tendered. My program only works with whole numbers?
I am writing a code in which a user inputs data as a string and that data must be verified as a valid number. A valid number is anything from 0-100. Then all valid numbers are converted into double numbers.
I am having trouble in how to write the validation part of the code.
Is it suppose to be an if, else statement? And if so how is it suppose to be validated?
I am trying to calculate factorials using BlueJ. All of my factorials calculate correctly, I am just having an issue with something the instructor asked of us. She asked us to force the loop to stop when the user inputs "Calculate the factorial of 0", and not give any print.
So far I have my for loop with the correct conditions, I am just really confused as to how to make an if statement to stop the code when the input is 0.
i have a problem, i need build a calender in BlueJ. The program need to found if I put this one year have to give me the full schedule but only the first semester.
the system I am using is blue jay and java. my problem is that I have this error message pop up whenever I try to compile any of my 6 classes. Three of the classes are cities, 3 others are shops, and the final one is a inventory called Player. The following is the error code that appears when I try to compile class THshopB or class CityB.
method run in class CityB cannot be applied to given types;required: Player; found: no arguments; reason: actual and formal argument lists differ in length
A similar error occurs for the rest of my City and THshop classes,except it replaces the class CityB with CityA and occurs in class THshopA, replaces CityB with CityC when occurring in class THshopC.The public methods of CityB and THshopB are shown below, as I believe that something is wrong with the public method of one or the other. I have the code [CItyB.run();}] inside of class THshopB, and run is the part that gets the red highlight error described above.THshopB public method
[ public static void shop(Player inventory){ CityB.run();}] CityB public method [ public static void run(Player inventory){ THshopB.shop(inventory);]
All of my THshop classes are written in the same format, and all of my City classes are written in the same format,
I've been doing a course with a heavy focus on applets and AWT using BlueJ. I've branched out and learn to convert my applets to applications. I can successfully use frames to do similar things to applets (in BlueJ's applet viewer) but I can not seem to run more than application at a time (unlike applets where I could run several at the same time).
Below is a simple Hello World program I wrote. When I re-run main() it closes the original and starts again. (I'm aiming to explore the java.net.* libraries and for testing would like to use more than one frame at a time which can communicate to each over via sockets).
import java.awt.*; import java.awt.event.*; public class HelloWorld extends Frame implements WindowListener { public static void main (String[] args) { HelloWorld app = new HelloWorld(); app.setSize(500,500);
If I have a method that creates a new variable (the name of which is a string argument for the method). Can I get that variable to appear in the workspace in the variables pane?
So instead of just using return to output the variable how do I get in that variables pane?
I have the following code, but it is missing a little bit. My instructor gave me a hint. He said that i have to use the setValue() and getValue() methods, but i'm still confused..
This code only return a black image with the same compositions as the image I want to mirror.
public Image mirror() { Image mirrorImage; mirrorImage = new Image(foto.getWidth(), foto.getHeight(), "spejling"); for(int i = 0; i < foto.getWidth(); i++)
I'm trying to generate a multicoloured brick wall as part of an assignment using BlueJ.
According to the instructions we've been given, once the wall is set up and we invoke the draw() method, if we invoke the toggleMultiColoured method the bricks will go from being all read to a mix of 6 different colours...
...When I invoke the toggleMultiColoured method, however, the wall I generate is still red.
Here's the source code for the BrickWall class I wrote:
Java Code:
public class BrickWall { private int bWidth; private int bHeight; private int numRows; private int rowLength; private ArrayList<String> colors; private ArrayList<Rectangle> bricks;
*initiating theGrid[][]* public Zhang() { con.setLayout(new FlowLayout()); for (int xDimension = 0; xDimension<theGrid.length; xDimension++)
[Code] ....
All I'm getting as a blank screen and I've searched for what to put into object oriented code to make the image show up how to program using 2d arrays. I don't get any error messages (which is good I suppose) but how to make the image show up because I have stuff under the graphics and draw methods, did I do something wrong where initializing the array?
I am coding for the first time using the simplified BlueJ platform. I am trying to write a simple conversion program from cm to inches and inches to cm.
Here is my code so far (I also have the textIO file linked to it):
public class UnitConverter { public static void main(String[] args){ int inchesTocentimetres, centimetresToinches, exitApplication; int choice; double result;
I am trying to write a program that will generate a QR Code from an input text and also display some information about the input/output bits. So far I have created the frame and what to do next. And I'm not sure if I am on the right track since my level of programming is not that great. By the way, I am using zxing libraries from GitHub. I know, there are plenty of generators online for the QR Code, but that is not what I am looking for. As you can see on the attached image, I am more interested in the efficiency of encoding 2D data. Also, I noticed that almost all the online projects regarding 2D codes are for Android. Which is not very useful.
So I was going to try to create a program that prompts input and creates a file (That didn't exist before) with that input as name.Then, the program prompts inputs after stating questions such as 1 + 1, then if the user inputs an answer, put "Question # = Correct "or" Wrong.Code SO Far:
Java Code:
import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; public class File_Read { public File_Read() {//File_Read is the Interactive object
[code]....
So that it puts the Correct or Wrong into the file.
I'm having some trouble with a code I am writing for class. I had an 2 errors like this before this one and fixed it by changing int avgRe, avgMiles =0; to double. Now I am getting this error and am stuck on what I need to change. Here is the code:
import java.io.*; import java.util.*; import java.text.*; public class Reimbursement_3_09 { static Toolkit tools = new Toolkit(); public static void main (String [] args) throws Exception {
[Code] ....
This is my error:
[code=Java] Reimbursement_3_09.java:33: error: incompatible types: possible lossy conversion from double to int summary (outFile, totalAmount, ctrMiles, ctrMilesgt0, avgRe, avgMiles); ^ Some messages have been simplified; recompile with -Xdiags:verbose to get full output 1 error
My question is how do I configure this so that when / if I need to change the discount value, it automatically adjusts the % discount off of the Full or Compact?
All I can get to work is simply hardcoding it and assigning the discount value, i'm probably just messing up the syntax.
I am trying to convert the double 4270571936.0000000000d to a hex string using Double.toHexString() and the answer I'm getting is 0x1.fd17834p31, what does p stands for?
The answer I'm expecting to get is 0x41efd17834000000 so not sure why it won't give me the correct answer?
The following floating point Double to hex calculator shows the write answer right Floating Point to Hex Converter
I have a jsf page.i need to confirm one filed only contain double value.how to validate this? I checked and found there is a validateDoubleRange,but its not suitable for this.
//Students Full Name firstName = JOptionPane.showInputDialog("Enter student " + "first name."); lastName = JOptionPane.showInputDialog("Enter student " + "last name."); // Get test grade (numbers) [b]test1 = JOptionPane.showInputDialog("Enter test1 grade")[/b];
The line in bold is where that error comes up. I know it something simple but I can't remember. I declared both firstName and lastName as Strings and then the test1 I declared as double. I had a similar error in a previous assignment where I had a integer(age) input and then i had an output statement asking for a name all I needed to do was put keyboard.nextLine(); after my age input and I was fine.