This question already has answers here:
How can I read input from the console using the Scanner class in Java?
(17 answers)
Closed 7 years ago.
I have been trying to find a way to make my code which does exponents accept an input for E and X. I know that if I use the scanner util, it would make this possible. But so far, I haven't found a way to do that. If you could help that would be sweet, thanks!
import java.util.Scanner;
public class SimpleExponents {
public static void main(String[] args) {
int e = 6;
int x = 4;
double result;
result = Math.pow(e,x);
System.out.println( result );
}
}
Scanner scanner = new Scanner(System.in);
int e = Integer.parseInt(scanner.next());
int x = Integer.parseInt(scanner.next());
or
Scanner scanner = new Scanner(System.in);
int e = scanner.nextInt();
int x = scanner.nextInt();
Related
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 6 months ago.
I'm doing the Java MOOC fi course. I'm supposed to make a list of show names and durations, and print the ones that are shorten than certain duration.
I want the while loop to end after I enter a blank name but it ends after the first object I add to the list.
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// implement here your program that uses the TelevisionProgram class
ArrayList<TelevisionProgram> programs = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Name: ");
String programName = scanner.nextLine();
if (programName.isEmpty()) {
break;
}
System.out.print("Duration: ");
int durationOfProgram = scanner.nextInt();
programs.add(new TelevisionProgram(programName, durationOfProgram));
}
System.out.println();
System.out.print("Program's maximum duration? ");
int maxDuration = scanner.nextInt();
for (TelevisionProgram show : programs) {
if (show.getDuration() <= maxDuration) {
System.out.println(show);
}
}
}
}
replaced
int durationOfProgram = scanner.nextInt();
with
int durationOfProgram = Integer.valueOf(scanner.nextLine());
and now it works but i still don't get why
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 1 year ago.
I am trying to run the following code in order to input an array of any size I desire:
import java.util.Scanner;
import java.util.Arrays;
public class testing2 {
public static void main(String[] args) {
double[] inputs = new double[5];
int currentSize = 0;
System.out.println("Please enter values, Q to quit:");
Scanner in = new Scanner(System.in);
while (in.hasNextDouble())
{
if (currentSize >= inputs.length)
{
inputs = Arrays.copyOf(inputs, 2 * inputs.length);
}
inputs[currentSize] = in.nextDouble();
currentSize++;
}
inputs = Arrays.copyOf(inputs, currentSize);
System.out.print(inputs);
}
}
But when I input any array, my output is just a combination of symbols such as: [D#13fee20c, and this is not the output I am hoping for. Could anyone help me?
You're not printing correctly:
for (int i = 0; i < input.length; i++)
System.out.print(input[i] + " ");
}
This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 6 years ago.
I am a beginner in java and trying to create a program that recieves input numbers in terminal, and will continuously ask for a new numbers until 0 is entered. After 0 has been entered I want the program to summarize all of the numbers and plus them together. But when I try to compile the program I get this error:
Heres the code:
import java.util.Scanner;
public class SumTall {
public static void main(String[] args) {
Scanner tallscanner = new Scanner(System.in);
int tall = 0;
int tall1;
System.out.println("Write a number:");
tall1 = Integer.parseInt(tallscanner.nextLine());
while(tall1 > 0) {
System.out.println("Write another number:");
tall1 = Integer.parseInt(tallscanner.nextLine());
int tall2 = tall + tall1;
}
if(tall1 == 0) {
System.out.println(tall2);
}
}
}
You declared tall2 in while block declare it outside while. it will stick to that block only in your case it belong to while block but you are trying to access that variavle tall2 out side while that's the reason you can see that error. hope it will help you.
I changed the declaration part out side.
import java.util.Scanner;
public class SumTall {
public static void main(String[] args) {
Scanner tallscanner = new Scanner(System.in);
int tall = 0;
int tall1,tall2;
System.out.println("Write a number:");
tall1 = Integer.parseInt(tallscanner.nextLine());
while(tall1 > 0) {
System.out.println("Write another number:");
tall1 = Integer.parseInt(tallscanner.nextLine());
tall2 = tall + tall1;
}
if(tall1 == 0) {
System.out.println(tall2);
}
}
}
This question already has answers here:
How do I concatenate two strings in Java?
(23 answers)
Closed 6 years ago.
package exercises;
import java.util.Scanner;
public class SentenceBuilder {
public static void main(String[] args) {
final int MAX_WORDS = 5;
Scanner scan = new Scanner(System.in);
String word ="";
for (int i = 0; i < MAX_WORDS; i++){
System.out.println("Please enter word "+(i+1)+" of "+MAX_WORDS);
word = scan.nextLine();
}
System.out.println(word);// im stuck on how to concatenate the result
}
}
Inside the for:
word = word + scan.nextLine();
This question already has answers here:
Can I multiply strings in Java to repeat sequences? [duplicate]
(19 answers)
Closed 7 years ago.
import java.util.Scanner;
public class myLine {
public static void main(String[] args) {
String CurrentLine = new String();
int length =0;
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
if (CurrentLine.charAt(0) == 'l') {
return '*' x times;
}
}
}
The last line is just me taking note of what I want to do. If the user puts in a number for x, how do I return the * character that many times?
You can't return in main (it's a void method). You aren't using length, and if I understand your question you could use a StringBuilder and a simple for loop. Then print the contents of the StringBuilder. Something like
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < x; i++) {
sb.append('*');
}
System.out.println(sb.toString());
}