Splitting Array By Specific Value
Jan 15, 2014How do you split an array by a specific value.
For instance this is my array: "4*6*9*89"
How can I take out "*" and therefore split up all the values like this: "4" "6" "9" "89"
How do you split an array by a specific value.
For instance this is my array: "4*6*9*89"
How can I take out "*" and therefore split up all the values like this: "4" "6" "9" "89"
The file has a 1000 strings that look like this.
6S 8D KS 2D TH TD 9H JD TS 3S
KH JS 4H 5D 9D TC TD QC JD TS
QS QD AC AD 4C 6S 2D AS 3H KC
4C 7C 3C TD QS 9C KC AS 8D AD
KC 7H QC 6D 8H 6S 5S AH 7S 8C
3S AD 9H JC 6D JD AS KH 6S JH
AD 3D TS KS 7H JH 2D JS QD AC
9C JD 7C 6D TC 6H 6C JC 3D 3S
QC KC 3S JC KD 2C 8D AH QS TS
AS KD 3D JD 8H 7C 8C 5C QD 6C
Each represents a poker hand. The issue is each line has player one and player two. I am trying to split them up so I can figure out who won.
package pokerHandCalculator;
import java.io.*;
import java.util.ArrayList;
public class PokerCalculator {
ArrayList<String>pokerHands = new ArrayList<String>();
void readFile()
[Code] ....
if there was a way to copy specific array info into a temp array with more concise code?
This is the code I have for instance:
tempList[0] = myCube.orange[2];
tempList[1] = myCube.orange[5];
tempList[2] = myCube.orange[8];
tempList[3] = myCube.blue[6];
tempList[4] = myCube.blue[7];
tempList[5] = myCube.blue[8];
But isn't there any way I could just condense it to be like:
tempList[0,1,2] = myCube.orange[2,5,8];
It would save me from 72 lines of code down to 24 if I could rock it all on one line.
I'm trying to iterate through an array of integers and find the start position of the part of the array containing the most consecutive 1s.
For example given the array {1,3,1,1,1,1,5,3,2,1,1}, the method should return 2 and given {1,4,5,1,1,1,5,1} the method should return 3.
So far, I've managed to retrieve the element where the consecutive 1s begin. However, I'm unsure how to get the index of this element and this implementation doesn't work if there is more than one set of consecutive 1s.
public class GetIndex {
public static int getPosition(int[] myArray) {
int index = 0;
int tracker = 0;
int mostOnes = 0;
for(int i = 0; i < myArray.length; i++) {
[Code] .....
how to index the arrays so that i can choose specific data so that averages and maximum and minimum values can be worked out. My code is below
package weatherProgramPackage;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
[code]....
I've been trying to make a list of numbers/ints and later on check that list for a specific number.
View Replies View RelatedI'm trying to build a monopoly like game, and atm I'm trying to find way how to build the Community and Chance chest cards. so far, my logic is
1-create an ArrayList of cards with a given order
2-for a given number of times(for loop) generate 2 random numbers ,which will be the parameters for Collection.swap().
3-swap.
here's the code for the shuffler Button
shuffler.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0;i<shuffeledMessages.length;i++){
int randoma=(int)(Math.random()*4);
int randomb=(int)(Math.random()*4);
Collections.swap(myMessages,randoma,randomb);
}
}
});
For now things seem to work pretty ok, but I'm wondering if this is a good and efficient way to shuffle a card chest especially in case of large number of cards. plus, I'm not sure what would be a good loop count for effective shuffling, in my case I used i<arraylist.size
I was inquiring about selecting random numbers from a Fibonacci array, the original post for which is here: Exiting a 'for' Loop Early. I have managed to achieve this with the following code:
System.out.println("Random numbers from the Fibonacci array");
for(int i = 0; i < limit; i++) //Limit is an 'int' of 15 & is set as the length of the Fib. array. I'm calling it for the Random array, too!!
{
Random dice = new Random();
int randomIndex = dice.nextInt(array.length); //The Fib. array was simply called 'array'!!
if (array[randomIndex] < 100)
{
System.out.print(array[randomIndex]+ " ");
}
}
When the code prints I get a random set of numbers which occur in the Fibonacci sequence preceding it. However, the actual length of this Random array also changes each time, and never more than the limit of 15 specified in the 'for' loop. What I want to try and do is print the Random array with a specific length each time. I've tried changing the conditional statement of the 'for' loop in different ways to set the Random array's length, but had no luck.
I am trying to write a specific byte sequence to a specific memory location on a removable storage drive. Does Java allow me a way to do this? I know the dangers in accessing memory, but the memory location of the data that will be written will never change.
how to assign a variable a memory location.
How do I move focus from a jcombobox to a specific component say a jtextarea.
My attempt below seems to be moving to a random component not the desired jtextarea.
takenByCombo.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_TAB ) {
e.consume();
dayJTArea.requestFocus(); // focust not moving dayJTArea
}
}
});
I applied the above logic to move focus from a specific jtextarea to another jtextarea as seen below and it works as desired.
dayJTArea.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_TAB ) {
e.consume();
monthJTArea.requestFocus();
}
}
});
If I specify the directory in file type or path string type how can I get a list of files that are file type .yml in that folder, so I can loop through and do something with each single file.yml in that specified directory.
View Replies View RelatedSo here we go with my problem:
- from the main class will arrive three variable (String name_used, int level_choose, int level_result)
I have a .txt file with this kind of formatting:
mario 1 1 0 1 0 1
carlo 0 0 0 1 1 0
...
Where I use 1 and 0 in the main for write if the level (you see that the numbers are always sixr? are egual to six level existing) BEFORE is done correct or wrong
- when in the main a user make a level a feedback coming back from the class level saying if the user made the count correctly or wrong. and i wanna replace the value (1 or 0) in the txt file with the new level result.
So i have all what i need as parameters i think.
name_used to look for the correct line in .txt file with .indexOf
level_choosed to go throught the correct index of that line
level_result (1 or 2) to be replaced with the existing one
Java Code:
public void salvaRisultati(String name_used, int level_choosed, int result_of_level) throws FileNotFoundException{
}
} mh_sh_highlight_all('java');
I have some problem to understand the way to make this:
In my main class a user can save his name in a txt file (and the system initially will add 6 value equals to 0) than he can choose between 6 level and make it.
example of .txt file data:
mario 0 0 0 0 0 0
carl 0 0 0 0 0 0
AT THIS MOMENT i just made other class and they work, is this new one that is hard for me. I'm trying to make a class that:
1- (first method called verificaRisultati) take name_used and level_choosed from the main and go to check in the .txt file if that level before was done right(1) or wrong(0)
and return something like "before you made this level properly" or "before you made this level incorrectly" AND THEN let the user start with the level.
2- (second method called salvaRisultati) at the end of the level i wanna pass the result (correct/incorrect) to another method of this class that will save the value (1 or 0) associated to the user in the right position.
This is the class that i'm writing:
Java Code:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class ResultUsers {
[Code] ....
I really need some hint and some code example because I'm stuck. How I can take exactly the line with the user name? How I can correctly split the line in an array and then read/modify the value for that level?
import java.util.Scanner;
public class namessplit{
public static void main(String args[]){
String fullName="";
String[] str;
[Code] .....
I need to y outpute like;
Enter the full name: xxxx xxxx
The names after split is:xxxx'xxxx
I am writing a program where I need to split an array of full names into First names and Surnames, using mapping. However, I am struggling how to split it up... and my First Names and Surnames list are both just displaying the full name.
public static void main(String[] args) {
String[] names;
names = new String[8];
Scanner s = new Scanner(System.in);
for (int i = 0; i < names.length; i++) {
System.out.println("Enter full student name:");
[Code] ....
I'm trying to use the split method to split a string for a calculator, but it keeps throwing this Exception:
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
+
public class exp {
private static String ques="88+12";
private static String [] splitCalc;
public static void main(String[] args) {
splitCalc = ques.split("+");
for(String s : splitCalc){
System.out.println(s);
}
}
}
I am developing a project where i have to split a zip file into fragments. Do we have any code for this splitting of zip files. I upload a zip file and it has to split into fragments and these fragments has to be renamed. I am having problem with splitting the zip file.
View Replies View RelatedI am currently trying to split the string "EAM est" between the part. I have gotten the code to work if the was a -. But I can't see why the error is occuring
I have tried
String test = "EAM-testing";
String[] parts = test.split("-");
System.out.println("parts[0] = " + parts[0]);
System.out.println("parts[1] = " + parts[1]);
String test1 = "EAM esting";
String[] parts1= test1.split("");
[Code] .....
The error occurs at line: String[] parts1= test1.split("");
I've written a program just for the sake of it (to learn) and it seem's like theres quite a lot in the main method that perhaps could be split up into seperate classes. I'm not too sure where should start with this though, or what my thought process should have been as I was writing the program.
import java.util.Scanner;
public class Loops {
public static void main(String[] args) {
int answer = 16;
Scanner scan = new Scanner(System.in);
// Question
System.out.println("What is 4 x 4 ?");
[code]...
--- Update ---
here's a version without code comments as they might make it harder to read here -
import java.util.Scanner;
public class Loops {
public static void main(String[] args) {
int answer = 16;
Scanner scan = new Scanner(System.in);
System.out.println("What is 4 x 4 ?");
int userAnswerInt = 0;
[code]...
How do I make a method that can seperate parts of a String. The String is [name,aid]... example [John,5],
I want to remove [ , and ] from the String... How can I do this?
This is what i've done this far:
static public String splitta(String tf)
{
String part1 = "";
String part2 = "";
String part11 = "";
String part22 = "";
String part111 = "";
String part222 = "";
String[] parts = new String[2];
[Code] ....
This dosn't work, it wont compile... It states the following:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at testprojekt.Validering.splitta(Validering.java:130)
I am trying to split a string into a String[] tokens array to declare variables for an object; however, I'm having an issue getting the string to tokenize correctly. Here's an example of the input:
a : 100 : John Smith : 20 Main St.
a : 101 : Mary Jones : 32 Brook Rd.
Here is the basic code I have now, to properly sort each line of text, etc. (without the split() method):
Java Code:
while (scanner.hasNextLine()) {
currentLine = scanner.nextLine();
lineScan = new Scanner(currentLine);
if (currentLine.startsWith("/") || currentLine.trim().isEmpty())
continue;
[Code] ....
I was able to eliminate the comments and identifiers from the text by trimming the first two characters of the string. For the split, I tried String[] tempArray = currentLine.split("s+"); however, that also took the spaces out of the addresses and names...so the results looked like this:
100
John
Smith
20
Main
St.
As you can see, it splits via space regardless, including where I replaced all the :'s with spaces. Is there any way to do this?
I'm trying to create a Java swing chess application, but would like to divide the GUI part of it into at least two different classes (possibly more later, but I'm not sure yet). Currently I have a mainGUI class and a ChessBoard class, both of which extend JFrame. I want the main GUI class to contain a JPanel which will house several components, including a JPanel originating from the chessboard class that contains the actual board.
Is there any way to do this? I tried just creating a ChessBoard object in my main GUI class and then adding it to the component, but I got an illegal argument exception, because apparently you can't add one JFrame to another. How to do what I'm trying to do, or just how to split GUIs into multiple classes in general?
Me and my mate are struggling to get our code to split a line in a text file which are separated by (":") and store into an array. We've managed to split each individual line into an array, but cannot work out how to do it with multiple splits over one line.
try{
FileReader fr = new FileReader("C:UsersTomDocumentsNetBeansPro jectsfootballA2_Tom_Tyler2srcukackingsto nci5100football_a2 om_mcLean_tyler_wattsmo delfootball_teams_phase1a.txt");
BufferedReader br = new BufferedReader(fr);
while(br.ready()){
String nextLine = br.readLine();
String[] fTArray = nextLine.split(",");
System.out.println();
for(String str : fTArray) {
System.out.println(str);
}
}
This code works but how would we do it for multiple splits over one line, along with this array?
I am new to a Java. I have a small project where I have a file of 10 million records of data located under d:/test/orders.csv
I would like to split this file into manageable file sizes of 5MB with the same name and be stored under a separate folder d:/test/output e.g
orders1.csv
orders2.csv
orders3.csv
..............
ordersn.csv
I am really struggling to make the Split String method work with another method which is double hours.
HtmlElement span = new HtmlElement(SPAN_OPEN, SPAN_CLOSE);
span.setValue("Drive Time: ");
td.addNestedElement(span);
HtmlElement span2 = new HtmlElement(SPAN_OPEN, SPAN_CLOSE);
span2.addAttribute("class", "drive_time");
String time=String.valueOf(showSet.getTransferHours());
/*Split the Strings into Hours and Minutes*/
[code]...
I am designing the game of Snake but it crashes after i added the checkScore() method.
package com.complex.snake;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
[Code] .....
The error i get is : Exception in thread "Thread-3" java.lang.ArrayIndexOutOfBoundsException: 1