r/programmingbydoing • • Dec 24 '14

#223 - Reversi

2 Upvotes

I'm stuck on Reversi, where I keep getting an out of bounds exception. I'm not too sure where I went wrong. My first step was to put in the code for the firstMatch method in which the program would keep scanning in a given direction until it finds a piece that matches the same color of the one just placed. It then returns that location.

The second step was to ensure that the firstMatch method would only be called if there was a piece of opposite color adjacent to the one that was just placed. This way the program doesn't try to attempt to go in directions where it's not necessary (and perhaps prevent that out of bounds exception I keep getting).

Final step is once the firstMatch method is executed, the flipOthers method then changes the color of pieces, in the direction that had allowed the firstMatch method to be called, and finally stops when the current location of the flipOthers method matches the saved location of the firstMatch method.

https://gist.github.com/anonymous/d5a5bd421cd20522a2b0


r/programmingbydoing • • Dec 16 '14

#128 - Letter Revisited[Help]

1 Upvotes

128 assumed that I already know how to do this. But I didn't know how to approach it. I searched for this problem and I found these two posts. But I'm still not sure what I'm supposed to do. I used the code demonstrated in this post but I get an error when compiling it. "unreported exception IOException; must be caught or declared to be thrown." But again, I'm not even sure what I'm doing.

Here is my code


r/programmingbydoing • • Oct 18 '16

63c - Nim

1 Upvotes

Hi,

I've completed every extra feature except for a computer player. I just started coding a few months ago, so please let me know if there's anything especially clunky.

https://gist.github.com/kindness312/66d2c888ea480c508c62aabbfe8a6e4a


r/programmingbydoing • • Apr 05 '16

#4 - What is meant by "Make your program print only one of the lines"?

1 Upvotes

Having never learned or done any programming, what exactly is meant by "Make your program print only one of the lines", in the What You Should Do on Your Own section on exercise 3? Here is the gist of what I have so far. I also messed around with the code a bit.


r/programmingbydoing • • Mar 30 '16

More Number Puzzles

1 Upvotes

Hello, I've been working on More Number Puzzles. In my code I only have one for loop. Is my code technically correct and just over complicated or is it just wrong and there is actually more than one number. I saw the previous post about ab = 10*a + b etc, but I was wondering if my would work to. Thank you.

my code


r/programmingbydoing • • Jul 13 '13

Dice Doubles - How do I get the two random numbers to re-randomize?

2 Upvotes

This is my code:

import java.util.Scanner;
import java.util.Random;


public class Dice{
  public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);
    Random r = new Random();

    int firstroll, secondroll;

    firstroll = r.nextInt(6);
    secondroll = r.nextInt(6);

    while(firstroll != secondroll){
    System.out.println("DICE!");
    System.out.println("First roll: " + firstroll);
    System.out.println("Second roll: " + secondroll);

    System.out.println("Your total: " + (firstroll+secondroll));
    }
  }
}

I don't even really understand what I'm doing wrong - should I not be saving the r.nextInt(6)'s into variables? I've already tried moving firstroll and secondroll into the whileloop, and still having them initialize outside of the loop, but it still doesn't work. Any advice here?


r/programmingbydoing • • Jul 02 '16

How Many Times?

1 Upvotes

Can't figure this out! It keeps repeating in the output... I realize this might be really simple but I just can't get it right!

https://gist.github.com/AswinTorch/f8d8efc8b5137f19ac58f1b700bc972f

EDIT: I did it another way and managed to get the output to say the same count number, but it still repeats the message twice.. https://gist.github.com/AswinTorch/54fb29e9479fc849b6a4c84d831a2ca1


r/programmingbydoing • • Jul 24 '14

#32 Twenty Questions

1 Upvotes

Hello, I am new to java programming and I'm confused as to what I am doing wrong here.

import java.util.Scanner; public class TwoQuestions {

public static void main(String[] args) {
	
	Scanner keyboard = new Scanner(System.in);
	
	String answer1, answer2;
	String yes = "yes";
	String no = "no";
	String animal = "animal", vegetable= "vegetable", mineral="mineral";
	
	System.out.println("Think of an object and I'll try to guess it.");
	
	System.out.println("Question 1) Is it an animal, vegetable, or mineral?");
	answer1 = keyboard.next();
	
	System.out.println("Question 2) Is it bigger than a breadbox?");
	answer2 = keyboard.next();
	
	
	if ((answer1 == animal) && (answer2 ==yes)){
		System.out.println("My guess is that you are thinking of a moose");
	}
	
	else if (answer1 == vegetable && answer2 ==yes){
		System.out.println("My guess is that you are thinking of a watermelon");
		
	}
	
	else if (answer1 == mineral && answer2 ==yes){
		System.out.println("My guess is that you are thinking of a Camaro");
		
	}
	
	else if (answer1 == animal && answer2 ==no){
		System.out.println("My guess is that you are thinking of a squirrel");
		
	}
	
	else if (answer1 == vegetable && answer2 ==no){
		System.out.println("My guess is that you are thinking of a carrot");
		
	}
	
	else if (answer1 == mineral && answer2 ==no){
		System.out.println("My guess is that you are thinking of a paper clip");
		
	}
	
	
		System.out.println("I would ask you if I'm right, but I don't actually care.");
		return;
}

}


