Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying a for loop piece of code, where the user inputs the number of words he will write and then afterwards the words itself, and at the end it should check for specific requirements (words that sstart with K increase the count value and also the last word that started with K will get registered). However for some reason unknown to me, it just wont loop, no matter the input it just prints out everything on the code.
package Others;
import java.util.*;
public class StartsWithString
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
int wordNumber = 0;
String words = "";
int wordCount = 0;
String lastWord = "";
System.out.println("How many words are you going to write?: ");
wordNumber = sc.nextInt();
System.out.println("Write the desired words: ");
for(int i = 0; i < wordNumber; i++);
{
words = sc.nextLine();
if(words.startsWith("K"))
{
wordCount++;
lastWord = words;
}
}
System.out.println("From a total of " + numriFjaleve + " words typed,);
System.out.println(wordCount + " started with the letter K.");
System.out.println("The last word typed which began with the letter K was: " + lastWord);
}
}
for(int i = 0; i < wordNumber; i++);
You have a semicolon at the end of your loop definition. So your loop isn't actually a loop. It's a standalone code block which runs once. Remove the semicolon.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am trying to make it print out a range of specified numbers. So if they select option 1 and put in 1 and 15, I want it to print out 1 to 15. Once it gets to the while statement though it just prints nothing.
import java.util.Scanner;
public class Lab4 {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.print("Please choose your choice from the following menu");
System.out.print("\n1) Print through all integer numbers between two given integers");
System.out.print("\n2) Display a right triangular pattern of stars");
System.out.println("\n3) Quit");
int userInput = in.nextInt();
if (userInput == 1) {
System.out.print("Enter the start number: ");
int firstInteger = in.nextInt();
System.out.print("Enter the second number: ");
int secondInteger = in.nextInt();
while (firstInteger < secondInteger);
System.out.print(firstInteger);
firstInteger++;
} else if (userInput == 2) {
System.out.print("Enter the height: ");
int triangleHeight = in.nextInt();
} else if (userInput == 3);{
System.exit(userInput);
}
in.close();
}
}
You should change :
while (firstInteger < secondInteger);
System.out.print(firstInteger);
firstInteger++;
to
while (firstInteger < secondInteger) {
System.out.print(firstInteger);
firstInteger++;
}
while (firstInteger < secondInteger);
This will be treated as two executable lines of instructions,
which is similar like
while (firstInteger < secondInteger)
;
Try to remove the ; after while statement
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 2 years ago.
Improve this question
I would very much appreciate your help.
The prompt: Write a program that reads from the console a sequence of n integer numbers and returns these numbers on a single line with the correct sign (<, > or =) between the numbers.
I have no idea how to construct the answer.
Thanks in advance:
desperate novice
If this is your homework question, these are the hints:
To read from console, you can use these options:
System.console.readLine()
System.in.read()
Scanner class - Scanner myScanner = new Scanner(System.in); then myScanner.nextLine()
You can split the inputs via String.split(",") if they are seperated by commas
You can convert the splitted input strings into Integer by Integer.parseInt(<strings from last step>) by iterating over them
Then you can take any 2-2 input and compare them and add to string with correct sign via concatenating.
Then you can output them into console.
imports:
import java.util.Arrays;
import java.util.Scanner;
Logic:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine()); // No. of input to read
int[] inputs = new int[n];
for (int i = 0; i < n; i++) {
inputs[i] = Integer.parseInt(scanner.nextLine()); // inputs
}
// Arrays.sort(inputs) if you want the output to be sorted
for (int i = 0; i < n; i++) {
System.out.print(inputs[i]);
if (i == n - 1) continue;
String sign;
if (inputs[i] > inputs[i + 1]) {
sign = " > ";
} else if (inputs[i] < inputs[i + 1]) {
sign = " < ";
} else {
sign = " = ";
}
System.out.print(sign);
}
}
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 3 years ago.
Improve this question
I was going through a Palindrome( Specifically String Palindrome) Problem and was checking whether the string is palindrome or not. But a problem struck in the program
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int n,flag=0;
n=sc.nextInt();
char a[]=new char[n];
int l=0;
int h=n-1;
while(l<h)
{
if(a[l++]!=a[h--])
{
flag=1;
}
}
if(flag==1)
{
System.out.println("String is not Palindrome");
}
else{
System.out.println("String is Palindrome");
}
}
So above is the code which I wrote but the problem is, I have created a character array instead of the string.
The main point of the argument is the above code correct in terms of code standards.
is the above code correct in terms of code standards
Not really:
Don't name a local variable l (lowercase L). It is too easy to confuse with 1 (one).
Since I don't know what h is supposed to be a shorthand for, I changed l and h to i and j below, as those are very common integer iterator variable names.
Don't declare a local variable before it's needed. Use int n = sc.nextInt();
Don't put array declaration on the variable name. Put it on the type, since it defines the type.
Don't use 0 / 1 for false / true values. Change flag to a boolean, and name it better, e.g. describe its value. notPalindrome seems appropriate here. It helps document the code.
The while loop should be a for loop. It helps keeping loop logic together, and isolated from other logic, and it helps limit the scope of the loop variable(s).
Those were my comments related to coding standards.
However, your code doesn't work, because you never get a string from the user. Your choice of using char[] is fine, but you need to change the logic for getting it. See code below for how to use toCharArray() to do that.
Also, once a difference is found, you should exit the loop, either by also checking the boolean variable in the loop condition, or by using break. Personally, I prefer break.
Scanner sc = new Scanner(System.in);
String sentence = sc.nextLine();
char[] a = sentence.toCharArray();
boolean notPalindrome = false;
for (int i = 0, j = a.length - 1; i < j; i++, j--) {
if (a[i] != a[j]) {
notPalindrome = true;
break;
}
}
if (notPalindrome) {
System.out.println("String is not Palindrome");
} else {
System.out.println("String is Palindrome");
}
please use below code to check given String/number Palindrome or Not
public static void main(String args[]) {
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter String ");
original = in.nextLine();
int n = original.length();
for ( int index = n - 1; index >= 0; index-- ) {
reverse = reverse + original.charAt(index);
}
if (original.equals(reverse)) {
System.out.println("String is Palindrome");
} else {
System.out.println("String is not Palindrome");
}
}
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
so basically I want the user to do a input through the console and I want to finger out the first number and give it out on to the console, like for example:
hello465924whats334up // userinput
465924 // console output
this is basically the code that i have till now:
import java.util.Scanner;
public class ZahlZusammenFueger {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please enter something!");
String e = s.nextLine();
}
}
Since that number is substring, find the indexes where it begins and where it ends. Like this:
int i = 0;
int j = 0;
Scanner s = new Scanner(System.in);
System.out.println("Please enter something!");
String e = s.nextLine();
while (!Character.isDigit(e.charAt(i))) i++; // finding index
// where substring of first number starts
j = i;
while (Character.isDigit(e.charAt(j))) j++; // finding index
// where substring of first number ends
String number = e.substring(i, j));
Now, you can make Integer from it, or Long (depending on size) by doing this:
System.out.println(Integer.parseInt(e.substring(i, j)));
System.out.println(Long.parseInt(e.substring(i, j)));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
What is the code for making a simple program that displays asterisk depending on the number entered by the user. I want it to go like this
Enter 1st Integer:2
Enter 2nd Integer:3
Enter 3rd Integer:4
Enter 4th Integer:5
and this would be the result
1st Integer:**
2nd Integer:***
3rd Integer:****
4th Integer:*****
Simple as it is, I'm having problems regarding this code, I just need the loop for this.
int inputNumber = 2;
StringBuilder asteriskBuilder = new StringBuilder();
for (int i = 0; i < inputNumber; ++i) {
asteriskBuilder.append("*");
}
// 1st Integer:**
System.out.println("1st Integer:" + asteriskBuilder);
use StringBuilder
StringBuilder builder = new StringBuilder();
for (int i = 0; i < inputNumber; ++i) {
builder.append("*");
}
System.out.println(builder.toString());
if your input number can be big, StringBuilder is better solution for you
try
{
Scanner sc = new Scanner(System.in);
for(int i=1;i<=4;i++)
{
System.out.print(i+"st Integer:");
int input=sc.nextInt();
System.out.print(i+"st Integer:");
for(int j=0;j<input;j++)
{
System.out.print("*");
}
System.out.print("\n");
}
}
catch(Exception e)
{
}