Why does my recursive backtracking stop at seemingly random points - java

I am attempting to generate all closed curves in a finite region of the simple hexagonal lattice. This isn't too important, it is just a finite set of points a distance of 1 away from each other. My code however, will generate closed curves for a while, and then my program will quit working and get stuck in a infinite loop? I have tried forcing java to garbage collect by command, but the same code stops at different points. As far as I can tell, where it stops is random.
Sphere is an array storing all the points in the region under consideration
private static Sphere Sphere1 = new Sphere();
private static double [][] vectors = {{0, 0, 1},{0, 0, -1},{1, 0, 0},{-1,0,0},{.5, (Math.sqrt(3))/2, 0},{-.5, -(Math.sqrt(3))/2, 0},{.5, -(Math.sqrt(3))/2, 0},{-.5, (Math.sqrt(3))/2, 0}};
private static StringBuilder loopString = new StringBuilder("r");
private static String posPoints = "abcdefghijklmnopqrstuvwxyz1234567";
private static int garbage = 0;
private static boolean once = false;
private static PrintWriter output = null;
public static void main (String [] args){
try {
output = new PrintWriter(new FileOutputStream("closedLoops.txt"));
} catch (FileNotFoundException e){
System.out.println("error");
}
System.out.println("start");
KnotPoint loopPoint = new KnotPoint(0,0,1);
addVector(loopPoint, vectors);
}
public static void addVector(KnotPoint loopPoint, double [][] vectors){
garbage ++;
if (garbage == 200){
System.gc();
garbage = 0;
}
if (loopString.length() > 19
&& (loopString.charAt(loopString.length()-1) == 'z' ||
loopString.charAt(loopString.length()-1) == '3' ||
loopString.charAt(loopString.length()-1) == 'u' ||
loopString.charAt(loopString.length()-1) == 'o' ||
loopString.charAt(loopString.length()-1) == 'g' ||
loopString.charAt(loopString.length()-1) == 'j' ||
loopString.charAt(loopString.length()-1) == 'q'))
{
System.out.println(loopString);
output.println(loopString);
once = true;
return;
}
for (int i = 0; i < 8 ; i ++){
if (validAdd(loopPoint, vectors[i], loopString, posPoints)){
loopPoint.addNext(vectors[i]);
addVector(loopPoint, vectors);
//System.gc();
loopString.deleteCharAt(loopString.length()-1);
loopPoint.subtractLast(vectors[i]);
if(loopString.toString().equals("r") ){
output.println("you did it");
output.close();
System.out.println("you did it good job");
System.exit(0);
}
}
}
return;
}
public static boolean validAdd(KnotPoint loopPoint, double [] vector, StringBuilder loopString, String posPoints){
KnotPoint testAdd = new KnotPoint();
testAdd.set(loopPoint);
testAdd.addNext(vector);
int pointIndex = 0;
boolean pointCheck = false;
char point = '.';
for (int i = 0; i < 33; i ++){
if( testAdd.equals(Sphere1.getKnotPoint(i))){
pointIndex = i;
pointCheck = true;
point = posPoints.charAt(i);
}
}
if (pointCheck && !loopString.toString().contains(posPoints.substring(pointIndex, pointIndex + 1))&& point != 'r'){// added not r check
loopString.append(point);
return true;
} else {
return false;
}
}

Related

Converting character map to array

