What is the problem with this code? When I execute it control is waiting to read.
public static void main(String[] args) {
int T,N;
String configurationl;
System.out.println("Enter the number of test cases.");
java.util.Scanner sc=new java.util.Scanner(System.in);
T=sc.nextInt();
System.out.println("Enter the configuration");
configurationl=sc.nextLine();
System.out.println("The configuration is: "+ configurationl);
}
You want to put the second input line - to take the configuration1 into a for loop to run T number of times and display it.
public static void main(String[] args) {
int T, N;
System.out.println("Enter number of test cases: ");
Scanner in = new Scanner(System.in);
T = in.nextInt();
String configuration1;
for (int i = 0; i < T; i++) {
System.out.println("Enter the configuration: ");
configuration1 = in.next();
System.out.println("The configuration is: " + configuration1);
}
}
Then the output will be
Enter number of test cases:
3
Enter the configuration:
10
The configuration is: 10
Enter the configuration:
20
The configuration is: 20
Enter the configuration:
30
The configuration is: 30
Related
Java program that will:
User can input an amount from 1.00 to 1,000.00
Any amount beyond the allowed range will display an error.
This block of code below is prints the number entered by an user:
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
// Creates a reader instance which takes
// input from standard input - keyboard
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number: ");
// nextInt() reads the next integer from the keyboard
int number = reader.nextInt();
// println() prints the following line to the output screen
System.out.println("You entered: " + number);
}
}
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
double number;
System.out.print("Enter a number from 1.00 to 1,000.00: ");
number= reader.nextDouble();
//As long as the input is not within the range requests a new input
while (!(number>=1.00 && number<=1000.00)){
System.out.print("Input arrival!\nEnter a number from 1.00 to 1,000.00: ");
number= reader.nextDouble();
}
System.out.println("You entered: " + number);
}
note: The number range you specified corresponds to a double type
Here is my code:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter area code: ");
int areaCode = scan.nextInt();
System.out.println("Enter number: ");
int localNumber = scan.nextInt();
String phoneNumber = "(" + areaCode + ")-" + localNumber;
System.out.println("Calling number "+phoneNumber);
}
}
Execution is ok, program asks to enter two int variables, but when I try to submit my task I got this kind message in console:
Number of passing tests: 0
Number of failing tests: 2
--- Details ---
JUnit version 4.11
.E.E
Time: 0.053
There were 2 failures:
1) areaCode222Number3334444_ShouldPrintFormattedNumber(MainTest)
org.junit.ComparisonFailure: expected:<[]Calling number (222)...> but was:<[Enter area code:
Enter number:
]Calling number (222)...>
How to handle it? Should I have to clean up console??
Thanks in advance
It seems that the automated test does not expect "Enter area code: " and "Enter number: " messages printed. Just remove two System.out.println sentences which print these lines from your code.
Try this after every occurence of the nextInt sentence :
field.nextLine();
This method advances the Scanner instance (scan) past the current line and returns the input that was skipped.
Your code should look like this:
-- collapsed package and imports statements --
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter area code: ");
int areaCode = scan.nextInt();
scan.nextLine();
System.out.println("Enter number: ");
int localNumber = scan.nextInt();
scan.nextLine();
String phoneNumber = "(" + areaCode + ")-" + localNumber;
System.out.println("Calling number "+phoneNumber);
}
}
EDIT: Syntaxe.
I've been instructed to create a code that takes a user's first 5 inputs (doubles) and finds the average. My only problem is creating an exception if a user inputs anything other than a number. Could someone show how I can add this exception to my code?
import java.util.*;
public class Test1
{
static Scanner userInput = new Scanner(System.in);
public static void main(String[] args)
{
Scanner numbers = new Scanner(System.in);
System.out.println("Please enter a number: ");
double first = numbers.nextInt();
System.out.println("Please enter a number: ");
double second = numbers.nextInt();
System.out.println("Please enter a number: ");
double third = numbers.nextInt();
System.out.println("Please enter a number: ");
double fourth = numbers.nextInt();
System.out.println("Please enter a number: ");
double fifth = numbers.nextInt();
System.out.println("The average is\t" + ((first + second + third + fourth + fifth)/5)+"\t");
}
}
This will handle the user typing non Integers.
It also removes the static Scanner userInput which isn't being used.
public class HelloWorld
{
public static void main(String[] args)
{
Scanner numbers = new Scanner(System.in);
int total =0;
int numberOfQuestion = 5;
for (int i = 0; i < numberOfQuestion ; i ++) {
System.out.println("Please enter a number: ");
while (!numbers.hasNextInt()) {
System.out.println("Input was not a number, please enter a number: ");
numbers.next();
}
total = total + numbers.nextInt();
}
System.out.println("The average is\t" + (total/numberOfQuestion)+"\t");
}
}
As the title said I need to enter a string "Enter a number: " repeat itself after I've entered multiple values until I enter "DONE."
So for example it should look like this:
Enter a number:
4
Enter a number:
53
Enter a number:
DONE //closes program
This is a small part to a larger program and granted I know its simple, but I cant figure this out :[
What I'm guessing and been trying is a public static class with a toString method. But I can only get one "Enter a number: " printed once.
Enter a number:
4
53
DONE //closes program
Thanks in advance.
Some code I have for this part would be:
import java.util.Scanner;
public class EnterANumba
{
public static void main(String[] args)
{
while() //Stuck here
{
System.out.println("Enter a number:");
}
Scanner scanner = new Scanner(System.in);
String word=null;
while (scanner.hasNextLine())
{
word = scanner.nextLine();
if (word != null)
{
word = word.trim();
if (word.equalsIgnoreCase("done"))
{
break;
}
}
else
{
break;
}
}
I guess this following code snippet may help you.
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Enter a number:");
while(!(s.next().equalsIgnoreCase("DONE"))){
System.out.println("Enter a number:");
}
}
}
try asking for the 1st time and then make a loop where you validate the input and ask until the condition is met...
Example:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a number:");
while (!("done".equalsIgnoreCase(scanner.next()))) {
System.out.println("Enter a number:");
}
System.out.println("Enter a number:we are done....");
}
I'm trying to write a program that gets a series of five double String values from a user and then attempt to convert those Strings to a double using the Double.parseDouble() method. The trouble I'm having is that I have to enter two values before the next "Please enter a number" pops up. Here is the output I've been getting:
Please enter a number 1: 4
3
Please enter a number 2: 5
4
Please enter a number 3: 3
4
Please enter a number 4: 5
6
Please enter a number 5: 4
2
The average is: 3.8
Here is my code:
import java.io.*;
import java.util.Scanner;
public class theman {
public static void main(String[] args) throws IOException{
// Variables
int counter;
int userEntries = 5;
double sum = 0;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
// Creating a new Scanner
Scanner scanner = new Scanner(System.in);
// Loop for the five entries
for (counter = 1; counter <= userEntries; counter++) {
String input = "";
System.out.print("Please enter a number " + counter + ": ");
input = br.readLine();
double number = Double.parseDouble(input);
sum += scanner.nextDouble();
}
// Print statements
System.out.println("The average is: " + sum / userEntries);
} // End of method header
} // End of class header
What is the best way to correct this problem? I'm assuming it's because of the sum += scanner.nextDouble(); but I need it to increment so I really can't delete it.
You already have the double, so there's no need to use Scanner.nextDouble(). The nextDouble() method scans the next token of the input as a double. What you should be doing is sum += number.
final int userEntries = 2;
Scanner in = new Scanner(System.in);
double sum = 0.0;
for (int counter = 1; counter <= userEntries; ++counter) {
System.out.printf("Please enter a number %d: ", counter);
sum += in.nextDouble();
}
System.out.printf("The average is: %.2f%n", sum / userEntries);
Here's an updated code -
import java.io.*;
import java.util.Scanner;
public class theman {
public static void main(String[] args) throws IOException{
// Variables
int counter;
int userEntries = 5;
double sum = 0;
// Creating a new Scanner
Scanner scanner = new Scanner(System.in);
// Loop for the five entries
for (counter = 1; counter <= userEntries; counter++) {
System.out.print("Please enter a number " + counter + ": ");
sum += scanner.nextDouble();
}
// Print statements
System.out.println("The average is: " + sum / userEntries);
} // End of method header
} // End of class header