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 8 years ago.
Improve this question
Hey so I'm writing a program where, the user inputs exactly 10 words, and the computer outputs each word that has an A or a in it. I wrote the program with some help and now I need to now how how make it where the user can input 10 words back to back with a while loop. Here is what I have so far.
import java.util.Scanner;
public class ReadASH{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String name;
String S="";
System.out.println("Please enter any word.");
S = in.nextLine();
if(S.contains("a")||S.contains("A"))
System.out.println(S);
else System.out.println("The word you entered contains no a's or A's");
}
}
You need to actually have a loop, something like:
import java.util.Scanner;
public class ReadASH{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String name;
String S;
for (int i=0; i<10; i++) {
System.out.println("Please enter any word.");
S = in.nextLine();
if (S.contains("a")||S.contains("A"))
System.out.println(S);
else System.out.println("The word you entered contains no a's or A's");
}
}
}
Updated code based on comment:
import java.util.Scanner;
public class ReadASH{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String name;
int wordsToRead = 10;
String words[] = new String[wordsToRead];
for (int i=0; i< wordsToRead; i++) {
System.out.println("Please enter any word.");
words[i] = in.nextLine();
}
for (int i=0; i< wordsToRead; i++) {
if (words[i].contains("a")||words[i].contains("A"))
System.out.println(words[i]);
}
}
}
Related
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 months ago.
Improve this question
I'm starting to code and trying to do a challenge of turning a sentence into camelCase. After some experimenting on my own, got to the following code:
package teste;
import java.util.Scanner;
public class Teste {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Insert the sentence to be turned into camelCase: ");
String entry = keyboard.nextLine();
System.out.print("Insert the character that is used as space: ");
String space = keyboard.nextLine();
char current;
char next;
String output = null;
for (int i=0; i<=entry.length(); i++){
current = entry.charAt(i);
next = entry.charAt(i+1);
if (i == entry.length()){
output += Character.toLowerCase(current);
} else if (entry.substring(i, i+1).equals(space)){
output += Character.toUpperCase(next);
i++;
} else {
output += Character.toLowerCase(current);
}
}
System.out.println("This sentence in camelCase is: " + output);
}
}
There is an error I can't seem to avoid with the last index of the input, even with the first if structure made especially for it, and I can't find out why. Could anyone explain to me what I did wrong?
you should avoid StringIndexOutOfBoundsException exception in line 15 "entry.charAt(i+1)",
This code will fix your error.
import java.util.Scanner;
public class Teste {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Insert the sentence to be turned into
camelCase: ");
String entry = keyboard.nextLine();
System.out.print("Insert the character that is used as
space: ");
String space = keyboard.nextLine();
char current;
char next;
String output = "";
for (int i=0; i<entry.length()-1; i++){
current = entry.charAt(i);
next = entry.charAt(i+1);
if (i == entry.length()-2){
output += Character.toLowerCase(current);
} else if (entry.substring(i, i+1).equals(space)){
output += Character.toUpperCase(next);
i++;
} else {
output += Character.toLowerCase(current);
}
}
//here we test the last character
int len=entry.length();
current = entry.charAt(len-1);
if(!entry.substring(len-1, len).equals(space)){
output += Character.toLowerCase(current);
}
System.out.println("This sentence in camelCase is: " +
output);
}
}
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");
}
}
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 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 8 years ago.
Improve this question
import java.util.Scanner;
//trying to print pyramid shape
class try
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of lines:");
int n=in.nextInt();
for(int i=n;i>0;i--)
{
for(int j=1;j<=i;j++)
{
System.out.print(" ");
}
j=n-(i-1);
for(int k=0;k<j;k++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
But iam not able to print solve.
try is a reserved keyword in Java. You cannot use it as a class name. Simply rename your class - i.e. to MyTry
Reference: Java Language Keywords
try is a key word you have to change your class name first. (Use MyClass)
How you can do this, You have to define and initialize the variable before use them.
for(int j=1;j<=i;j++){
System.out.print(" "); // j visible inside for loop only
}
j=n-(i-1); // access j from outside for-loop is not possible
You can make this mistake correct by your own. Don't you use an IDE for coding?
Here is the solution of your problem.
import java.util.Scanner;
//trying to print pyramid shape
class Try
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of lines:");
int n=in.nextInt();
int i,j;
for(i=n;i>0;i--)
{
for( j=1;j<=i;j++)
{
System.out.print(" ");
}
j=n-(i-1);
for(int k=0;k<j;k++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}