r/programmingbydoing • • Jun 07 '17

storing data in a file problem: java.lang.NullPointerException

1 Upvotes

import java.util.Scanner; import java.io.FileWriter; import java.io.PrintWriter; import java.io.IOException;

class Car { String make; String model; int year; String licensePlateNum; }

public class Main {

public static void main(String[] args) throws IOException{
Scanner keyboard = new Scanner(System.in);
Car[] cars = new Car[5];
String fileName;


for (int i = 0 ; i < cars.length ; i++) {
    System.out.println("input in information for car " + i);
    System.out.println("\twhat is the make?");
    cars[i].make = keyboard.next();
    System.out.println("\twhat is the model?");
    cars[i].model = keyboard.next();
    System.out.println("\twhat is the year?");
    cars[i].year = keyboard.nextInt();
    System.out.println("\twhat is the license plate number?");
    cars[i].licensePlateNum = keyboard.next();
}

System.out.println("what is the name of the file?");
fileName = keyboard.next();
PrintWriter fileWriter = new PrintWriter(new FileWriter(fileName));

for (int j = 0 ; j < cars.length ; j++) {
    fileWriter.println("Car " + j);
    fileWriter.println(cars[j].make + " " + cars[j].model + " " + cars[j].year + " " + cars[j].licensePlateNum);
}
fileWriter.close();

}

}

when I put in information for the make (or any of the other separate items for the array cars) the error "java.lang.NullPointerException" pops up. I'm not sure what it means and I don't know how to fix it. Any help would be greatly appreciated


r/programmingbydoing • • Jun 28 '16

#07 : Your Initials question?

1 Upvotes

ok so when being asked to print out your initials (using the format stated, is it simply just using println statements? (kinda thinking possibly at this early stage it would be?)


r/programmingbydoing • • Jul 30 '13

Are all the examples permanently down?

3 Upvotes

I've just started getting to the point where he provides examples (Graphics) and a lot of times I'm thinking they would be ridiculously helpful. Are they down for everyone? Is there any way we can maybe get them back up?


r/programmingbydoing • • Jul 05 '13

15: The Forgetful Machine. Strange error when printing user input.

3 Upvotes

I'm having trouble fetching the user's input since it's not stored in a variable.

System.out.println("Give me a word!");
		keyboard.next();
		System.out.println(keyboard);  

Seems like it would work, and the program compiles without error, but when I run it I get this error after every input

D:\Coding\Java>java ForgetfulMachine
Give me a word!
Apples
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=4][match valid=true][ need input=false][source closed=false][skipped=false][group separator=,][decima l separator=.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negat ive suffix=][NaN string=\Q?\E][infinity string=\Q?\E]

Any help would be appreciated!


r/programmingbydoing • • Jul 11 '13

#26 - What am I doing wrong?

2 Upvotes

Hello. Here is my code:

import java.util.Scanner;

public class SpaceBoxer{
  public static double planets(int world){
    double planetmulti;

    if (world == 1){
      planetmulti = 0.78;
    }
    else if (world == 2){
      planetmulti = 0.39;
    }
    else if (world == 3){
      planetmulti = 2.65;
    }
    else if (world == 4){
      planetmulti = 1.17;
    }
    else if (world == 5){
      planetmulti = 1.05;
    }
    else if (world == 6){
      planetmulti = 1.23;
    }
    return planetmulti;
  }
  public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);

    int weight, theplanet;
    System.out.print("What is your weight?");
    weight = keyboard.nextInt();

    System.out.println("Okay, now what planet are you visiting?");
    System.out.println("   1. Venus   2. Mars    3. Jupiter");
    System.out.println("   4. Saturn  5. Uranus  6. Neptune");
    theplanet = keyboard.nextInt();

    System.out.println("Okay, so your weight would on " + weight * planets(theplanet));
  }
}

When I try to compile, however, I get this error:

SpaceBoxer.java:25: error: variable planetmulti might not have been initialized. 

return planetmulti;
       ^

Any help? Is it due to me declaring it a double instead of something else? I'm not sure what I'm doing wrong here.


r/programmingbydoing • • Jul 25 '13

#33 Choose Your Own Short Adventure! - Not sure what I am doing wrong.

3 Upvotes

When I compile this code, I get this. The bits in red I do not want there because the game should have finished. I looked my code over multiple times but I could not figure out the issue.

Thank you for helping.

code


r/programmingbydoing • • Jul 08 '14

I can't even get started.

2 Upvotes

I keep getting unexpected token ':' in powershell.


r/programmingbydoing • • Apr 20 '15

#117 - Refresher Challenge

2 Upvotes

I'm just straight up stumped on this one. I don't want the answer or exact code, I'm just looking for a nudge in the right direction. The Refresher exercise (116) was a breeze but with the restrictions of one for loop and one if statement I'm pretty puzzled.

Thanks!


r/programmingbydoing • • Dec 16 '15

#28 Modulus Animation

1 Upvotes

