Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
the link of the problem : http://codeforces.com/contest/734/problem/A
and what is the problem of my code
My Code :
import java.util.Scanner;
import java.lang.String;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n=in.nextInt();
String s=in.nextLine();
int d=0;
int a=0;
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='A')
a++;
else
d++;
}
if(a>d)
System.out.println("Anton");
else if(a<d)
System.out.println("Danik");
else
System.out.println("Friendship");
}
}
The Scanner.nextInt() method does not read the newline character in your input created by hitting Enter, So change your code like below instead of using nextInt();
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
String s = in.nextLine();
This will fix the issue
Scanner in=new Scanner(System.in); does not read the new line character , so i had created new object for the next line read , it will solve the problem
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in = new Scanner(System.in);
String s = in.nextLine();
if (n == s.length()) {
int d = 0;
int a = 0;
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'A')
a++;
else
d++;
}
if (a > d)
System.out.println("Anton");
else if (a < d)
System.out.println("Danik");
else
System.out.println("Friendship");
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Problem is I will print all the characters in the keyboard, however everytime it counts 10 the program will stop then press any key to continue.. the sequence will continue.. so far this is what i've done.
public class AsciiDisplay
{
public static void main (String [] args)
{
for (int i = 0; i<=255; i++)
System.out.printf("%d\t%c\n",i,i);
}
}
public static void main(String a[]){
int count = 0;
for (int i = 0; i<=255; i++){
if(count == 10){
System.out.println("Press \"ENTER\" to continue...");
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
count = 0;
}else{
System.out.printf("%d\t%c\n",i,i);
count++;
}
}
}
Logic:-
Maintain a counter, increment it every time you print a character, after every 10 increments, it will stop for the user to provide the input, as scanner methods are blocking.
count%10 ==0 means, the number is completely divisible by 10.
public static void main(String[] args){
{
int count=0;
Scanner scanner = new Scanner(System.in);
try{
for (int i = 0; i<=255; i++){
System.out.printf("%d\t%c\n",i,i);
count++;
if((count%10)==0){
System.out.println("Click to continue...");
scanner.nextLine();
}
}
}
finally{
scanner.close();
}
}
}
Though above code is not printing characters by taking input from keyboard.
Maybe, is good solution, beacuse I understood, You want press any key, after 10 numbers was printed starts from 0. Right?
import java.util.Scanner;
public class Test {
private static final Scanner SCANNER = new Scanner(System.in);
public static void main(String a[]) {
int count = 0;
for (int i = 0; i <= 255; i++) {
count++;
if (count % 10 == 0) {
System.out.printf("%d\t%c\n", i, i);
System.out.println("Press \"ENTER\" to continue...");
SCANNER.nextLine();
count = 0;
} else {
System.out.printf("%d\t%c\n", i, i);
}
}
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have written a simple test program for a larger program, where I need to validate the String by a range of letters from a through to g.
The below test program should ask for a letter, then if within range of a-g print an acceptance message, else say 'oops' and ask again.
Try this Code:
public static void main(String... params) {
Scanner s = new Scanner(System.in);
Character minValue = 'a';
Character maxValue = 'g';
while (true) {
System.out.print("enter a char between a-g: ");
Character input = s.nextLine().charAt(0);
if (input >= minValue && input <= maxValue) {
System.out.println("Ok");
} else {
System.out.println("oops");
System.exit(0);
}
}
}
You can get a char from String using charAt(index).
You can check if the char is withing given range using simple comparison like c >= 'a' && c <= 'g'.
First, I don't think you should name your variable "a".
Aniways, I would validate my input like this:
a.compareTo(maxValue) >= 0 && a.compareTo(minValue) <= 0 && a.length == 1
Please try with the below code:
import java.util.Scanner;
import java.util.Scanner;
public class StringValidation {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String a = null;
char minValue = 'a';
char maxValue = 'g';
boolean loop = true;
int i = 0;
while (loop) {
System.out.print("enter a char between a-g: ");
a = input.nextLine();
if ((int) minValue <= (int) a.charAt(i)) {
if ((int) maxValue <= (int) a.charAt(i)) {
System.out.println("Oops! ");
System.out.print("enter a char between a-g: ");
i++;
input.nextLine();
} else {
System.out.println("Accepted");
break;
}
}
}
input.close();
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
public class decisionMaker {
public static void main(String args[]) {
String option[] = new String[10];
// Output
for (int i = 0; i <= 9; i++) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the next option:");
option[i] = input.next();
System.out.println(" ");
}
for (int i = 0; i <= 9; i++) {
System.out.println("option: ");
System.out.println("option[i]+" ");
}
// Output
}
I'm trying to figure out how to add a count to the options, exit and end the program after entering a certain letter or number, and how to create a random output from the user input. I want it to give me one option that I had input at random. Can anyone help me with one or a few of these things. I'm trying to learn to code on my own, and I'm stuck on these.
Randomness
You can generate random numbers using java.util.Random;:
import java.util.Random;
public class SomeClass{
static Random rand = new Random();
public static void main(String args[]){
System.out.println(rand.nextInt());
}
}
About some broken code:
If you want to print out the value of a variable with System.out.println() then you need only type the variable without any quotation marks. The code you've written below will not compile:
System.out.println("option: ");
System.out.println("option[i]+" ");
Assuming that's what you want to do, it should instead be written as:
System.out.println("option: ");
System.out.println(option[i]);
Or even System.out.println("option: \n"+option[i]);
(The escape sequence \n when placed inside of quotation marks just indicates to the console to add a new line.)
Scanner:
Additionally, as nick zoum pointed out, your Scanner object should be initialized outside of the for loop, such as right underneath of the main() method.
Please comment below if you need clarification or if I misunderstood what you were looking for. It was very hard to understand your question.
You could try something like this:
public class DecisionMaker {
public static void main(String[] args) {
// output
Scanner scanner = new Scanner(System.in);
int size = getInt(scanner);
String option[] = new String[size];
for (int index = 0; index < size; index++) {
System.out.print("Enter the next option:");
option[index] = scanner.next();
}
int index = (int) (Math.random() * size);
System.out.println(option[index]);
scanner.close();
// output
}
public static int getInt(Scanner scanner) {
int size = 0;
while (size <= 0) {
if (scanner.hasNext()) {
if (scanner.hasNextInt()) {
size = scanner.nextInt();
}
}
if (size <= 0) {
System.out.println("The input: " + scanner.next() + " is not a valid value.");
}
}
return size;
}
}
How the program works:
The Scanner is initialized in the beginning and there is only
one instance of it.
Then the program will wait until the user inserts a valid number for
the size of options.
The next 5 lines were essentially copied from your code.
Finally we get a random Integer in the range of 0 - (size - 1) and print
the String of the array with that index.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to get the user's input, then store the input into an array. I'm going to get a string input and with this code, I thought it would work, but it's not working. Any help would be appreciated!
import java.util.Scanner;
public class NameSorting {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String[] array = new String[20];
System.out.println("Please enter 20 names to sort");
Scanner s1 = new Scanner(System.in);
for (int i = 0; i < 0;){
array[i] = s1.nextLine();
}
//Just to test
System.out.println(array[0]);
}
}
Since you know that you want to have an array of 20 string:
String[] array = new String[20];
Then your for loop should use the length of the array to determine when the loop should stop. Also you loop is missing an incrementer.
Try the following code to get you going:
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(System.in);
String[] array = new String[20];
System.out.println("Please enter 20 names to sort");
for (int i = 0; i < array.length; i++) {
array[i] = s.nextLine();
}
//Just to test
System.out.println(array[0]);
}
Look at your for-loop, it lacks of increment attribute. Example: for(int i = 0; i < 0; i++)
If you want to debug each loop I recommend you to print assignment inside for-loop
for (int i = 0; i < 0;)
{
array[i] = s.nextLine();
System.out.println(array[i]); // Debug
}
for (int i = 0; i < 0;){
array[i] = s.nextLine();
}
For the first iteration i will be initialised to '0' and since i should be less then '0' as per your condition it wont even go into the loop.change the loop to
for(int i=0;i<20;i++){
array[i]=s.nextLine();
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I can't figure out why the following program doesn't work. Please help me where did I make a mistake. Thank you.
import java.util.Scanner;
public class LargestNumber {
public static void main(String[] args) {
int[] numbers = new int[100];
int largestNumber = 0;
System.out.println("Enter numbers. When you want to finish, type 'finish'.");
Scanner sc = new Scanner(System.in);
do {
if (sc.hasNextInt()) {
for (int counter = 0; counter < 10; counter++)
numbers[counter] = sc.nextInt();
}
if (!sc.hasNextInt() && !sc.hasNext("finish")) {
System.out.println("It's neither number nor 'finish'.");
}
}
while (!sc.hasNext("finish"));
for (int x : numbers) {
if (x > largestNumber) {
largestNumber = x;
}
}
System.out.println("The largest number is: " + largestNumber);
}
}
This part:
do {
if (sc.hasNextInt()) {
for (int counter = 0; counter < 10; counter++)
numbers[counter] = sc.nextInt();
}
if (!sc.hasNextInt() && !sc.hasNext("finish")) {
System.out.println("It's neither number nor 'finish'.");
}
}
while (!sc.hasNext("finish"));
makes no sense. You:
test if the next thing in the input is an integer;
attempt to parse ten next tokens, assuming that they are all integers;
at the eleventh token you check whether it's another integer or "finish";
throw an exception if it's neither;
repeat everything if it's not "finish".
What you should actually do is something much, much simpler:
check next token:
if it's "finish", you're done;
if it's an integer, parse it;
otherwise throw error;
repeat this for up to 100 times;
you are done accepting input. Proceed to processing it.
I think that Scanner is unnessecarily complicated and doesn't work to much of the time. Here's how to do it the old fashioned way:
public class LargestNumber {
public static void main(String[] args) {
int largestNumber=0;
System.out.println("Enter numbers. When you want to finish, type 'finish'.");
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
String line;
while (!(line = r.readLine()).equals("finish")) {
int val = Integer.parseInt(line);
if (val > largestNumber)
largestNumber = val;
}
System.out.println("The largest number is: " + largestNumber);
}
}
For this, enter each number on a new line. I used a shorter algorithm here, which is to read one number, and if it is bigger than the maximum so far, the new number is the maximum so far
According to the doc:
Throws:
InputMismatchException - if the next token does not match the Integer regular expression, or is out of range
So i thing you could do this instead:
int counter = 0;
while (counter < numbers.length) {
if (sc.hasNextInt()) {
numbers[counter++] = sc.nextInt();
} else {
if (sc.hasNext("finish")) {
sc.close();
break;
} else {
System.out.println("It's neither number nor 'finish'.");
sc.next();
}
}
}
for (int x : numbers) {
if (x > largestNumber) {
largestNumber = x;
}
}
System.out.println("The largest number is: " + largestNumber);
hope that helps