I have no clue how I would research this otherwise, so here this is. I'm trying to create a program that will "flip bits", e.g. the string "00110011" would return "11001100". I tried to make a for loop to output the individual characters so see if getting the characters would work in this way, but it stops without outputting the characters.
public static void main(String[] args) {
String bitsList = "01010101";
char[] sepBits = bitsList.toCharArray();
System.out.println("Array'd");
int num = bitsList.length();
System.out.println("Got length");
for (int count = 0; count == num;) {
System.out.println(sepBits[count]);
System.out.println("Outputted " + sepBits[count]);
}
}
You never go in your for loop because count is 0 and num is 8 (length of "01010101"). Therefore count == num evaluates to false and the for loop is not entered.
Try to replace your for loop with:
for (int count = 0 ; count < num ; count++) {
// ...
}
The variable count is not equal to the variable num so the for loop never triggers. I think you are looking for <= not ==. Also you never change the count so the loop will just keep printing the same spot over and over even if you do change this.
this might work for you
public static void main(String []args){
String bitsList = "01010101";
char[] sepBits = bitsList.toCharArray();
int num = bitsList.length();
for ( int i = num - 1 ; i >= 0 ; i-- ) {
System.out.println("Outputted " + sepBits[i]);
}
}
Related
I'm trying to get all of the indexes of a Boolean array to be printed out where its element is true. The end goal is to be able to find a prime number of the indexes (where I change each index number that isn't prime to false in the array) then print out only what is left of the prime numbers of the indexes of the array.
The very first step I'm just trying to do is at least to get some integer index to print out, but nothing seems to be working and I don't know what is wrong.
public class PriNum{
private boolean[] array;
public PriNum(int max){
if (max > 2){ //I don't have any problems with this if statement
throw new IllegalArgumentException();
}
else{
array = new boolean[max];
for(int i = 0; i < max; i++){
if(i == 0 || i == 1){ //Automatically makes 0 and 1 false
//because they are not prime
array[i] = false;
}
else{
array[i] = true;
}
}
toString(); //I know for sure the code gets to here
//because it prints out a string I have
// there, but not the index
}
}
public String toString(){
String s = "test"; //this only prints test so I can see if
//the code gets here, otherwise it would just be ""
for (int i = 0; i < array.length; i++){
if(array[i] == true){
s = s + i; //Initially I tried to have the indexes returned
//to be printed and separated by a comma,
//but nothing comes out at all, save for "test"
}
}
return s;
}
}
EDIT: Included is the driver class that's requesting the print of the class PriNum
class Driver{
public static void main(String [] args){
PriNum theprime = null;
try{
theprime = new PriNum(50);
}
catch (IllegalArgumentException oops){
System.out.println("Max must be at least 2.");
}
System.out.println(theprime);
}
}
I tried running this, and the first change that needs to happen is to set this argument:
if(max < 2)
Then, if I'm reading this correctly: 0 and 1 are false. Every index after that is true. The output is fine as I see it. Just all the numbers crunched as a continuous list.
To get a better output, put a space between indexes:
if(array[i] == true){
s = s + " " + i;
}
You may even just output to screen directly as
if(array[i])
System.out.print( i );
numbers is initialized without declaration, array is declared but not initialized anywhere in your code. You have also a syntax error after array[i] = true, should be curly brace...
I need to see when a given variable value in an array is first occurred and once occurred change that variable to the number when it first occurs, and if it does not occur than change the value to -1.
For example 32 appears first in the array so it should print 1 but 100 never appears in the array so it should print -1 but how do I make a second if statement in my loop so that the variable will be -1 but it test the original value to find it appears. Sorry if I did not explain it well enough.
here is the code for the loop and the first if statement
public static int occurrence(int[] a) {
Scanner scnr = new Scanner(System.in);
int occurrence = scnr.nextInt();
for (int i = 0; i < a.length; i++)
if (a[i] == occurrence)
occurrence = i + 1;
return occurrence;
The JDK library comes with an Arrays class just for things like this.
First, import the class:
import java.util.Arrays;
Now all you have to do is this:
System.out.println(Arrays.binarySearch(a, occurrence));
And that's basically it. The "binarySearch()" method takes two parameters, those being the name of the array you're referencing (a), and the value you are searching for in that array (occurrence). It then returns the index of the value. If the value is not found in the array, it returns -1.
Take a look at this. Try to separate your input code (scanner) from the function logic. That will make the occurrence method reusable.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int data[] = { 2,5,7};
Scanner scnr = new Scanner(System.in);
int inputValue = scnr.nextInt();
int index = occurrence( data, inputValue);
System.out.println("value at index: " + index);
}
//
// find first match, then exit the loop if found.
//
public static int occurrence(int[] a, int inputValue) {
int result = -1;
for (int i = 0; i < a.length; i++)
if (a[i] == inputValue) {
result = i;
break;
}
return result;
}
}
My Question is :
First I have String variable of 1000 character, and I have another set of String variable of 1000 character
1] In First Set of Variable contains "1110000XXXXX0001111...." like this and so on, till 1000
2] In The second Set of Variable contains "1110000101010001111..." like this and so on till 1000
3] I need to get the position of X in first Variable and replace the Value of similar position from the second variable
For ex : 1st Variable of data "000XXX000X0"
2nd Variable of data "00011000010"
The X should be replaced by the values which is in the position in 2nd set of data.
NOTE : TO BE DONE WITHOUT LOOP
because if we put loop its runs 1000 times in a loop and 'X' may be anywhere in 1000 characters in the String
For ex: 1 Record 1000 Times
if 100K Records means 1000*100K (PERFORMANCE FAILS)
So need solution for it.
Kindly Help me out with this.
My Code is :
String sInputStr="0X11XXXXX000000000000000000000000000000000000000000000000X000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011";
String sDbStr="0111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011";
int iLength=sInputStr.length();
for(int i=0;i<iLength;i++){
if(sInputStr.charAt(i)=='X'){
}else{
if(i>sDbStr.length()){
break;
}else{
sChar[i] = sInputStr.charAt(i);
}
}
}//End of For
sVal=String.valueOf(sChar);
System.out.println("sVal == " +sVal);
Help Me friends
All you need is something like this
class FirstApp {
public static void main(String[] args) {
String sDbStr="0111111110000001234000000000000011";
StringBuilder sNewStr= new StringBuilder("011111111000000XXXX00000000000001112");
String findStr = "X";
int lastIndex = 0;
System.out.println("Starting");
long startTime = System.currentTimeMillis();
String result = replaceValues("X", sDbStr, sNewStr);
long endTime = System.currentTimeMillis();
System.out.println("Result");
System.out.println(result);
System.out.println(String.valueOf(endTime-startTime));
}
public static String replaceValues(String toReplace, String fromStr, StringBuilder toStr) {
int lastIndex = toStr.indexOf(toReplace);
if(lastIndex != -1){
toStr.replace(lastIndex,lastIndex+1,Character.toString(fromStr.charAt(lastIndex)));
System.out.println(toStr);
return replaceValues(toReplace, fromStr, toStr);
} else {
return toStr.toString();
}
}
}
sample result:
Starting
0111111110000001XXX00000000000001112
01111111100000012XX00000000000001112
011111111000000123X00000000000001112
011111111000000123400000000000001112
Result
011111111000000123400000000000001112
UPDATE Updated solution to ensure less execution time using stringBuilder and recursion
If X point to one value, like as mentioned X replace by 1. Go for string.replaceAll function.
ie.
String oriString="000XXX000X0";
String replaceOne=oriString.replace('X','1');
System.out.println(replaceOne);
If I understood the problem correctly then you want to replace the values of X in your first array with the values from seconds array at the same positions. For Example, array1: 000XXX000 & array2: 100101001. Then array1 should finally be 000101000.
Here is a simple code snippet to achieve this:
char[] arr1 = sInputStr.toCharArray();
char[] arr2 = sDbStr.toCharArray();
for(int i = 0; i < arr1.size(); i++)
if(arr1[i] == 'X')
arr1[i] == arr2[i];
The idea is to search for the index of the occurrence of the character say 'X' and copy all the characters from second string into the first as long as we find 'X'. Repeat the process till the last occurrence of 'X'.
public class Test
{
public static void main(String args[])
{
String s1 = "0X11XXXXX000000000000000000000000000000000000000000000000X000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011";
String s2= "0111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011";
char a[] = s1.toCharArray();
int i = s1.indexOf('X', 0);
while(i!=-1)
{
while(a[i] == 'X'){
a[i] = s2.charAt(i);
i++;
}
i = s1.indexOf('X',i+1);
}
s1 = new String(a);
System.out.println("result: "+s1);
}
}
The code below is a simplified version of a method I am working on for a java project. The method will sort through a list of items(two different categories), in this case 0,s and 1's. The code reads through an array of numbers stops at either 0 or 1 and then prints out both the 0 or one and the string of numbers following the 0 or 1. If a preceding string is a 1 or a zero then it will stop and switch to another if statement. However it only executes each statement once. However there is more in the array that it needs to read through and organize. I would like to set up some sort of loop so that it loops through the set of if statements until it has read through the entire array.
public class tester
{
public static void main(String[] args )
{
String flags[] = {"0","23","25","34","1","9","12","13","0","67","2","43"};
String array[] = new String[flags.length];
String zeros [] = new String[array.length];
String ones[] = new String[array.length];
int i,j,k,h;
int count = 0;
for (i = 0; i<flags.length; i++)
{
if (flags[i].equals("0"))
{
for (j=0; !flags[j].equals("1") ; j++)
{
count = j+1;
array[j] = flags[j];
zeros[j] = flags[j];
}
} else
if (flags[count].equals("1"))
{
j = 0;
for(k=count; !flags[k].equals("0");k++)
{
array[k] = flags[k];
j++;
ones[j-1] = flags[k];
}
}
}
for(i=0; i<zeros.length; i++)
{System.out.println(zeros[i]);}
System.out.println();
for(i=0; i<ones.length; i++)
{System.out.println(ones[i]);}
}
}
What it prints out now:
0
23
25
34
null
null
null
null
null
null
null
null
1
9
12
13
null
null
null
null
null
null
null
null
String flags[] = {"9","0","23","25","34","1","9","12","13","0","67","2","43"};
String array[] = new String[flags.length];
String zeros [] = new String[array.length];
String ones[] = new String[array.length];
int i;
boolean addingZeroes = false;
boolean addingOnes = false;
int zeroCount = 0;
int onesCount = 0;
for (i = 0; i<flags.length; i++) {
if (flags[i].equals("0")) {
zeros[zeroCount] = flags[i];
zeroCount++;
addingZeroes = true;
addingOnes = false;
} else if (flags[i].equals("1")) {
ones[onesCount] = flags[i];
onesCount++;
addingZeroes = false;
addingOnes = true;
} else if (addingZeroes) {
zeros[zeroCount] = flags[i];
zeroCount++;
} else if (addingOnes) {
ones[onesCount] = flags[i];
onesCount++;
}
}
for(i=0; i<zeroCount; i++) {
System.out.println(zeros[i]);
}
System.out.println();
for(i=0; i<onesCount; i++) {
System.out.println(ones[i]);
}
Hey, couple things were wrong. Basically, you need a little state machine where you need to know whether you are in the midst of storing the sequence after a 1 or a 0. I used the boolean values (eg addingZeroes) for that.
Then, you need to separately keep track of your element count (eg zeroCount) for each of the storage arrays. You might have 20 digits after a 0 and just 2 after a 1.
Finally, at the end, your length of your storage arrays is not what you want - you want the amount of values you ended up storing. That's why you got all those "nulls".
One other thing I noticed is that your j value is initialized always to 0 in the 0 block, so you would always be using the lowest values of the start array.
I am taking characters from a string and its values are added still I am getting a particular value.. suppose my value to get is 50 .. i need to take character each from a string and add its values
for example the value I need to get is 10
and value for letters a=1,b=2,c-3;
and the string is abca so its total value is 1+2+3+1 = 7
so 10 didn't reached so I need to add once agin from start like abca 7 + 1 + 2. So at the place of b we got the 10 value.. so the result is 2.
I know how to take the value for once but the second time calculation an dthird time I am not getting if anyone can help..pls help
The code so far I completed..
long currentValueFN = 0;
long value = 0;
char[] currentFN = new char[text.length()];
currentFN = text.toCharArray();
Long l = Long.parseLong(String.valueOf(currentAge));
for(int i=0; i<text.length(); i++)
{
currentValueFN += valueLetters( currentFN[i] );
if(currentValueFN >= l)
{
value = valueLetters( currentFN[i] );
}
}
return value;
long currentValueFN = 0;
long value = 0;
char[] currentFN = new char[text.length()];
currentFN = text.toCharArray();
Long l = Long.parseLong(String.valueOf(currentAge));
while(currentValueFN < l ) //check if current value is enough
{
for(int i=0; i<text.length(); i++)
{
currentValueFN += valueLetters( currentFN[i] );
if(currentValueFN >= l)
{
value = valueLetters( currentFN[i] );
break;
}
}
}
return value;
1) if there are enough characters in "text": will escape from for by break and then from while by expression;
2) if there aren't enough characters in "text": will finish for and start it again by while expression. until 1)
please forgive me if my answer is wrong.
1)I have taken a string variable called "variable" and converted it to character array called "character_array"(array name).
2)Then i converted the character array elements to its equivalent ascii code and it is stored in an integer array called "value_array"(array name).
3)Then i checked the condition that the givenvalue is less than the addedvalue.
public class Stack {
public static void main(String args[])
{
long addedvalue=0;
long requiredvalue=600;
String variable="ravi";
char characterarray[]=new char[variable.length()];
for(int i=0;i<variable.length();i++)
{
characterarray[i]=variable.charAt(i);
System.out.println(characterarray[i]);
}
int valuearray[]=new int[variable.length()];
for(int j=0;j<variable.length();j++)
{
valuearray[j]=(int)(variable.charAt(j));
System.out.println(valuearray[j]);
}
while(addedvalue<=requiredvalue)
{
for(int j=0;j<variable.length();j++)
{
valuearray[j]=(int)(variable.charAt(j));
if(addedvalue>=requiredvalue)
break;
addedvalue=addedvalue+valuearray[j];
System.out.println(j);
}
}
}
}
This should help you.
currentValueFN =0;
a=0;
while(currentValueFN !=l){
a=valueLetters( currentFN[i] );
currentValueFN +=a;
if(currentValueFN >= l)
{
value = a;
break;
}
}
return value;