I'm trying to learn java using the basic tictactoe example, but just for fun I wanted to design an ai later.
I'm having an issue flattening the char[] to Array. I'm so confused is there a better way to do this?
Do I need to create another method to specific convert this char[] to array?
ERROR:
The method mapToCharater(Charater.class::cast) is undefined for the type Stream<Object>
CONSOLE OUTPUT
EDIT:
Exception in thread "main" java.lang.ClassCastException: Cannot cast [C to java.lang.Character
at java.base/java.lang.Class.cast(Class.java:3610)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.stream.Streams$StreamBuilderImpl.forEachRemaining(Streams.java:411)
at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658)
at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:274)
at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:550)
at java.base/java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:260)
at java.base/java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:517)
at java.base/java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:523)
at aiPackage.game.Board.<init>(Board.java:17)
at aiPackage.game.Tester.main(Tester.java:15)
package aiPackage.game;
import java.util.*;
import java.util.stream.*;
public class Board {
//declaration of private members
private int score;
private Board previousB = null;
private char[][] thisBoard;
// ------------------------------------------------------;
public Board (char [][] inBoard) {
//what
//char[] flats = flatten(inBoard).map
Object[] flats = flatten(inBoard).map(Character.class::cast).toArray(); //mapToCharater(Charater.class::cast).toArray();
int[] flat = flatten(inBoard).mapToInt(Integer.class::cast).toArray();
int flatSize = flat.length;
// ------------------------------------------------------;
//check if square
if (Math.sqrt(flatSize)==3) {
if(inBoard.length == 3) {
thisBoard = inBoard;
}
else {
System.out.println("The array isnt a square.");
}
}
else {
System.out.println("It doesnt match the dimensions of a tictactoe board.");
}
//we'll assume its not gonna break from the input atm
setThisBoard(inBoard);
}
//https://stackoverflow.com/questions/31851548/flatten-nested-arrays-in-java
private static Stream<Object> flatten(Object[] array) {
return Arrays.stream(array)
.flatMap(o -> o instanceof Object[]? flatten((Object[])o): Stream.of(o));
}
public Board getPreviousB() {
return previousB;
}
public void setPreviousB(Board previousB) {
this.previousB = previousB;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public char[][] getThisBoard() {
return thisBoard;
}
public void setThisBoard(char[][] thisBoard) {
this.thisBoard = thisBoard;
}
//if there are even elements on the board, its x's turn
public ArrayList<Board> getChildren(){
return null;
}
public void checkIfEnded() {
for (int i = 0; i < 3; i++) {
//check row wins
if (thisBoard[i][0] != '-' &&
thisBoard[i][0] == thisBoard[i][1] &&
thisBoard[i][1] == thisBoard[i][2]) {
//updates score based on winner
if (thisBoard[0][2] == 'x') {
updateScore(1);
}
else {
updateScore(-1);
}
return;
}
//check column wins
if (thisBoard[i][0] != '-' &&
thisBoard[0][i] == thisBoard[1][i] &&
thisBoard[1][i] == thisBoard[2][i]) {
//updates score based on winner
if (thisBoard[0][2] == 'x') {
updateScore(1);
}
else {
updateScore(-1);
}
return;
}
}
//check diagnals
if (thisBoard[0][0] != '-' &&
thisBoard[0][0] == thisBoard[1][1] &&
thisBoard[1][1] == thisBoard[2][2]) {
//updates score based on winner
if (thisBoard[0][2] == 'x') {
updateScore(1);
}
else {
updateScore(-1);
}
return;
}
if (thisBoard[0][2] != '-' &&
thisBoard[0][2] == thisBoard[1][1] &&
thisBoard[1][1] == thisBoard[2][0]) {
//updates score based on winner
if (thisBoard[0][2] == 'x') {
updateScore(1);
}
else {
updateScore(-1);
}
return;
}
}
//outputs the board's contents as a string
public String toString() {
String result = "";
//gets the previous board's info to output first
result = "" + previousB;
//gets this boards info to output
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
result += thisBoard[i][j] + " ";
}
//adds newline char at end of row
result += "\n";
}
//adds an extra newline char at end of board
result += "\n";
return result;
}
private void updateScore(int win) {
if (win > 0) {
score = 1;
} else if(win == 0){
score = 0;
}else {
score = -1;
}
}
}
package aiPackage.game;
import java.util.*;
public class Tester {
private static Board start;
private static Board finish;
public static void main(String[] args) {
char temp [][] = {
{'o','-','x'},
{'-','-','-'},
{'-','-','-'}};
start = new Board(temp);
finish = minMax(start.getChildren());
System.out.println();
}
public static Board minMax(ArrayList<Board> resultList) {
return null;
}
}
The reason it fails is because Arrays.stream() does not accept char[] The following should work.
Arrays.stream(inBoard)
.flatMap(x -> (Stream<Character>)new String(x).chars().mapToObj(i->(char)i))
.collect(Collectors.toList())
check out
Want to create a stream of characters from char array in java for
and
Why is String.chars() a stream of ints in Java 8?
for better understanding.
Your problem is here:
private static Stream<Object> flatten(Object[] array) {
return Arrays.stream(array)
.flatMap(o -> o instanceof Object[]? flatten((Object[])o): Stream.of(o));
}
You are using the comparation with Object[] to decompose the array, but in reality is a char[].
That makes that your result instead of being an Stream Object is a Stream char[].
The main point is that you can't use a Stream to work with primitives. Stream only works with Objects and char is a primitive.
you can manually flatten the char[][] using a classic loop.
flattening char[][] or impossible doesn't count
char[] array = Stream.of( inBoard ).flatMap( arr -> Stream.of( String.valueOf( arr ) ) )
.collect( Collectors.joining() ).toCharArray(); // [o, -, x, -, -, -, -, -, -]

