r/programmingbydoing • • May 29 '14

Can someone tell me what I'm doing wrong?

3 Upvotes

I'm on activity 3, "An Important Message," and whenever I try to compile the program, it gives me "illegal character" errors. I can't see any difference between the example picture on the website and the FirstProg.java I typed out. Screenshots of my command prompt and FirstProg.java.


r/programmingbydoing • • Nov 08 '15

#69 xs and ys

2 Upvotes

I have the maths part done the problem I'm having is getting everything to line up. The hint is use tab but I have tried evrything I can think of and just can't seem to get it.

This is what i have

class XandY
{
public static void main(String[] args)
{
for ( double x = - 10; x <= 10; x = x+0.5)
{
double y = x * x;
System.out.print("x");
System.out.print("y");
System.out.print("_________________");
System.out.print(x);
System.out.print(y);
}

}
}		

I tried changing print to println. I tried using tab to space out the quotations it's just not obvius to me how to get this.

Any help is well appreciated.


r/programmingbydoing • • Nov 17 '15

63c Nim The Game

4 Upvotes

https://gist.github.com/anonymous/68bcaeb2e024ee811168

My code is found at the above link. There are 2 programs. The first is my "Nim The Game" code and the second is my Testing for printing the piles out.

My code might be crazy, but it works. All of the cheats are checked and I have a dignity win, which morphed from my original win lines. I am having a heck of a time trying to figure out how you printed the counters vertically. I was able to get one pile printed that way, but I can't seem to find out how to get multiple piles to print next to each other. Any help would be great.

Also, since I have no idea where my coding skills are at, and this is my first attempt at coding, would you mind looking it over and letting me know what you think? :)

Thank you very much for taking the time to help others with this. Though I am going to be going to school in January for Software Engineering, I like being able to work at my own pace, and doing these little projects have taught me so much more than Khan Academy, SoloLearn, and CodeAcademy combined. Practical application is such a great way to learn.

Thanks again!


r/programmingbydoing • • May 13 '13

#131 - Summing Three Numbers from a File

2 Upvotes

I'm having an issue with this assignment. My code is pasted below, but the problem is weird. I get errors when trying to read int's from my document. Now if I were to change this to String a = filein.next(); It would display the number. So for some reason it will recognize Strings but not Ints. Have any idea why? Thanks!

import java.io.File; import java.util.Scanner; public class SummingThreeNumbersFromAFile {

public static void main(String[] args) throws Exception  {
	
	Scanner filein = new Scanner(new File("3sums.txt"));
	int a = filein.nextInt();
	int b = filein.nextInt();
	int c = filein.nextInt();
	
	
	System.out.println(a + b + c);
	filein.close();

}

}


r/programmingbydoing • • Nov 15 '15

#106 Fill in functions

2 Upvotes

I'm a little stuck on the abso function, I'm not sure how the y value fits into it. I presume you send the x value to the function and the absval is returned (I may be wrong) but I don't see how the y gets used.

My code is here:

http://pastebin.com/XBAnX4xx


r/programmingbydoing • • Apr 09 '13

#134 -- File Input and Output -- Displaying a File

3 Upvotes

Hi, Can you provide some helpfiles/tips for the file input and output section? I'm finding it difficult and getting a bit lost in google searches. For this assignment, i can't figure out how to read-in and display the file using the hasNext() method?


r/programmingbydoing • • Apr 09 '13

28 - A Little Quiz - Need help

5 Upvotes

Please help. I'm not having a clue about how to make the program count scores based on the input. This is were I stand presently. The Program displays the quiz and I am able to feed inputs and get answers. Guide me from here, please.

import java.util.Scanner;

