Cannot get a very basic program to produce any output whatsoever - java

I cannot get the Pearson MyProgrammingLab to produce any output from this code and several other programs like it. But the code works perfectly fine in other IDEs. Is there an error in the Scanner construction or anything else basic that I am overlooking?
I have exhausted so many solutions and still no luck. There is just simply no output when the program is checked, and cites it as a logic error.
Here is the code:
import java.util.Scanner;
class Area {
public static void main (String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter the side: ");
double s = input.nextDouble();
double area = (6 * Math.pow(s, 2)) / (4 * Math.tan(Math.PI/6));
System.out.println("The area of the hexagon is " + area);
}
}
Any ideas?
EDIT: This is what the ouput when checked would display:
Given the following was entered from the keyboard:
5.5⏎
you displayed:
instead of:
Enter◦the◦side:The◦area◦of◦the◦hexagon◦is◦78.59⏎

Related

Is there a way that I can scan comma values in Java? [duplicate]

This question already has answers here:
Scanner double value - InputMismatchException
(2 answers)
Closed 2 years ago.
I'm rather new to Java and I was making a simple calculator. Problem is when I my input number is for example "3.1" it gives an exception error, but when writing "3,1" it works just fine.
My friend, however, has a slightly more advanced calculator (with string parsing) and when I run his code the opposite happens: 3,1 gives exception error, 3.1 works perfectly.
I was looking forward to know what causes these different behaviors.
I made this simple sum just now and the same happens, I'll edit and put his calculator code in a few minutes
import java.util.Scanner;
public class Tutorial_7 {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
double num1, num2;
System.out.println("Introduza os dois números");
System.out.println("1º: ");
num1 = scan.nextDouble();
System.out.println("2º: ");
num2 = scan.nextDouble();
System.out.println((num1 + num2));
scan.close();
}
}
Final edit: He does use Double.parseDouble(). Got it, the difference is indeed in where it is localized. Should have looked for it but never heard of this concept before.
Thank you
Because you are using difference Local for that one can scan it with a dot . and another with a comma , to fix it you should to fix one for your Scanner like this :
Scanner scan = new Scanner(System.in).useLocale(Locale.US);
For example:
If you are using Local.US you should to scan your double with a .
like 6.6
If you are using Locale.FRENCH you should to scan your double with
a , like 6,6

How to read float input without knowing size in java?

Sample Input:
9.1 9.0 8.9 8.8 9.4 7.9 8.6 9.8
Here is my code for getting input.
I dont know how to get this type of input without knowing the number of inputs.
import java.io.*;
import java.util.*;
/**
* Diving_Competition
*/
public class Diving_Competition {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
ArrayList<Float> lst = new ArrayList<Float>();
while (in.hasNextFloat()) {
lst.add(in.nextFloat());
}
System.out.print(lst);
in.close();
}
}
This loop runs infinite time. How to get input in a single line without knowing its size?
I'm from python background
As long as there are numerical inputs, your code will not exit the while loop because the condtion in.hasNextFloat() is satisfied.
Assuming you are entering the sample in the terminal: To exit the loop, just enter any non-numerical value like a or enter the EOF-command. In Unix this is Ctrl+D and in Windows it is Ctrl+Z (not sure about Windows-Command)
Haven't java in a bit but I think this works. I read the whole line and separate them.
import java.io.*;
import java.util.*;
/**
* Diving_Competition
*/
public class read {
public static void main(String[] args) throws IOException{
Scanner in = new Scanner(System.in);
String string = in.nextLine();
String[] stringLst = string.split(" ");
ArrayList<Float> numLst = new ArrayList<Float>();
for (String num : stringLst) {
numLst.add(Float.parseFloat(num));
}
System.out.print(numLst);
in.close();
}
}
The best way to figure out such issues is either by debugger OR use console to print helpful information. Your program is perfectly fine, it hangs because it is expecting an input that needs to be entered (unless you want to scan input as program args then its a different question).
Here I made a simple change in your program
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
ArrayList<Float> lst = new ArrayList<Float>();
while (in.hasNextFloat()) {
float x = in.nextFloat();
System.out.println("Number Entered: " + x);
lst.add(x);
}
System.out.print(lst);
in.close();
}
and here is console output
2.3
Number Entered: 2.3
re
[2.3]
Notice how to end the input, I had to enter non-float letters. If I don't enter that, the program will continue to take input from the console, one-by-one.
That being said, from your question it looks like you want to get a line of float input from console. If that the case, then you should read by line and parse it based on "space" character.

How can I make this statement (within a while-loop) not print out twice to the console?

I am very new to Java, (and to programming altogether). I am sure that the solution to my problem is very simple, but I just can't figure it out. The code below is a small chunk of my program. But it is still compile-able, and still has the same problem.
Here is the code::
import java.util.Scanner;
public class Practice{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
String command = "";
double d; // Distance
double t; // Time
double s; // Speed
System.out.println("Hello, I am the Physics Calculator!!");
while (!command.equals("end")){
System.out.print("What would you like to calculate?: "); // after the first loop, this statement is printed twice to the screen! :(
command = input.nextLine();
System.out.println();
if (command.equals("speed")){
System.out.print("What is the distance in meters?: ");
d = input.nextDouble();
System.out.print("What is the time is seconds?: ");
t = input.nextDouble();
s = d / t;
System.out.println("The Speed is "+ s +" m/s");
System.out.println();
}
} //End of the while-loop
}
}
The first line of code within the while-loop is:
System.out.println("What would you like to calculate?: ");
So far so good. When I run the program, it prints: *What would you like to calculate?: *
And then the program continues as expected.
The problem is after the program reaches the end of the while-loop, and returns to the top of the while loop. it will print:
What would you like to calculate?:
What would you like to calculate?:
I just can't figure out why it prints it out twice.
Here is an example of the exact output I received when running the program (The italic is the output to the console and the bold is my input):
Start{
Hello, I am the Physics Calculator!!
What would you like to calculate?: speed
What is the distance in meters?: 50
What is the time is seconds?: 50
The Speed is 1.0 m/s
What would you like to calculate?:
What would you like to calculate?:
}End
At the end, I can still type 'speed' and continue on with the program. I just want to get rid of the second 'What would you like to calculate'.
Any input regarding the problem that I am having will be highly appreciated!!
Thank you very much for your time!!
This happens because nextDouble doesn't consume the newline that follows the number. You need to do that separately. Currently, that newline (after the number) gets consumed the next time you are prompted for a command. Since empty string is not "speed", the loop goes through one extra time.
Add Input.nextLine(); after t = Input.nextDouble(); and this will work just fine.

