the first line takes the size of the array to follow.The array's digits are checked to see if they can form a ambiguous permutation.
public class Codechef2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int intnum=10;
intnum=input.nextInt();
input.nextLine();
String a[]=new String[100000];
int count=0;
int i=0;
While(intnum>0)
{
a[i]=input.nextLine();
String arr[]=a[i].split(" ");
int aj[]=new int[arr.length];
int k=0;
for(int j=0;j<arr.length;j++)
{
aj[Integer.parseInt(arr[k])]=j+1;
k++;
}
for(int l=0;l<arr.length;l++)
{
if(aj[l]==Integer.parseInt(arr[l]))
count++;
}
if(count==arr.length)
System.out.println("ambiguous permutation");
else
System.out.println("Not ambiguous permutation");
intnum=input.nextInt();
}
}
}
EDIT:
Again: pleas post a compilable code:
//wrong
//While(intnum>0)
while(intnum>0)
Also:
//you need to print a msg so the user knows that he / she
//needs to input and tell the user what to input
System.out.println("Please enter ....");
input.nextLine();
The code has many errors.
For example :
String aj[]=new aj[arr.length]; //will not compile
While(intnum>0) //will not compile
a[i]=input.nextLine(); //will not compile. i is defined later.
aj[arr[k]]=i+1; //wrong : aj[] is a String. i+1 is an int.
Fix them and post a compilable code.
Code below the while loop is not executed until the conditions of the while loop are met.
Related
I was trying to solve a question for Java Hashmaps where we need to search if key is present or not. If yes, print the String value otherwise print -1.
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Map<Integer,String> hmap=new HashMap<Integer,String>();
Scanner in=new Scanner(System.in);
int length= in.nextInt();
for(int i=0;i<length;i++) {
hmap.put(in.nextInt(),in.nextLine());
}
while(in.hasNext()) {
int number= in.nextInt();
if(hmap.containsKey(number)) {
String value=hmap.get(number);
System.out.println(value);
}
else {
System.out.println(-1);
}
}
in.close();
}
But all test cases are not passing. Can anyone please help with what is wrong?
Scanner has a problem interpreting your input if you use in.nextLine() after in.nextInt(). It is because the newline (\n) of your int input (e.g. 4 ENTER => 4\n) remains after returning the int via in.nextInt() and is recognized by the in.nextLine().
As workaround you could use an additional in.nextLine().
for(int i=0;i<length;i++) {
Integer key =in.nextInt();
in.nextLine();
String line = in.nextLine ();
hmap.put(key,line);
}
I keep trying different kinds of code and I always come back to this. but it never seems to work. The last if statement is making the i's underlined red but I can't even understand why. The homework was to make a program that takes user input and put it into an array and see if the user input is already sorted. Please Help!
import java.util.Scanner;
public class Sorting
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the array size: ");
int a = input.nextInt();
System.out.println("Enter the numbers using spaces between each number: ");
int[] numbers = new int[a];
for (int i=0; i<numbers.length; i++)
{
numbers[i]=input.nextInt();
if(isSorted(numbers))
{
System.out.println("Sort is already sorted");
}
else
{
System.out.println("Sort is not sorted sorry");
}
}
}
public static boolean isSorted(int[] numbers)
{
for(int i = 0; i<numbers.length-1; i++);
{
if(numbers[i]>numbers[i+1])
{
return false;
}
}
return true;
}
}
Close the for loop before the if statement.
for(int i = 0; i<numbers.length-1; i++); //<===== remove the ';' here
I think you missed place the ; after the for loop and that cause your issue.
I am running into a problem, that everytime the do{...} while(...) loop runs the second time, the first iteration of for(...) loop does not execute the following statement
array[o] = scan1.nextLine();.
This is what i have tried so far:
import java.util.Scanner;
public class test
{
public static void main(String args[]) throws Exception
{
int columns=2;
String array[]=new String[10];
char ins_check='y';
Scanner scan1=new Scanner(System.in);
do {
System.out.println("Enter the value");
for (int o = 1; o <= columns; o++) {
System.out.println("Enter the value");
array[o] = scan1.nextLine();
}
System.out.println("record inserted");
System.out.println("Do you want to insert again?(y/n)");
ins_check= (char) System.in.read();
}while(ins_check != 'n');
}
}
While several commenters told you what to do and what not to do, they didn't answer your question about the reason why this is occuring.
the first time of for loop does not execute this statement: array[o] = scan1.nextLine();
You are mistaken - the statement is well executed, it's only that an empty line is read. And this is because after the prompt "Do you want to insert again?(y/n)" you entered a line consisting of the two characters y \n, and System.in.read() read only one byte of data (the y), leaving the newline character \n in the input stream. The subsequent scan1.nextLine() gets this \n and returns the empty line.
Here is still someone who post answer faster then me. Take a look on reply from Amali, thats right answer :) this should work fine:
import java.util.Scanner;
public class test
{
public static void main(String args[]) throws Exception
{
Scanner scan1=new Scanner(System.in);
String array[]=new String[10];
String ins_check="y";
int columns=9;
do {
for (int o = 0; o <= columns; o++) {
System.out.printf("Enter the value for array[%s]",o);
array[o] = scan1.nextLine();
}
System.out.println("record inserted");
System.out.println("Do you want to insert again?(y/n)");
ins_check= scan1.nextLine();
}while(ins_check.equals("y"));
System.out.println("end");
}
}
So, I am very new at coding but have a college assignment to create a Word Manipulator. I am supposed to get a string and an INT from the user and invert every Nth word, according to the int input.
I am following steps and am stuck with this error at line 38 (the start of my last FOR LOOP). The compiler is giving me an Not an Statement Error in this line but I cant see where I went wrong.
Could someone gimme a light, please?
ps: I am not allowed to use Token or inverse().
import java.util.Scanner;
public class assignment3 {
public static void main(String[] args) {
// BOTH INPUTS WERE TAKEN
Scanner input = new Scanner (System.in);
String stringInput;
int intInput;
System.out.println("Please enter a sentence");
stringInput = input.nextLine();
System.out.println("Please enter an integer from 1 to 10. \n We will invert every word in that position for you!");
intInput = input.nextInt();
int counter = 1;
// ALL CHARS NOW ARE LOWERCASE
String lowerCaseVersion = stringInput.toLowerCase();
// SPLIT THE STRING INTO ARRAY OF WORDS
String [] arrayOfWords = null;
String delimiter = " ";
arrayOfWords = lowerCaseVersion.split(delimiter);
for(int i=0; i< arrayOfWords.length; i++){
System.out.println(arrayOfWords[i]);
// THIS RETURNS AN ARRAY WITH ALL THE WORDS FROM THE INPUT
}
// IF THE INTEGER INPUT IS BIGGER THAN THE STRING.LENGTH, OUTPUT A MESSAGE
// THIS PART IS WORKING BUT I MIGHT WANT TO PUT IT IN A LOOP AND ASK FOR INPUT AGAIN
if (intInput > arrayOfWords.length){
System.out.println("There are not enough words in your sentence!");
}
// NOW I NEED TO REVERSE EVERY NTH WORD BASED ON THE USER INPUT
//THIS IS WHERE THE ERROR OCCURS
for(int i=(intInput-1); i<arrayOfWords.length; (i+intInput)){
char invertedWord[] = new char[arrayOfWords.length()];
for(int i=0; i < arrayOfWords.length();i++){
ch[i]=arrayOfWords.charAt(i);
}
for(int i=s.length()-1;i>=0;i--){
System.out.print(invertedWord[i]);
}
}
}
}
(i+intInput) isn't a statement. That's like saying 12. Perhaps you mean i=i+intInput or i+=intInput which assigns a value to a variable
well, for one thing, i dont see "s" (from s.length()) initiated anywhere in your code.
In the program given I have to make sure that if two consequtive characters are the same. I shouldn't increase the value of the variable (Count)... I have tried "break;", but that skips me out of the "for loop" which is very counter-productive. How can I skip the given part and still continue the "for loop"?
Currently my output for "Hello//world" is 3. It should be 2 (the '/' indicates a ' '(Space)).
Code
import java.util.Scanner;
class CountWordsWithEmergency
{
public static void main()
{
Scanner input = new Scanner(System.in);
System.out.println("Please input the String");
String inp = input.nextLine();
System.out.println("thank you");
int i = inp.length();
int count = 1;
for(int j=0;j<=i-1;j++) //This is the for loop I would like to stay in.
{
char check = inp.charAt(j);
if(check==' ')
{
if((inp.charAt(j+1))==check) //This is the condition to prevent increase for
//count variable.
{
count = count; //This does not work and neither does break;
}
count++;
}
}
System.out.println("The number of words are : "+count);
}
}
You can use the keyword continue in order to accomplish what you are trying to do.
However you can also inverse your conditional test and use count++ only if it is different (!= instead of == in your if) and do nothing otherwise
if ((inp.charAt(j+1)) != check) {
count++;
}
The word you are looking for is "continue".
Try this:
if ((inp.charAt(j+1)) != check) {
count++;
}
Increment the value of count by checking with !=.
Try using continue where you want to skip an block.
Use "continue;" when you want to break the current iteration.
continue is a keyword in java programming used to skip the loop or block of code and reexecutes the loop with new condition.
continue statement is used only in while,do while and for loop.
You may want to use the continue keyword, or modify the logic a little bit:
import java.util.Scanner;
class CountWordsWithEmergency
{
public static void main()
{
Scanner input = new Scanner(System.in);
System.out.println("Please input the String");
String inp = input.nextLine();
System.out.println("thank you");
int i = inp.length();
int count = 1;
for(int j=0;j<=i-1;j++) //This is the for loop I would like to stay in.
{
char check = inp.charAt(j);
if(check==' ')
{
if((inp.charAt(j+1))!=check)
{
count++;
}
}
}
System.out.println("The number of words are : "+count);
}
}
Edit:
You may want to use the split method of the String class.
int wordsCount = str.split(' ').length;
Hope it helps :)
The following should work.
import java.util.Scanner;
class CountWordsWithEmergency
{
public static void main()
{
Scanner input = new Scanner(System.in);
System.out.println("Please input the String");
String inp = input.nextLine();
System.out.println("thank you");
int i = inp.length();
int count = 1;
for(int j=0;j<=i-1;j++) //This is the for loop I would like to stay in.
{
char check = inp.charAt(j);
if(check==' ')
{
if((inp.charAt(j+1))==check) //This is the condition to prevent increase for
//count variable.
{
continue;
}
count++;
}
}
System.out.println("The number of words are : "+count);
}
}