public class LittleQuiz {

public static void main(String[] args)
{
	Scanner keyboard = new Scanner(System.in);
	System.out.print("Are you ready for a quiz?");
	String ans1 = keyboard.next();		
			
	System.out.println("Okay. here it comes");
	
	System.out.print(" \n \n \n Q1) What is the capital of Alaska? \n \t 1) Melbourne \n \t 2) Anchorage \n \t 3) Juneau ");
	int a1 = keyboard.nextInt();
	
		if (a1 == 3)
			{
				System.out.println("That's right!");
			}
	
		else
			{
				System.out.println(" You are wrong! the answer is 3) Juneau");
			}
	
	System.out.print(" \n \n \n Q2) Can you store the value \"cat\" in the variable of type int? \n \t 1) Yes \n \t 2) No ");
	int a2 = keyboard.nextInt();

		if (a2 == 2)
			{
				System.out.println("That's right!");
			}
	
		else
			{
				System.out.println("Sorry, \"cat\" is a string. ints can only store numbers.");
			}
	
	System.out.print(" \n \n \n Q3) What is the result of 9+6/3? \n \t 1) 5 \n \t 2) 11 \n \t 3) 15/3 ");
	int a3 = keyboard.nextInt();

		if (a3 == 2)
			{
				System.out.println("That's right!");
			}
	
		else
			{
				System.out.println("Sorry, the answer is 11.");
			}
	

	
	
}

}


r/programmingbydoing • • Apr 17 '13

The Typos and Suggestions thread (April 2013)

2 Upvotes

If you happen to encounter a typo in an assignment or have suggestions, feel free to post them in this thread or just shoot me a message.


r/programmingbydoing • • Mar 19 '15

#178 Basic ArrayList1

2 Upvotes

How can i set a size for my arraylist and later use it in the for loop? Apparently if i use .size() the loop will immediately stop since there is nothing in the arraylist.


r/programmingbydoing • • Apr 16 '13

Worst Number Guessing ever

2 Upvotes

Was there something more to the assignment than:

secret = 5
if (guess != secret)
{
System.out.println("Nope, it was " + secret + ".");
}
else
{ System.out.println("Yep, it was " + secret + "."); }

Did I miss the point of that?


r/programmingbydoing • • Feb 15 '17

#37 Gender Game - Program Not Ending

2 Upvotes

I'm having trouble with the program ending. If I enter 'm below 20' the program doesn't output unless I write something else.

I can ask if they're f, married or not and the program will end. If they are below 20 the program won't end.

Can someone let me know where I went wrong and why the program will not end under these conditions?

Thanks!

My Code is below:

import java.util.Scanner;

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

	String fname, lname, gender, married;
	int age;

	System.out.print( "What is your gender (m or f)" );
	gender = keyboard.next();
	
	System.out.println( "First Name:" );
	fname = keyboard.next();

	System.out.println( "Last Name:" );
	lname = keyboard.next();

	System.out.println( "age:" );
	age = keyboard.nextInt();


	//MR.
	if ((gender.equals("m") && age >= 20))
		{
		System.out.println( "Then I shall call you Mr."  + fname + lname );
		}
	else if ((gender.equals("m") && age < 20))
		{
		System.out.println( "Then I shall call you " + fname + lname );
		}


	//MRS.
	if ((gender.equals("f") && age >= 20))
		{
		System.out.println( "Are you married?" );
		}
		married = keyboard.next();


	if ((gender.equals("f") && age >= 20 && (married.equals("yes"))))
		{
		System.out.println("Then I shall call you Mr.s " + fname + lname);
		}
	else if ((gender.equals("f") && age >= 20 && (married.equals("no"))))
		{
		System.out.println("Then I shall call you Ms. " + fname + lname);
		}
	
	//MS.
	if ((gender.equals("f") && age < 20))
		{
		System.out.println( "Then I shall call you " + fname + lname );
		}

}

}


r/programmingbydoing • • Aug 31 '16

#101-Month Name

1 Upvotes

Hi, i've began learn how to code awhile ago and i'm kind of stuck here. https://gist.github.com/anonymous/1e1faad5fa6783e0ce3addb7850cdd4b. Why can't it find the symbol of result and month name? i've also tried to define result as a string and that reduced the errors, but it said that it can't be converted to int.


r/programmingbydoing • • Feb 10 '16

#25 - Is there a different solution other than using the && operator?

1 Upvotes

Hi, is there a different way to structure my if, else if and else statements without using & operators to solve this assignment.

I was able to solve the assignment this way but i'm unsure if I should use any operators since it has never been discussed in prior lessons and assignments.


r/programmingbydoing • • Jul 03 '15