Dijkstra adjacency list

I have run into a problem converting pseudocode of Dijkstras algorithm into actual code. I was given and adjacency list such as "Location - adjacent location - distance to location," example for one node: AAA AAC 180 AAD 242 AAH 40.
My task was to read a file organized as adjacency list as described, and compute the shortest path from one node to another.
Here is the Dijkstra pseudocode:
void dijkstra( Vertex s )
{
for each Vertex v
{
v.dist = INFINITY;
v.known = false;
}
s.dist = 0;
while( there is an unknown distance vertex )
{
Vertex v = smallest unknown distance vertex;
v.known = true;
for each Vertex w adjacent to v
if( !w.known )
{
DistType cvw = cost of edge from v to w;
if( v.dist + cvw < w.dist )
{
// Update w
decrease( w.dist to v.dist + cvw );
w.path = v;
}
}
}
}
im having the most trouble with the line "for each Vertex w adjacent to v"
Here is my nonworking code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class Dijkstra {
public static boolean isInteger(String s) {
return isInteger(s, 10);
}
public static boolean isInteger(String s, int radix) {
if (s.isEmpty())
return false;
for (int i = 0; i < s.length(); i++) {
if (i == 0 && s.charAt(i) == '-') {
if (s.length() == 1)
return false;
else
continue;
}
if (Character.digit(s.charAt(i), radix) < 0)
return false;
}
return true;
}
public static void dijkstra(Vertex[] a, Vertex s, int lineCount) {
int i = 0;
while (i < (lineCount)) // each Vertex v
{
a[i].dist = Integer.MAX_VALUE;
a[i].known = false;
i++;
}
s.dist = 0;
int min = Integer.MAX_VALUE; //
while (!(a[0].known == true && a[1].known == true && a[2].known == true && a[3].known == true
&& a[4].known == true && a[5].known == true && a[6].known == true && a[7].known == true
&& a[8].known == true && a[9].known == true && a[10].known == true && a[11].known == true
&& a[12].known == true)) {
System.out.println("here");
for (int b = 0; b < lineCount; b++) {
if (a[b].dist < min && a[b].known == false) {
min = a[b].dist;
}
}
int c = 0;
while (c < lineCount) {
if (a[c].dist == min && a[c].known == false) {
break;
}
c++;
}
System.out.println(min);
a[c].known = true;
int adjSize = a[c].adj.size();
int current = 0;
System.out.println(adjSize);
while (current < adjSize - 1) {
String currentAdjacent = (String) a[c].adj.get(current);
int p = 0;
while (p < lineCount) {
if (a[p].name.equals(currentAdjacent)) {
if (!a[p].known) {
String cvwString = (String) a[c].distance.get(current);
int cvw = Integer.parseInt(cvwString);
System.out.println(" This is cvw" + cvw);
System.out.println("Here2");
if (a[c].dist + cvw < a[p].dist) {
a[p].dist = a[c].dist + cvw;
a[p].path = a[c];
}
}
}
p++;
}
current++;
}
}
}
public static class Vertex {
public List adj; // Adjacency list
public List distance;
public boolean known;
public int dist; // DistType is probably int
public Vertex path;
public String name;
// Other fields and methods as needed
}
public static void printPath(Vertex v) {
if (v.path != null) {
printPath(v.path);
System.out.print(" to ");
}
System.out.print(v);
}
public static void main(String[] args) throws IOException {
int lineCounter = 0;
BufferedReader br = new BufferedReader(new FileReader("airport.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
lineCounter = lineCounter + 1;
}
Vertex[] arr = new Vertex[lineCounter];
for (int i = 0; i < lineCounter; i++) {
arr[i] = new Vertex();
arr[i].adj = new LinkedList<String>();
arr[i].distance = new LinkedList<Integer>();
}
;
//
int arrayCounter = 0;
String everything = sb.toString();
String[] lines = everything.split("\\s*\\r?\\n\\s*");
for (String line1 : lines) {
arr[arrayCounter] = new Vertex();
arr[arrayCounter].adj = new LinkedList<String>();
arr[arrayCounter].distance = new LinkedList<Integer>();
String[] result = line1.split("\\s+");
for (int x = 0; x < result.length; x++) {
if (x == 0) {
arr[arrayCounter].name = result[0];
continue;
} else if (isInteger(result[x])) {
arr[arrayCounter].distance.add(result[x]);
continue;
} else {
arr[arrayCounter].adj.add(result[x]);
continue;
}
}
arrayCounter++;
}
for (int i = 0; i < 12; i++) {
System.out.println(arr[i].name);
}
System.out.println(lineCounter);
dijkstra(arr, arr[3], lineCounter - 1);
printPath(arr[11]);
} finally {
br.close();
}
}
}
Using my vertex class as is I was using a series of while loops to first, traverse the adjacency strings stored in a linked list while comparing to see which vertex is equivalent to the adjacency list string. Is there a better way to code "for each Vertex w adjacent to v" using my Vertex class? And apologies ahead for messy code and any others style sins i may have committed. Thanks!
To solve this problem you need a bunch of "Node" objects, stored in a HashMap, keyed on Source Location.
In the node, you need a collection of references to adjacent "Node" objects (or at least their "key" so you can write logic against it. The "Node" also needs to know it's location and distance to each "adjacent" node. Think Lundon Underground Tube Maps - each station connects to at least one other station. Usually two or more. Therefore, adjacent nodes to tube stations are the immediate next stops you can get to from that station.
Once you have that data structure in place, you can then use a recursive routine to iterate through each individual node. It should then iterate through each child node (aka adjacent node), and track distances from the initial (source) node to the current node by storing this data in a HashMap and using the current accumulated distance whilst recursing (or "walking" the graph"). This tracking information should be part of your method signature when recursing. You will also need to track the current path you have taken when recursing, in order to avoid circular loops (which will ultimately and ironically cause a StackOverflowError). You can do this by using a HashSet. This Set should track the source and current node's location as the entry key. If you see this present during your recursion, then you have already seen it, so don't continue processing.
I'm not going to code the solution for you because I suspect that you ask more specific questions as you work your way through understanding the answer, which are very likely answered elsewhere.

remove duplicate characters from a string

import java.util.Scanner;
public class StringWithoutDuplicate {
public static void stringWithoutDuplicate(String s1)
{
int n = s1.length();
int i = 0;
while(i<n)
{
if(s1.charAt(i) == s1.charAt(i+1))
{
if(s1.charAt(i) == s1.charAt(n-1))
{
System.out.println(s1.charAt(i));
}
i++;
}
else if(s1.charAt(i) != s1.charAt(i+1))
{
if(s1.charAt(i) == s1.charAt(n-1))
{
System.out.println(s1.charAt(i));
}
System.out.println(s1.charAt(i));;
i++;
}
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
s.useDelimiter(",");
String s1 = s.next();
System.out.println(s1);
stringWithoutDuplicate(s1);
}
}
The code is giving the output but with an exception
please tell me the error in my code and ways to correct it.
I don't want to change the logic of my code so kindly solve it using this logic only.
ERROR:
Range of your i is from 0 to (n-1) which is same as the range of index of characters in your string s1. This is correct.
But during the last iteration of your while loop, i = n-1
At this point, s1.charAt(i+1) becomes same as s1.charAt(n). This should be giving an error.
public static void stringWithoutDuplicate(String s1) {
int prev = -1;
for (int i = 0, size = s1.length(); i < size; ++i) {
char c = s1.charAt(i);
if (c != prev) {
System.out.println(c);
prev = c;
}
}
}

RockPaperScissors game using hashmap

I am working on a rock paper scissors game for my programming class and the professor wanted us to use a hash map to store the user's patterns and the times the pattern occurred in the hash map.
So I created a Pattern class that holds an array of values and in the Computer class I store it inside a hash map. The way I intended for the program to work is that the program will first generate a move based on the patterns in the hash map. If the map is empty then it would just generate a random move. Then after the user has made his move, his move will be put into an array to make a new pattern and the pattern will be saved to the hash map. If the pattern is already inside the map then the number of times it occurred will increase.
The predicted move is made by comparing the last three moves of the user with the patterns in the map and see which move will the user potentially throw next. So if the user last 4 moves were: R P S R then the program will take P S R and then add in R P S and see if those patterns are in the map. If they are, it will see which one is most likely to occur. Then if the user plays R next, the array will be updated into P S R R and the pattern continues.
So beginner mode is to start with an empty map and veteran is to load a previously saved map. However, I ran into some problems:
After I put the Pattern and times into the hash map, when I tried to iterate through it and see what patterns is it storing inside the map, I see that all patterns are the same and that does not suppose to happen. The pattern is suppose to be: R -> R P - > R P S (if the user throws rock, paper, scissors respectively) but now it just shows R P S -> R P S -> R P S. This can be seen in the getSize() in Computer.
I ran into a NullPointerException after the 4th move. The problem might be solved if I can solve the previous question but I have no idea why it happens.
I get a warning when I tried to read the map from a file so I was just wondering if the warning could potentially mess with the program.
Unchecked cast from Object to HashMap<Pattern, Integer>
Some help or pointers of what went wrong in my program would be greatly appreciated.
Computer:
import java.io.*;
import java.util.*;
public class Computer {
/**
* The hashmap that will holds the pattern and how many times it occured.
*/
private HashMap<Pattern, Integer> map;
/**
* Constructor
*/
public Computer() {
map = new HashMap<Pattern, Integer>();
}
/**
* Storing the pattern to the map.
*
* #param p
* The pattern that will be saved to the map.
*/
public void storePattern(Pattern p) {
Integer time = map.get(p);
// If time is null then the Pattern is not yet in the hashmap
if (time == null) {
map.put(p, 1);
} else {
map.put(p, time + 1);
}
}
/**
* Generating the computer's next move.
*
* #return The move that the computer will make.
*/
public char generateMove(Pattern user) {
int r = 0, p = 0, s = 0;
char returns = 'a';
if (!map.isEmpty()) {
char[] userPatts = user.getPattern();
char[] patts = userPatts.clone();
patts[patts.length - 1] = 'R';
Pattern testPatt = new Pattern(patts);
if (map.containsKey(testPatt))
r = map.get(patts);
patts[patts.length - 1] = 'P';
testPatt = new Pattern(patts);
if (map.containsKey(testPatt))
p = map.get(patts);
patts[patts.length - 1] = 'S';
testPatt = new Pattern(patts);
if (map.containsKey(testPatt))
s = map.get(patts);
if ((s - r) > 0 && (s - p) > 0)
return 'R';
if ((p - s) > 0 && (p - r) > 0)
return 'S';
if ((r - s) > 0 && (r - p) > 0)
return 'P';
if (s == r && r != 0)
return 'P';
if (s == p && s != 0)
return 'R';
if (r == p && p != 0)
return 'S';
}
// Throwing a random move
int max = (int) (Math.random() * 3) + 1;
if (max == 1)
returns = 'P';
else if (max == 2)
returns = 'S';
else if (max == 3)
returns = 'R';
return returns;
}
/**
* Loading the hashmap from a file.
*/
public void loadMap() {
File f = new File("HashMap.dat");
if (f.exists()) {
try {
ObjectInputStream in = new ObjectInputStream(
new FileInputStream(f));
map = (HashMap<Pattern, Integer>) in.readObject();
System.out.println("Successfully loaded.");
in.close();
} catch (IOException e) {
System.out.println("Error processing file.");
} catch (ClassNotFoundException e) {
System.out.println("Could not find class.");
}
}
}
/**
* Saving the hashmap to a file.
*/
public void saveMap() {
File f = new File("HashMap.dat");
try {
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(f));
out.writeObject(map);
System.out.println("Map saved.");
out.close();
} catch (IOException e) {
System.out.println("Error processing file.");
}
}
public void getSize() {
System.out.println("Map size: " + map.size());
for (Map.Entry<Pattern, Integer> entry : map.entrySet()) {
Pattern b = entry.getKey();
char[] a = b.getPattern();
for (int i = 0; i < a.length; i++) {// Why a.length allows i to go
// from 0 to 3 if a.length == 4?
System.out.print(a[i] + " ");// Why are all the patterns the
// same?
}
System.out.println();
}
}
}
Pattern:
import java.io.Serializable;
import java.util.Arrays;
public class Pattern implements Serializable {
/**
* Array that holds the patterns.
*/
private char[] pattern;
/**
* Constructor.
*/
public Pattern(char[] patt) {
pattern = patt;
}
/**
* Getting the pattern array.
*
* #return The pattern array.
*/
public char[] getPattern() {
return pattern;
}
/**
* Override the hashCode().
*/
#Override
public int hashCode() {
return Arrays.hashCode(pattern);
}
/**
* Override the equals()
*/
#Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof Pattern)) {
return false;
}
Pattern s = (Pattern) o;
return Arrays.equals(s.getPattern(), pattern);
}
}
Main:
import java.util.Scanner;
/**
* This program allows the user to play Rock Paper Scisors with a computer with
* a twist: The computer will try to predict the user's next move and try to
* beat it.
*
* #author:
*/
public class RockPaperScisors {
public static void main(String[] args) {
char computer = 'S';
int playerScore = 0, compScore = 0, tie = 0, full = 0;
char[] patt = new char[4];
Computer comp = new Computer();
boolean stop = false;
System.out
.println("Do you want to play veteran or beginner mode?\n1. Veteran\n2. Beginner");
int mode = input(2, 1);
if (mode == 1)
comp.loadMap();
comp.getSize();
while (!stop) {
// Generate computer's move.
computer = comp.generateMove(new Pattern(patt));
System.out.println("Enter R P S. Enter Q to quit.");
char a = input();
if (a == 'Q') {
stop = true;
break;
}
System.out.println("You threw: " + a);
if (full <= (patt.length - 1)) {
patt[full] = a;
full++;
} else {
for (int i = 0; i <= patt.length - 2; i++) {
patt[i] = patt[i + 1];
}
patt[patt.length - 1] = a;
}
for (int i = 0; i <= patt.length - 1; i++) {
System.out.print(patt[i]);
}
System.out.println();
// Store the new pattern
comp.storePattern(new Pattern(patt));
System.out.println("Computer plays: " + computer);
// Check for win or tie
if (a == computer) {
System.out.println("Tie.");
tie++;
} else {
if (a == 'R' && computer == 'P') {
System.out.println("Computer wins.");
compScore++;
}
if (a == 'R' && computer == 'S') {
System.out.println("Player wins.");
playerScore++;
}
if (a == 'P' && computer == 'S') {
System.out.println("Computer wins.");
compScore++;
}
if (a == 'P' && computer == 'R') {
System.out.println("Player wins.");
playerScore++;
}
if (a == 'S' && computer == 'R') {
System.out.println("Computer wins.");
compScore++;
}
if (a == 'S' && computer == 'P') {
System.out.println("Player wins.");
playerScore++;
}
}
// Saving the map
comp.saveMap();
comp.getSize();
System.out.println("Your score: " + playerScore + "\tTie: " + tie
+ "\tComputer score: " + compScore);
}
System.out.println("Thank you for playing.");
}
public static int input(int upper, int lower) {
Scanner in = new Scanner(System.in);
boolean valid = false;
int validInt = 0;
while (!valid) {
if (in.hasNextInt()) {
validInt = in.nextInt();
if (validInt <= upper && validInt >= lower) {
valid = true;
} else {
System.out.print("Invalid- Retry: ");
}
} else {
in.next();
System.out.print("Invalid input- Retry: ");
}
}
return validInt;
}
public static char input() {
Scanner in = new Scanner(System.in);
boolean valid = false;
char validChar = 'a';
while (!valid) {
if (in.hasNext()) {
validChar = in.next().charAt(0);
if (validChar == 'R' || validChar == 'P' || validChar == 'S'
|| validChar == 'Q') {
valid = true;
} else {
System.out.print("Invalid- Retry: ");
}
} else {
in.next();
System.out.print("Invalid input- Retry: ");
}
}
return validChar;
}
}
Why a.length allows i to go from 0 to 3 if a.length == 4?
In computer science you start counting at 0 so a length of 4 is
int array = new array[4];
array[0];
array[1];
array[2];
array[3];
Why are all the patterns the same?
in your main inside while (!stop) you should try patt = new char[4]; to ensure you don't use the same reference to that array over and over, because changing the base object will change all references aswell.
Just to clarify what i mean by references:
Is Java “pass-by-reference” or “pass-by-value”?

