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
In an interview I was asked a question like:
Take an input from console like: "welcome to world" and count a particular character entered by the user at which index and how many times(i.e. occurance of the character) without using any built-in methods like charAt(int ind) etc.
Giving you a potted answer won't help you at all. Here's how you can achieve what you want. Try to code it yourself.
Convert your String to a Character Array.
Have a Map which would store the char element as the key and its count as the value.
Iterate over the char array and for each element, check if its already existing in the map.
a. If it exists, increment the value for that element by 1 and put it back in the map.
b. If it doesn't, insert that char along with 1 as its count value in the map.
Continue till there are more elements in the char array.
The map now has all the char along with their respective counts.
import java.io.Console;
class dev
{
public static void main(String arg[])
{
Console c=System.console();
String str=c.readLine();
System.out.println(str);
int times=0;
//find c character
char find='c';
char strChar[]=str.toCharArray();
System.out.print("positions of char c in the string :");
try
{
for (int i=0; ;i++ )
{
if(strChar[i]==find)
{
System.out.print((i-1)+", ");times++;
}
}
}
catch (Exception e)
{
System.out.println("\n"+"The no. of times c occur : "+times);
}
}
}
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 1 year ago.
Improve this question
I need to print an array's element with it's index next to it. If the array element is even, it must display the element and its correlating index number, if the element is odd, it must display the element and "1".
For example:
Input: int[] array = new int [25]
Output: 0:0, 1:1, 2:2, 3:1, 4:4, 5:1, 6:6, 7:1 and so on
How would I do this?
You could do something like, You can check if a number is even then print the number and index separated by a colon else print number and 1 separated by a colon, This can be achieved by using for loop construct.Would recommend you to follow proper guide/tutorial as you look like a beginner in java.
public static void main(String[] args) {
int[] array = {1,2,3,4,5};
int counter = 0;
for(int i : array) {
if(i % 2 == 0)
System.out.print(i+":"+counter);
else
System.out.print(i+":1");
if(counter<array.length-1)
System.out.print(", ");
counter++;
}
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 2 years ago.
Improve this question
The question given asked us to print characters at even and odd postions seperately with two spaces between them ,i was able to find the strings but the spaces between them aren't printing.
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
String s=sc.next();
String temp1="";
String temp2="";
for(int j=0;j<s.length();j+=2)//getting characters at even locations
{
temp1+=s.charAt(j);
}
for(int j=1;j<s.length();j+=2)
{
temp1+=s.charAt(j);//getting characters at odd locations
}
System.out.println(temp1+" "+temp2);
}
}
}```
input as follows :
2
Hacker
Rank
output gained as:
Hceakr
Rnak
expected output as:
Hce akr
Rn ak
You you are not assigning odd character to temp2 so your temp2 is empty and System.out.println(temp1+" "+temp2); is printing temp1(Which contains even position's characters followed by odd position's characters) value and space, here is working code
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
String s=sc.next();
String temp1="";
String temp2="";
for(int j=0;j<s.length();j+=2)//getting characters at even locations
{
temp1+=s.charAt(j);
}
for(int j=1;j<s.length();j+=2)
{
temp2+=s.charAt(j);//getting characters at odd locations
}
System.out.println(temp1+" "+temp2);
}
}
Change
Actually in your code .
temp1+=s.charAt(j);//getting characters at odd locations
Suppose to be
temp2+=s.charAt(j);//getting characters at odd locations
Use the temp2 variable inside the second for loop instead of temp1 variable, in the code you posted above you didn't stored anything in the temp2 so it prints an empty string in the 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 3 years ago.
Improve this question
I'm trying to answer the question that asks for user input in entering three alphabetical letters (eg. a=1, b=2,… A=101, B=102, etc) and then the total sum.
So far I can only get the first lowercase user input working correctly?
Any suggestions would be very helpful.
You can convert the letters from their ascii value
Scanner scnObj = new Scanner(System.in);
System.out.println("Please enter a word");
String input1 = scnObj.nextLine();
char[] arr=input1.toCharArray();
for(int i=0;i<arr.length;i++)
{
int ascii=(int)arr[i];
if(ascii>=97&&ascii<=122)
{ //
total=total+ascii-96; //ascii are set values, a-z = 97-122 etc
}
else if(ascii>=65&&ascii<=90)
{
total=total+ascii+36; //This will convert A-Z values to 101 etc values
}
else if(ascii>=48&&ascii<=57)
{
total=total+ascii-48; //value of the digit to be worth itself
}
else
{
total=total+0; //special character to be worth 0
}
}
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
I was required to make a code that accepted a binary number (1's and 0's) and then counted how many ones were in that binary number. My code fulfills this purpose.
The second part of the exercise is this: if the user enters a number that is NOT binary, I must output that there is an error and keep prompting the user until they give a binary number.
Can someone show me how to incorporate this? I have tried several times but cannot make it click. Thanks! Here is my code.
import java.util.Scanner;
public class NewClass
{
public static void main( String [] args )
{
Scanner scan = new Scanner( System.in);
int i = 0, count = 0;
String number;
System.out.println("Please enter a binary number.");
number = scan.next();
String number1 = "1";
while ((i = number.indexOf(number1, i++)) != -1) {
count++;
i += number1.length();
}
System.out.println("There are "+ count + " ones in the binary number.");
}
}
You already know how to find all of the 1's; you should be able to do something similar to find all of the 0's.
If the sum of those two counts is not the length of the string, there must be at least one illegal character in it.
If you use the parseInt method you can do
Integer.parseInt("1100110", 2) returns 102
or in your case
int intVal = Integer.parseInt(number, 2);
as per the javadocs
Throws:
NumberFormatException - if the String does not contain a parsable int.
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
How to fill an array of integers in Java by asking the user to enter the number once ?
for example :
Enter 3 integers >: 123
then I want to get the array filled like that :
arr[0]=1
arr[1]=2
arr[2]=3
I hope the question is clear :)
rgds
One way of going about this would be to take the integer in as a String. You can then use toCharArray() in order to grab an array of the characters - these characters can then be individually parsed into Integer by utilizing Character.getNumericValue().
See the String and Character APIs for more details on those methods.
Small example of what I mean:
String numbers = "123";
char[] numArray = numbers.toCharArray();
for (char c : numArray) {
int num = Character.getNumericValue(c);
// Do what you will with the number.
}
As noted, this particular implementation would only work in this format if you are assuming that each integer is single digit.
How about this:-
Scanner input = new Scanner(System.in);
System.out.println("How many numbers do you want to enter?");
int num = input.nextInt();
int array[] = new int[num];
System.out.println("Enter the " + num + " numbers now.");
for (int i = 0 ; i < array.length; i++ ) {
array[i] = input.nextInt();
}