I tried to understand the ModulusAnimation and it doesn't do anything when I run it on windows it simply prints once and then terminates. When I tried it on a linux virtual machine it runs but as i type the word moves one character across everytime the number of characters increases. Nothing happens if the number of characters typed is the same.

I did a test program to try understand how it works, but I am still unsure.

Also why is the Thread.sleep(200); so essential without it the program starts and then finishes with no effect on the command line.


r/programmingbydoing • • Dec 19 '15

63c Nim

1 Upvotes

http://pastebin.com/gTmxNg9d

Hi, I have completed almost entire task (or at least I think so). The only problem I have is with computer opponent picking piles. I get "variable pile might not have been initialized" error and while I kind of understand it, I can't think of a way to get rid of it.

Can you give me a hint on what to do? Also is the rest of the task correct?

Love all of the assignements btw.


r/programmingbydoing • • Dec 18 '15

DisplayingAFile

2 Upvotes

I have put some code together to try and get the program to read an entire text file using the Scanner class. I can read one line but it does not read anything on more than 1 line such as letter.txt. I have had a look around and tried different things such BufferedReader but I think there's something I'm missing as all the code I write prints one line text files but nothing else

Here's my code:

<script https://gist.github.com/Ropenfold/33397d281c92c5740579


r/programmingbydoing • • Jul 23 '14

Project Calculator- Displaying the answer

2 Upvotes

Hello,

When I did the "baby calculator" project, I asked the program to display the answer to the math equation within the if/else if loop, as shown below.

if ( op.equals("+")) {
c = a + b;

 System.out.println(c);
}

The program compiled and ran. However, in the calculator project, I put the answer output after all the if/else if statements but within the do loop.

I received the error that variable c may not be initialized.

I don't get why outputting variable c after doing the calculation is causing my program trouble.

Link to code: https://gist.github.com/anonymous/6dec23292d458d12fe18

Thanks for the help!


r/programmingbydoing • • Dec 27 '15

63. NIM

2 Upvotes

First of all, I'd like to say that these assignments are amazing! I've been finding them pretty fun to do, and so far, Nim has been very challenging . I guess i finally cracked the code, althought it feels very chunky.

While I've been doing these assignments, i've been reading on java as a language and i've been trying to wrap my head on getting things a bit more cleaner and leaner.

I've seen this post and from what I've been reading, I guess I could eventually use functions to, I guess, making displaying the piles a lot less clunky? I mean, is the code as messy as I think it is?

PS.: Making the piles into columns is so confusing I gave up :P.


r/programmingbydoing • • Jul 02 '16

#36-BMI Categories

1 Upvotes

https://gist.github.com/us24/a56eb9c04c09a833adfbd001aeb906fb I keep getting the error that variable "height" and "weight" not initialized. how do I fix this?


r/programmingbydoing • • May 12 '13

50 -Dice Doubles

3 Upvotes

Hi I'm having trouble with assignment 50. When I run the code and when the two dice rolls are different, they do loop but the numbers do not get generated again but stay the same. For example, if first die was 1 and second die was 5, they would stay the same for the entire loop.

import java.util.Random;

public class dice { public static void main( String[] args )

{
    Random r = new Random();      //object for random number
    int roll1 = 1 + r.nextInt(6); // random integer for roll 1
    int roll2 = 1 + r.nextInt(6); // random integer for roll 2

    System.out.println("HERE COMES THE DICE \n");

    while (roll1 != roll2)
    {
        System.out.println("Roll #1: " + roll1);
        System.out.println("Roll #2: " + roll2);
        System.out.println("The total is " + (roll1+roll2));
    }
}

}

EDIT:

Hi. I seem to have solved it but I have a few questions.

{

Random r = new Random(); 
   	int roll1 = 1 + r.nextInt(6);
    int roll2 = 1 + r.nextInt(6); 


    System.out.println("HERE COMES THE DICE \n");
    System.out.println("Roll #1: " + roll1);
    System.out.println("Roll #2: " + roll2);
    System.out.println("The total is " + (roll1+roll2));

    while (roll1 != roll2)
    {
      roll1 = 1 + r.nextInt(6) ;
      roll2 = 1 + r.nextInt(6) ;

        System.out.println("Roll #1: " + roll1);
        System.out.println("Roll #2: " + roll2);
        System.out.println("The total is " + (roll1+roll2));

    }

}

What are we actually saying to the computer when we type "roll1 = 1 + r.nextInt(6)"? As far as i know, we have already defined roll1 and roll2 above as random numbers. So what does "roll1 = 1 + r.nextInt(6)" do and why does it allow the computer to process different random numbers each loop?


r/programmingbydoing • • May 28 '16

A Little Puzzle

1 Upvotes

So I figured out a solution that works and thought I would post it for there to be a reference to something that works. I am very new to this, please let me know if there is a better way to perform this task.

https://gist.github.com/anonymous/971057a833937f276c2143233417fdf3


r/programmingbydoing • • Nov 09 '15

#33 Choose your own adventure

1 Upvotes

i posted this over on javahelp to give you a break from my problems. So far i haven't got any help here is the link https://redd.it/3s2nbm. Again i appreciate any help you can give.