Return the sum of all integers from a random String without using regex

I was asked this question in an interview recently (Java programming que)
Return the sum of all integers from a random String.
Just iterate over the string, handle one digit at a time. This is pretty much exactly what the regex would do anyway:
String testStrings[] = { "-1a2b3c", "123ab!45c", "abcdef", "0123.4",
"dFD$#23+++12##T1234;/.,10" };
for (String testString : testStrings) {
String currentNumber = "";
int sum = 0;
for (int i = 0; i < testString.length(); i++) {
char currentChar = testString.charAt(i);
// Add digits or a leading minus to "currentNumber"
if (Character.isDigit(currentChar)
|| (currentNumber.equals("") && currentChar == '-')) {
currentNumber += currentChar;
} else {
// We've stumbled across a non-digit char.
//Try to parse the "currentNumber" we have so far
if (!currentNumber.equals("") && !currentNumber.equals("-"))
sum += Integer.parseInt(currentNumber);
currentNumber = "";
}
}
// Add the last "currentNumber" in case the string ends with a
// number
if (!currentNumber.equals("") && !currentNumber.equals("-"))
sum += Integer.parseInt(currentNumber);
System.out.println(sum);
}
Output:
4
168
0
127
1279
public class Random {
public int SumofNumbers(String s){
char[] str = s.toCharArray();
String answer="";
int sum = 0;
List<String> al = new ArrayList();
for (int i=0;i< str.length;i++){
if (checkNumber(str[i])){
answer=answer+str[i];
}
else
{
if(!answer.isEmpty()){
al.add(answer);
answer = "";
}
}
if (i == str.length -1 && !answer.isEmpty()) {
al.add(answer);
}
}
for (String a1 : al){
sum = sum + Integer.valueOf(a1);
}
return sum;
}
private boolean checkNumber(char c) {
if ((int)c > 47 && (int)c < 58){
return true;
}else if ((int)c == 45){
return true;
}
return false;
}
public static void main(String [] args){
Random r = new Random();
String test = "123ab!45c";
System.out.println(r.SumofNumbers(test));
}
}
public class StringToIntAddition {
public static void main(String[] args) throws Exception {
String str = "2e40 ssdf 23-9", number="";
int sum=0;
for(int i=0; i<str.length() ;i++){
if(Character.isDigit(str.charAt(i))){
number += str.charAt(i);
}
else if(!number.isEmpty()){
sum += Integer.parseInt(number);
number= "";
}
if (str.charAt(i) == '-'){
number = "-" ;
}
}
if(!number.isEmpty()){
sum += Integer.parseInt(number);
}
System.out.println("number= " + sum);
}
}
I've got a slightly 'cute' way to do this in Java 8: implement it as a Collector
public DigitCollector {
private boolean negative = false;
private int current = 0;
private int total = 0;
public int getTotal() {
if (negative) {
total -= current;
} else {
total += current;
}
current = 0;
negative = false;
return total;
}
public void accept(Character ch) {
if (Character.isDigit(ch)) {
current = 10 * current + Integer.parseInt(ch.toString());
} else if (ch.equals('-')) {
negative = true;
} else {
getTotal();
}
}
}
Now you can collect a stream of characters:
text.chars().map(ch -> new Character((char)ch))
.collect(DigitCollector::new, DigitCollector::accept, null)
.getTotal();
I realise the mapping ch -> new Character((char)ch)) looks strange but .chars() returns a stream of integers instead of characters. See here for reasons why (though pretty much everyone agrees it was a mistake).
This is a slightly longwinded way of doing it but it's pretty flexible: you could take a stream of Character from anywhere and do any sort of manipulation you wanted before collecting them. It seems to me to be a natural representation of the problem and, mostly, I just reckon streams are cooler than traditional iteration :-)
There's already quite a few answers, but this one seemed fun. I have a different solution that should be pretty efficient:
public static int countString(String input) {
if (input == null) return 0;
int sum = 0;
int accumulator = 0;
boolean lastCharWasDigit = false;
for (int i = 0, len = input.length(); ++i) {
char c = input.charAt(i);
// If a non-digit character is found, clear the
// accumulator and add it to the sum.
if (c < '0' || c > '9') {
sum += accumulator;
accumulator = 0;
lastCharWasDigit = false;
continue;
}
// If the previous character was a digit, that means
// this is a continuation. Multiply by ten to shift
// it over one power of ten before adding the new value
if (lastCharWasDigit) {
accumulator *= 10;
}
// Add the integer value of the character
int charValue = c - '0';
accumulator += charValue;
lastCharWasDigit = true;
}
// Finally, clear the accumulator for any ending digits,
// and return the sum
sum += accumulator;
return sum;
}

Categories

Resources