What import should I use for keyboard scanners and Math.?

I tried every import I know, and it still basically keeps giving me class, interface, or enum expected error on every line that uses Keyboard or Math.
If you're wondering what the program does, it's suppose to find the distance between 2 points that the user puts in.
// Sam
// 9.25.13
// import csl.Keyboard from the L: drive jdk
import java.io.*;
import java.util.*;
public class swagggg
public static void main ( String [] args)
{
// declare variables
int x1, y1 ,x2, y2;
double distance;
// get user input
Scanner Keyboard = new Scanner (System.in);
System.out.println("Enter the first set of coordinates: ");
x1 = Keyboard.nextInt();
y1 = Keyboard.nextInt();
System.out.println("Enter the second set of coordinates: ");
x2 = Keyboard.nextInt();
y2 = Keyboard.nextInt();
// calculate using the Math class static method
distance = Math.sqrt(Math.pow(x2-x1,2) + (Math.pow(y2-y1,2));
// out results
System.out.println ("The distance between (" +x1+","+y1+") and ("+ x2 +","+y2+") is " + distance);
}
}
/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Now it compiles but when I type in the first coordinate, it looks like this
Enter the first set of coordinates:
(2,9)
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at C2p8.main(C2p8.java:17)
Process completed.
The Keyboard class doesn't exist in the JDK. Probably it's a custom class made by your teacher of some sort. You would need either the .jar of that class or the actual code. Are you supposed to do your work in your computer? If that's the case the teacher probably gave you that file. The only class I think that you might be needing here instead of Keyboard is Scanner but that one has nextInt() not readInt() method. Also that one has to be instantiated, it is not static and it appears your Keyboard one is.
For the Math class, you're not supposed to import it. It is imported automatically always.
Keyboard is not part of the standard library. Perhaps you meant java.util.Scanner, but that doesn't have readInt() method, it has nextInt(). There's a comment at the top of your code that tells you where it is, it looks like a custom class.
Math is java.lang.Math.
You're missing a ; at the end of
double distance; // <missing that ;
And you have a dangling quote here
y1 = Keyboard.readInt();' // < what is that?
Get rid of it.
Had you used a proper IDE like Eclipse, Netbeans, or IntelliJ you wouldn't have had any of these problems.
java.lang.Math.* for the math...
For getting input from the keyboard, you need a scanner.
Scanner scannerVariableNameWhichYouCouldCallKeyboard = new Scanner(System.in);
so if you simply added the line Scanner Keyboard = new Scanner(System.in);, all of your Keyboard lines would work. You may have to import java.util.Scanner;.
And for reference, if you're working in Eclipse, you can have Eclipse automatically handle your imports by either hovering over the thing that needs an import and waiting for the pop up, or I think Ctrl+O.

Need help jumping into Java and Java input from C++

This is my first program in java and I haven't found any good websites like this one for C++ and it's confusing for me because I just started writing java and I just came from C++. Anyways, concerning this code, could someone explain how to fix this code because of the line containing Scanner and/or how to simply receive inputs, because I haven't found any simple way to translate cin >> from C++
public class input {
public static void main(String[] args) {
double total = 0;
Scanner in = new Scanner(System.in);
System.out.println("As you enter numbers, they will be added.");
System.out.println("Entering a non-number will stop the program.");
while (in.hasNextDouble()) {
double n = in.nextDouble();
total = total + n;
System.out.println("The total is " + total);
}
}
}
Your code works. Just make sure you have import java.util.Scanner. On a related note, use Eclipse or Netbeans as they would have told you this. Also, you should capitalize class names and put your class in a package instead of in the "default package". I recommend "Head First Java".
package sand1;
import java.util.Scanner;
public class Input {
public static void main(String[] args) {
double total = 0;
Scanner in = new Scanner(System.in);
System.out.println("As you enter numbers, they will be added.");
System.out.println("Entering a non-number will stop the program.");
while (in.hasNextDouble()) {
double n = in.nextDouble();
total = total + n;
System.out.println("The total is " + total);
}
}
}
Here is output when I ran it. I think I might consider it a bug that I was able to hit enter with a blank line without it ending.
run:
As you enter numbers, they will be added.
Entering a non-number will stop the program.
12.2
The total is 12.2
43
The total is 55.2
a
BUILD SUCCESSFUL (total time: 11 seconds)
I'm at work and don't have the jdk installed, so I can't compile and run this. Giving a quick look though, it seems like the only thing you might have problem breaking out of the scanner, though. After entering a few numbers, try pressing ctrl-d - this should signal end of input.
As Borealid said you need to add the following line at the top of the class to get it to compile:
import java.util.Scanner;
Also note that by convention in java classes are named with an uppercase character Input, not input.
Finally, you can obtain input directly through System.in.read() and the other overloaded permutations of the read() method, against System.in
Check out the Java Tutorials,they're quite good for a beginner

Categories

Resources