Sorry for my English. I do not know why I did not properly displays a message GraphView. Now in the graph below displays constantly 1,1,1,1 ... Though would have 1,2,3,4 ...And evaluation are all 1. A shows the graph as 10. Why is it, tell me please.
GraphViewSeriesStyle seriesStyle = new GraphViewSeriesStyle();
BarGraphView graphView = new BarGraphView(this, "test");
//Our vertical graph
graphView.setVerticalLabels(new String[] { "10", "9", "8", "7", "6",
"5", "4", "3", "2", "1" });
//listMarks its ArrayList whith Marks
String[] array = new String[ listMarks.size() ];
//add marks in array
for(int i = 0; i < listMarks.size(); i++) {
array[i] = "1";
}
graphView.setHorizontalLabels(array);
seriesStyle.setValueDependentColor(new ValueDependentColor() {
#Override
public int get(GraphViewDataInterface data) {
return Color.rgb((int)(22+((data.getY()/3))), (int)(160-((data.getY()/3))), (int)(134-((data.getY()/3))));
}
});
GraphViewData[] data = new GraphViewData[array.length];
for (int a = 0; a < array.length; a++) {
data[a] = new GraphView.GraphViewData(a, Double.parseDouble(array[a]));
}
GraphViewSeries series = new GraphViewSeries("aaa", seriesStyle, data);
graphView.setManualYMinBound(0);
graphView.addSeries(series);
LinearLayout layout = (LinearLayout) findViewById(R.id.subLayout);
layout.addView(graphView);
Change:
array[i] = "1";
to:
array[i] = ""+i;
in the following loop"
//add marks in array
for(int i = 0; i < listMarks.size(); i++) {
array[i] = "1";
}
Related
I'm building a Java app that tracks a chosen playing card during an "Out" faro shuffle. I've been a magician for a long time and this just seemed like a fun thing to do, and while I've seen apps/sites that will show you the order of an out faro after every shuffle, they don't specifically track a chosen card.
I sort of have a brute force method for every shuffle. I only have two shuffles completed, but the rest of the shuffles would basically be copy/paste, which goes against DRY, but every shuffle has a specific order, so I'm not sure how I could ensure that order remains the same after every iteration. Maybe store each iteration in its own list?
Oh and an out faro is when the cards are perfectly weaved, but the top and bottom cards remain the same. So a perfect 26/26 split and then a perfect weave.
If anyone has any better ideas let me know. The code isn't very pretty yet and I have refactoring to do.
package Tracker;
import java.util.ArrayList;
import java.util.List;
public class NewDeckOrder {
private ArrayList<String> bicycleDeckOrder;
public NewDeckOrder() {
String[] suitesFirstHalf = {"Hearts", "Clubs"};
String[] valuesFirstHalf = {"Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"};
String[] suitesSecondHalf = {"Diamonds", "Spades"};
String[] valuesSecondHalf = {"King", "Queen", "Jack", "10", "9",
"8", "7", "6", "5", "4", "3", "2", "Ace"};
bicycleDeckOrder = new ArrayList<>();
for (String deckFirstSuites : suitesFirstHalf) {
for (String deckFirstValues : valuesFirstHalf) {
bicycleDeckOrder.add(deckFirstValues + " of " +
deckFirstSuites);
}
}
for (String deckSecondSuites : suitesSecondHalf) {
for (String deckSecondValues : valuesSecondHalf) {
bicycleDeckOrder.add(deckSecondValues + " of " +
deckSecondSuites);
}
}
}
public ArrayList<String> getList() {
return bicycleDeckOrder;
}
#Override
public String toString() {
return "New Deck Order " + bicycleDeckOrder;
}
}
package Tracker;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class OutFaro {
private ArrayList<String> oneShuffle;
private ArrayList<String> twoShuffles;
private ArrayList<String> threeShuffles;
public ArrayList<String> getOneShuffle() {
return oneShuffle;
}
public void setOneShuffle(ArrayList<String> oneShuffle) {
this.oneShuffle = oneShuffle;
}
public ArrayList<String> firstShuffle(ArrayList<String> newDeckOrder)
{
ArrayList<String> first26 = new ArrayList<>();
ArrayList<String> last26 = new ArrayList<>();
for(int i = 0; i < newDeckOrder.size() - 26; i++) {
first26.add(newDeckOrder.get(i));
}
for(int j = 26; j < newDeckOrder.size(); j++) {
last26.add(newDeckOrder.get(j));
}
// One Shuffle
oneShuffle = new ArrayList<>();
for (int i = 0; i < first26.size(); i++) {
for (int j = 0; j < last26.size(); j++) {
oneShuffle.add(first26.get(i));
oneShuffle.add(last26.get(j));
i++;
}
}
return oneShuffle;
}
public ArrayList<String> getTwoShuffles() {
return twoShuffles;
}
public void setTwoShuffles(ArrayList<String> twoShuffles) {
this.twoShuffles = twoShuffles;
}
public ArrayList<String> secondShuffle(ArrayList<String>
firstShuffleResults) {
ArrayList<String> first26SecondShuffle = new ArrayList<>();
ArrayList<String> last26SecondShuffle = new ArrayList<>();
for(int i = 0; i < firstShuffleResults.size() - 26; i++) {
first26SecondShuffle.add(firstShuffleResults.get(i));
}
for(int j = 26; j < firstShuffleResults.size(); j++) {
last26SecondShuffle.add(firstShuffleResults.get(j));
}
// Second Shuffle
twoShuffles = new ArrayList<>();
for (int i = 0; i < first26SecondShuffle.size() - 1; i++) {
for (int j = 0; j < last26SecondShuffle.size() - 1; j++) {
twoShuffles.add(first26SecondShuffle.get(i));
twoShuffles.add(last26SecondShuffle.get(j));
i++;
}
}
return twoShuffles;
}
}
import Tracker.NewDeckOrder;
import Tracker.OutFaro;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
NewDeckOrder deckOrder;
// Bicycle new deck order
System.out.println("Current deck order:");
NewDeckOrder newDeckOrder = new NewDeckOrder();
System.out.println(newDeckOrder.toString());
// One out faro
System.out.println("\nPlease enter the card you wish to track: ");
Scanner input = new Scanner(System.in);
String cardToTrack = input.nextLine();
deckOrder = new NewDeckOrder();
int cardsPosition = 0;
ArrayList<String> temp = deckOrder.getList();
if (temp.contains(cardToTrack)) {
cardsPosition += temp.indexOf(cardToTrack) + 1;
}
System.out.println("\n" + cardToTrack + " starting position is " + cardsPosition + ".");
System.out.println("\n" + "Performing one out faro.");
OutFaro outFaro = new OutFaro();
String[] firstHalfSuites = new String[]{"Hearts", "Clubs"};
String[] firstHalfValues = new String[]{"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
String[] secondHalfSuites = new String[]{"Diamonds", "Spades"};
String[] secondHalfValues = new String[]{"King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2", "Ace"};
outFaro.setOneShuffle(outFaro.firstShuffle(newDeckOrder.getList()));
// System.out.println("\n" + "First shuffle results: " + outFaro.getOneShuffle());
outFaro.setTwoShuffles(outFaro.secondShuffle(outFaro.getOneShuffle()));
int cardAfterOneShuffle = 0;
if (outFaro.getOneShuffle().contains(cardToTrack)) {
cardAfterOneShuffle += outFaro.firstShuffle(newDeckOrder.getList()).indexOf(cardToTrack) + 1;
}
System.out.println("\n" + "After one shuffle, your card is at number " + cardAfterOneShuffle);
int cardAfterTwoShuffles = 0;
// System.out.println("\n" + "Second shuffle results: " + outFaro.getTwoShuffles());
if (outFaro.getTwoShuffles().contains(cardToTrack)) {
cardAfterTwoShuffles += outFaro.getTwoShuffles().indexOf(cardToTrack) + 1;
}
System.out.println("\n" + "After two shuffles, your card is at number " + cardAfterTwoShuffles);
}
}
Alright, seems like every time I post on stackoverflow I end up finding the solution myself. Maybe it's just typing it out and working through it? Anyways, I found a much much much less convoluted way to do this recursively. I am actually proud of myself because I solved this 100% on my own so I guess my Java/programming knowledge is at a decent spot.
I don't think the code is 100% perfect yet, but far better than the "code vomit" solution I had before. Thank you to Meepo for kind of guiding me.
OutFaro Class that has the shuffling logic:
package Tracker;
import java.util.ArrayList;
public class OutFaro {
private ArrayList<String> shuffleResults;
private int shuffleCounter;
public ArrayList<String> getOneShuffle() {
return shuffleResults;
}
public ArrayList<String> outFaro(ArrayList<String> newDeckOrder, int numberOfFaros) {
shuffleResults = new ArrayList<>();
ArrayList<String> temp = new ArrayList<>();
while (shuffleCounter != numberOfFaros) {
for (int i = 0; i < newDeckOrder.size() - 26; i++) {
for (int j = 26; j < newDeckOrder.size(); j++) {
temp.add(newDeckOrder.get(i));
temp.add(newDeckOrder.get(j));
i++;
}
}
shuffleCounter++;
if (shuffleCounter == numberOfFaros) {
shuffleResults = temp;
} else {
outFaro(temp, numberOfFaros);
}
}
return shuffleResults;
}
#Override
public String toString() {
return "OutFaro{" +
"oneShuffle=" + shuffleResults +
'}';
}
}
New Deck Order class that builds the new deck:
package Tracker;
import java.util.ArrayList;
public class NewDeckOrder {
private ArrayList<String> bicycleDeckOrder;
public ArrayList<String> americanNewDeckOrder() {
String[] suitesFirstHalf = {"Hearts", "Clubs"};
String[] valuesFirstHalf = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
String[] suitesSecondHalf = {"Diamonds", "Spades"};
String[] valuesSecondHalf = {"King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2", "Ace"};
bicycleDeckOrder = new ArrayList<>();
for (String deckFirstSuites : suitesFirstHalf) {
for (String deckFirstValues : valuesFirstHalf) {
bicycleDeckOrder.add(deckFirstValues + " of " + deckFirstSuites);
}
}
for (String deckSecondSuites : suitesSecondHalf) {
for (String deckSecondValues : valuesSecondHalf) {
bicycleDeckOrder.add(deckSecondValues + " of " + deckSecondSuites);
}
}
return bicycleDeckOrder;
}
public ArrayList<String> getBicycleDeckOrder() {
return bicycleDeckOrder;
}
public void setBicycleDeckOrder(ArrayList<String> bicycleDeckOrder) {
this.bicycleDeckOrder = bicycleDeckOrder;
}
#Override
public String toString() {
return "New Deck Order " + bicycleDeckOrder;
}
}
Client code:
import Tracker.NewDeckOrder;
import Tracker.OutFaro;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int cardAfterOneShuffle = 0;
Scanner input = new Scanner(System.in);
// Bicycle new deck order
System.out.println("Current deck order:");
NewDeckOrder newDeckOrder = new NewDeckOrder();
newDeckOrder.setBicycleDeckOrder(newDeckOrder.americanNewDeckOrder());
System.out.println(newDeckOrder.getBicycleDeckOrder().toString());
// User input
System.out.println("\nPlease enter the card you wish to track: ");
String cardToTrack = input.nextLine();
System.out.println("\nHow many out Faro's would you like to shuffle?");
int numberOfFaros = input.nextInt();
int cardsPosition = 0;
if (newDeckOrder.americanNewDeckOrder().contains(cardToTrack)) {
cardsPosition += newDeckOrder.americanNewDeckOrder().indexOf(cardToTrack) + 1;
}
System.out.println("\n" + cardToTrack + " starting position is " + cardsPosition + ".");
OutFaro outFaro = new OutFaro();
outFaro.outFaro(newDeckOrder.getBicycleDeckOrder(), numberOfFaros);
System.out.println("\n" + "After " + numberOfFaros + " shuffles the deck order is: " + outFaro.getOneShuffle());
if (outFaro.getOneShuffle().contains(cardToTrack)) {
cardAfterOneShuffle += outFaro.getOneShuffle().indexOf(cardToTrack) + 1;
}
System.out.println("\n" + "After " + numberOfFaros + " shuffles your card is at number " + cardAfterOneShuffle);
}
}
I am writing a code for translating Signals from one form to another form.
My code works well but fails for the ends.
INPUT: String [] test = {"B","100","B","B","2","3","100","B","200","B","3","17","B","10" };
REQUIRED OUTPUT: B/101 B/1 B/106 B/201 B/21 B/11
GOT OUTPUT: B/1 B/101 B/1 B/106 B/201 B/21
Comparison of Required Output and got output
The first term B/1 is not required in got output.
B/11 is missing at the end in the required output.
ALGORITHM: "B" is replaced by "B/", and followed by addition of numbers appearing in Strings like "2", "3","100" which gives 105 and "1"
is to be added for "B" hence 106 and final result becomes 'B/106'.
I am new comer to java and programming. I need help to get the required output.
This is my code:
public class SignalConversion {
public static void main(String args[]) {
String [] test ={"B","100","B","B","2","3","100","B","200","B","3","17","B","10" };
int i=0; int x=test.length;
String netSignal="";
int total=0;
while(!(x==0)){
StringBuilder sb_matra= new StringBuilder();
StringBuilder sb_sur= new StringBuilder();
if(!test[i].equals("B")) {
total=total+(Integer.valueOf(test[i]));
}
else {
total=total+1;
sb_sur.append(test[i]+"/"+Integer.toString(total)+" " );
total=0;
}
netSignal=sb_sur.toString()+sb_matra.toString();
System.out.printf(netSignal);
i++;
x--;
}
}
}
When you encounter a "B", you should start summing the numbers following it, but only output the result when you encounter the next "B". That's why you have a problem at the ends. You print the first "B" when you encounter it, before calculating the number that should come with it.
Similarly, at the end of the loop, you should add an additional B with the last sum.
Here's a potential way of doing it (I think this loop is simpler than yours):
StringBuilder sb_sur= new StringBuilder();
boolean first = true;
for (int i = 0; i < test.length; i++) {
if(!test[i].equals("B")) {
total=total+(Integer.valueOf(test[i]));
} else {
if (!first) {
total=total+1;
sb_sur.append("B/"+Integer.toString(total)+" " );
total=0;
}
first = false;
}
}
total=total+1;
// account for the last B
sb_sur.append("B/"+Integer.toString(total)+" " );
I would have done this way,
public static void main(String[] args) {
String[] test = { "B", "100", "B", "B", "2", "3", "100", "B", "200",
"B", "3", "17", "B", "10" };
boolean foundB = false;
int total = 0;
for(int i=0;i<test.length;i++){
if(foundB){
if(test[i].equals("B")){
System.out.print("B/"+(total+1)+" ");
total=0;
}else{
total += Integer.parseInt(test[i]);
}
if(i==(test.length-1)){
System.out.print("B/"+(total+1)+" "); // The last B
}
}
if(test[i].equals("B")){
foundB = true; // start counting only after you find a B
}
}
}
Oh, i see Eran has made nearly the same attemp.
String[] test = { "B", "100", "B", "B", "2", "3", "100", "B", "200", "B", "3", "17", "B", "10" };
List<String> resultTest = new ArrayList<>();
int value = 0;
for (int i = 0; i < test.length; i++) {
if (i != 0 && test[i].equalsIgnoreCase("B")) {
resultTest.add("B\\" + (value + 1));
value = 0;
} else {
if (!test[i].equalsIgnoreCase("B")) {
value += Integer.parseInt(test[i]);
}
}
}
resultTest.add("B\\" + (value + 1));
resultTest.forEach(System.out::println);
I'm writing a function for a program and I need to generate a list of numbers in an Object[]
For example.
Object[] possibilities = functionName(13);
Should generate
Object[] possibilities = {"1", "2", "3","4","5","6","7","8","9","10","11","12","13"};
How should I go about achieving this?
String functionName(int number){
StringBuilder str = new StringBuilder("{");
for(int i = 1; i <= number; i++){
str.append(Integer.toString(i)).append(", ");}
String string = str.toString().trim();
string = string.substring(0, str.length()-1);
string += "}";
return string;
}
This should give you the desired string and you just print it.
First, you need a method to print the results from your functionName (that's setting a goal post). Something like,
public static void main(String[] args) {
Object[] possibilities = functionName(13);
System.out.println(Arrays.toString(possibilities));
}
Then you might implement functionName with a basic for loop like
static Object[] functionName(int c) {
Object[] ret = new String[c];
for (int i = 0; i < c; i++) {
StringBuilder sb = new StringBuilder();
sb.append("\"").append(i + 1).append("\"");
ret[i] = sb.toString();
}
return ret;
}
And when I run the above I get (the requested)
["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]
Try this method.
private Object[] function(int size) {
Object[] result = new String[size];
for (int i = 0; i < size; i++) {
result[i] = Integer.toString(i + 1);
}
return result;
}
}
I am keen to sort only numeric elements that are in String array. I am doing it in java. Please help me to solve this Problem.
Here is my Problem
For the given set of characters, choose integers only and sort them in descending order and put in their position leaving other characters position intact.
The change is position should only be of integers not of other characters.
Sample Input:-
d,1,4,c,9,6
109,87,911,b,645
77,19,#,.,95
8,99,14,2,5,6,49
Sample Output:-
Case #1: d,9,6,c,4,1
Case #2: 911,645,109,b,87
Case #3: 95,77,#,.,19
Case #4: 99,49,14,8,6,5,2
Thank you to all viewer. Please would you all help me to solve this problem in Java
Here is my Code, I have tried So far.
import java.util.Arrays;
import java.util.Iterator;
import java.util.ArrayList;
class alphaNumeric {
public static void main(String a[]) {
String s1[] = new String[9];
ArrayList l_numList = new ArrayList();
ArrayList l_strList = new ArrayList();
s1[0] = "1000.1";
s1[1] = "100";
s1[2] = "xBC100";
s1[3] = "XBB100";
s1[4] = "TEST";
s1[5] = "AYZ2100";
s1[6] = "99";
s1[7] = "90";
s1[8] = "1000";
System.out.print("Before sorting, numbers are ");
for(int i = 0; i < s1.length; i++)
{
System.out.print(s1[i]+" ");
}
System.out.println();
for (int i = 0; i < s1.length; i++) {
if (isNumber(s1[i])) {
l_numList.add(s1[i]);
} else {
l_strList.add(s1[i]);
}
}
Object[] l_objArray = (Object[]) l_numList.toArray();
int l_intArray[] = new int[l_objArray.length];
for (int i = 0; i < l_objArray.length; i++) {
l_intArray[i] = Integer.parseInt((String) l_objArray[i]);
}
Arrays.sort(l_intArray);
for (int i = 0; i < l_intArray.length; i++) {
System.out.println("after Numsort: " + l_intArray[i]);
}
System.out.print("After sorting, numbers are ");
for(int i = 0; i < l_intArray.length; i++)
{
System.out.print(l_intArray[i]+" ");
}
Object[] l_strArray = (Object[]) l_strList.toArray();
Arrays.sort(l_strArray);
for (int i = 0; i < l_strArray.length; i++) {
System.out.println("after Strsort: " + l_strArray[i]);
}
}
static boolean isNumber(String s) {
String validChars = "0123456789";
boolean isNumber = true;
for (int i = 0; i < s.length() && isNumber; i++) {
char c = s.charAt(i);
if (validChars.indexOf(c) == -1) {
isNumber = false;
} else {
isNumber = true;
}
}
return isNumber;
}
}
I couldn't figure out what you were trying to do, but here's how I'd do it
Extract a List of only the Integers AND create List of indexes at which they occur in original array
Sort that List using standard reverse sort
Put sorted List values back into the original array, using index List
Working example
public class SortOnlyNumbers {
public static void main(String[] args) {
sortOnlyNumbers(new String[] { "d", "1", "4", "c", "9", "6" });
sortOnlyNumbers(new String[] { "109", "87", "911", "b", "645" });
sortOnlyNumbers(new String[] { "77", "19", "#", ".", "95" });
sortOnlyNumbers(new String[] { "8", "99", "14", "2", "5", "6", "49" });
}
private static void sortOnlyNumbers(String[] array) {
List<Integer> indexes = new ArrayList<Integer>();
List<Integer> numbers = new ArrayList<Integer>();
for (int i = 0; i < array.length; i++) {
try {
numbers.add(Integer.parseInt(array[i]));
indexes.add(i);
} catch (NumberFormatException e) {
// don't care
}
}
Collections.sort(numbers, Collections.reverseOrder());
for (int i = 0; i < numbers.size(); i++) {
array[indexes.get(i)] = String.valueOf(numbers.get(i));
}
System.out.println(Arrays.toString(array));
}
}
Output
[d, 9, 6, c, 4, 1]
[911, 645, 109, b, 87]
[95, 77, #, ., 19]
[99, 49, 14, 8, 6, 5, 2]
I have an array, for example:
{ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }
I want to break it into sub array. When I do testing it, it displayed error:
java.lang.ArrayStoreException at line: String[] strarray = splitted.toArray(new String[0]);
Code:
public static String[] splittedArray(String[] srcArray) {
List<String[]> splitted = new ArrayList<String[]>();
int lengthToSplit = 3;
int arrayLength = srcArray.length;
for (int i = 0; i < arrayLength; i = i + lengthToSplit) {
String[] destArray = new String[lengthToSplit];
if (arrayLength < i + lengthToSplit) {
lengthToSplit = arrayLength - i;
}
System.arraycopy(srcArray, i, destArray, 0, lengthToSplit);
splitted.add(destArray);
}
String[] strarray = splitted.toArray(new String[0]);
return strarray;
}
From the java.lang.ArrayStoreException documentation:
Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects.
You are attempting to store a String[][] into a String[]. The fix is simply in the return type, and the type passed to the toArray method.
String[][] strarray = splitted.toArray(new String[0][0]);
Change
String[] strarray = splitted.toArray(new String[0]);
to
String[][] split = new String[splitted.size()][lengthToSplit];
for (int index = 0; index < splitted.size(); index++) {
split[index] = splitted.get(index);
}
You'll need to change your return type to be String[][]