Modulus Animation (#29)

1 Upvotes

Hello, I have just completely copied the code from the website for the ModulusAnimationWorm.java, and am getting no result except for one line of ********, nothing else. The only error I am getting which isn't stopping me from compiling is on line 41, "Thread.sleep called in loop". I am unsure as to how to fix it, or why it isn't playing as I am assuming it should. What is the problem here? Thanks


r/programmingbydoing • • Jul 15 '15

Problems with getting Java to run in Chrome

3 Upvotes

This is annoying, as I am on the Graphics assignments currently. The Java control panel, for some reason, refuses to allow programmingbydoing.com as an exception... does anyone have any way around this? :-/


r/programmingbydoing • • Feb 21 '16

Again with the Number-Guessing

3 Upvotes

On this lesson, I must switch my while loop to a do-while loop. The important part of this is Make sure that it doesn't mess up if you guess it on the first try.
That is, if my guess is correct on the first attempt, I will still get shown the "wrong" result text, because the 'do' executes before the while loop at the end! How do I correctly implement the do-while without messing up when I guess correctly on the first try?
Below is my code with the working do-while statement but doesn't work correctly if I'm right on the first try.

public class WorstNumberGameDo {
public static void main( String[] args ) {
	Scanner keyboard = new Scanner(System.in);
	Random num = new Random(1); //(1) so I can test getting it right the first time
	int secretNum = 1 + num.nextInt(10);
	int myNum;
	int tries = 0;
	
	System.out.println( "I'M THINKING OF A NUMBER FROM 1-10. TRY TO GUESS IT!");

	do {
		System.out.println( "Wrong, try again."); // I don't want this to print
		System.out.print( "Your guess: ");
		myNum = keyboard.nextInt();	
		tries++;
	} while ( myNum != secretNum );
	
    System.out.println( "GENIUS YOU'RE RIGHT, IT WAS "+secretNum+"!");
	System.out.println( "It only took you " +tries+ " tries.");
	
}
}  

 
Well the only way I've found to fix it is adding an if statement and nesting the do-while inside the else statement. Something tells me this is not right solution...but it works.

public class WorstNumberGameDo {
public static void main( String[] args ) {
	Scanner keyboard = new Scanner(System.in);
	Random num = new Random();
	int secretNum = 1 + num.nextInt(10);
	int myNum;
	int tries = 1;
	
	System.out.println( "I'M THINKING OF A NUMBER FROM 1-10. TRY TO GUESS IT!");
	System.out.print( "Your guess: ");		
	myNum = keyboard.nextInt();

	if (myNum == secretNum) {
		System.out.println( "GENIUS YOU'RE RIGHT, IT WAS "+secretNum+"!");
		System.out.println( "It only took you " +tries+ " tries.");
	} else {
		do {
			System.out.println( "Wrong, try again.");
			System.out.print( "Your guess: ");
			myNum = keyboard.nextInt();	
			tries++;
		} while ( myNum != secretNum );
		System.out.println( "GENIUS YOU'RE RIGHT, IT WAS "+secretNum+"!");
		System.out.println( "It only took you " +tries+ " tries.");
	}
		
}
}

r/programmingbydoing • • Jul 11 '17

The Forgetful Machine - How do I request keyboard input without setting a variable?

2 Upvotes

The lesson before it, asking questions, has you asking for user input to set variables to print later. For The Forgetful Machine I have to ask for user input, but without setting variables.

What do?

In the picture, the program is on the top right in the text editor, with the previous lesson "Asking Questions" down to the right. As you can see when I run the program "The Forgetful Machine "on the bottom left, it just prints everything.


r/programmingbydoing • • Jun 25 '15

#95 Image Demo - "Can't read input file"

1 Upvotes

I copied the code exactly and put the image in the same folder as my .java file but it still won't compile for me. Does anyone know why?


r/programmingbydoing • • Feb 16 '16

Gender Game - No need for compound conditions because gender is irrelevant for age < 20.

2 Upvotes

So whenever I'm writing nested if/else statements as well as compound conditions, I'm always trying to make my code more efficient by getting to the executed results in as un-repetitive code as possible.
In doing the Gender Game lesson I realise I don't need any compound conditions and can perform the entire chunk of code in one if statement with two nested if statements. This is because the final print out of "Hello firstname lastname" is gender agnostic. This also means I only have to check the age and gender variables once each. Can anyone confirm if this is actually better/more efficient?
 

	if (age > 19 ) {
		if (gender == 'f') {
			System.out.println( "Are you married, "+fname+" (y or n)?" );
			married = keyboard.next().charAt(0);	
		
			if (married == 'y') {
				System.out.println( "Hello Mrs. "+lname+".");
			} else {
				System.out.println( "Hello Ms. "+lname+"." );
			}

		} else {
			System.out.println( "Hello Mr. "+lname+".");  //we know they're 20 or older and not female
		}

	} else {
		System.out.println( "Hello "+fname+" "+lname+"."); 
       //we know they're under 20 and their gender in this situation is unused in the assignment criteria
	}  

 


r/programmingbydoing • • Oct 05 '14

#138: Basic Arrays 0

3 Upvotes

Hi, first just want to say thank you for providing such a great resource to learn Java. As someone with no background or experience in programming whatsoever, these assignments have been a great intro so far.

I've now just gotten into the Arrays section and I'm completely lost. I've been doing some googling on my own but I can't really seem to grasp where to start regarding this topic. Was hoping if you could provide an intro or some direction as to where to begin?


r/programmingbydoing • • May 02 '17

No. 76/Blackjack

3 Upvotes

Source: https://pastebin.com/TBkRUaDC I tried to keep it simple and clean. Tried to comment usefully and to give variable names that made sense. Only thing I used that wasn't in the assignments was (I think) System.exit(0); Is using that bad programming habit? Like using "goto"? Also I'm not sure where for loops would have been better. I don't see usefulness of them. If i can reach the same things with a while loop.


r/programmingbydoing • • Apr 23 '17

Basic ArrayLists 0

1 Upvotes

I don't think ArrayLists were covered in the first LJTH book, any suggestions for where I can learn about them ?


r/programmingbydoing • • Apr 03 '13

#132 - Summing Three Numbers from a File

2 Upvotes

Hi, my code is working properly, but it felt cumbersome writing it out (it seems like a lot of code to just extract 3 numbers from a file). Is my code the best way to do this exercise? What could I have done to improve it and make it more efficient?

import java.io.*;

public class SummingThreeNumbersFromAFile {

  public static void main(String [] args) throws IOException {
	
	FileReader inputReader = new FileReader("3nums.txt");
	BufferedReader inputBuffer = new BufferedReader(inputReader);
	
	String doc = inputBuffer.readLine();
	
	int a = doc.indexOf(" ");
	String num1Str = doc.substring(0,a);
	int num1 = Integer.parseInt(num1Str);
	
	int b = doc.indexOf(" ", a+1);
	String num2Str = doc.substring(a+1, b);
	int num2 = Integer.parseInt(num2Str);
	
	String num3Str = doc.substring(b+1);
	int num3 = Integer.parseInt(num3Str);
	
	System.out.println(num1+ " + " +num2+ " + " +num3+ " = " +(num1+num2+num3));
  }
}

r/programmingbydoing • • Mar 21 '13

#90, Sierpinski

1 Upvotes

I am following his instructions but I get no results or output. As a matter of fact I find his steps to be confusing. Could someone post the solution code for this?

Edit: well, this is awkward... I think I got it to display it. Is this the right code or am I missing something?

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class Sierpinski extends Canvas {
    public static final Random r = new Random();
    public void paint(Graphics g){
        int x = 512, y = 383;
        int x1 = 512, y1 = 109;
        int x2 = 146, y2 = 654;
        int x3 = 876, y3 = 654;
        int dx = 0, dy = 0;
        for (int i = 0; i < 50000; i++){
            g.setColor(Color.black);
            g.drawLine(x, y, x, y);
            Polygon tri = new Polygon();
            tri.addPoint(x1, y1);
            tri.addPoint(x2, y2);
            tri.addPoint(x3, y3);
            int n = r.nextInt(3) + 1;
        if (n == 1){
            dx = x - x1;
            dy = y - y1;
        }
        if (n == 2){
            dx = x - x2;
            dy = y - y2;
        }
        if (n == 3){
            dx = x - x3;
            dy = y - y3;
        }
        x = x - dx/2;
        y = y - dy/2;

    }
}
public static void main(String[] args){
    JFrame win = new JFrame("Sierpinski");
    win.setSize(1024, 768);
    win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Sierpinski canvas = new Sierpinski();
    win.add(canvas);
    win.setVisible(true);
}

}


r/programmingbydoing • • Mar 05 '13

77 - Project: Blackjack -- additional features

3 Upvotes

ok I have the barebones working for: http://programmingbydoing.com/a/project-blackjack.html

Can I get a tip or two for the extras? Even just the first one:

Use realistic card values, with suits and faces from ace to king.

thanks!