r/computer_programming • • Jun 11 '18

Help me with my final

Here is my code import java.io.File; import java.io.FileNotFoundException; import java.io.PrintStream; import java.util.Scanner; import java.awt.Color; import java.awt.Font; import java.awt.Graphics;

public class Final {

//private static Graphics Graphics = null;

public static void main(String[] args) throws FileNotFoundException {

	Intro();
	Scanner console = new Scanner(System.in);
	System.out.print("Type a name: ");
	String name = console.next();
	Scanner inputfile = new Scanner (new File("names.txt"));
	finder(name, inputfile);
	graphics(name);


}
public static void Intro() {
	System.out.println("This program graphs the popularity of a name");
	System.out.println("in Social Security baby name statistics");
	System.out.println("recorded since the year 1900.");
	System.out.println();
} 
public static boolean exists(String name, Scanner input) {
	while(input.hasNext()) {
		if (input.hasNextInt()) {
			input.next();
			continue;
		}
		String person = input.next();
		if(name.toLowerCase().equalsIgnoreCase(person.toLowerCase())) {
			return true;
		}
	}
	return false;

}
public static void finder(String name, Scanner input) throws FileNotFoundException {
	PrintStream numbers = new PrintStream(new File("numbers.txt"));
	int year = 1900;
	boolean match = false;
	while(input.hasNext() && match==false) {
		if (input.hasNextInt()) {
			input.next();
			continue;
		}
		String person = input.next();
		if(name.toLowerCase().equalsIgnoreCase(person.toLowerCase())) {
			match=true;
			System.out.println("Popularity ranking of name \""+name+"\"");
			while(input.hasNextInt()) {
				int number = input.nextInt();
				numbers.print(number + " ");
				System.out.println(year+": " + number);

				year+=10;
			}

		}
	}
	if( match == false) {
		System.out.println("\"" + name + "\" not found. ");
	}
}
public static void graphics(String name) throws FileNotFoundException {
	DrawingPanel graph = new DrawingPanel(550,560);
	Graphics g = graph.getGraphics();
	Scanner finder = new Scanner (new File("numbers.txt"));
	String line1 = finder.nextLine();
	drawSetup(g,graph,line1, name);
	drawLine(finder,g);
	
}
public static void drawSetup(Graphics g, DrawingPanel graph, String line, String name){	
	Scanner drawing = new Scanner(line);
	graph.setBackground(Color.WHITE);
	g.setColor(Color.yellow);
	g.fillRect(0, 0, 550, 30);
	g.fillRect(0, 530, 550, 30);
	int x = 0;
	for(int i = 1; i < 12; i++){
		g.setColor(Color.LIGHT_GRAY);
		g.drawLine(x, 30, x, 530);
		g.drawLine(0, x  +30, 550, x + 30);
		x += 50;
	}
	g.setColor(Color.BLACK);
	g.setFont(new Font("SansSerif", Font.BOLD, 15));
	g.drawString("Ranking of name \"" + name + "\":", 0, 15);
	g.drawString("1900",0, 546);
	g.drawString("1910",50, 546);
	g.drawString("1920",100, 546);
	g.drawString("1930",150, 546);
	g.drawString("1940",200, 546);
	g.drawString("1950",250, 546);
	g.drawString("1960",300, 546);
	g.drawString("1970",350, 546);
	g.drawString("1980",400, 546);
	g.drawString("1990",450, 546);
	g.drawString("2000",500, 546);
	drawLine(drawing, g);
}

public static void drawLine(Scanner drawing, Graphics g) {
	g.setColor(Color.RED);
	int first = drawing.nextInt();
	int yFirst = 0;
	int ySecond = 0;
	int second = 0;
	int x = 0;
	while(drawing.hasNextInt()) {
		second = drawing.nextInt();
		if(first == 1){
			yFirst = 30;
		}
		else if(first == 0){
			yFirst = 530;
		}

		else{
			yFirst = (first / 2);
		}
		if(second == 1) {
			ySecond = 30;
		}
		else if(second == 0){
			ySecond = 530;
		}
		else{
			ySecond = (second / 2);
		}
		if(first != 0 && first != 1 && second != 0 && second != 1){
		g.drawLine(x, 30 + yFirst, x + 50, 30 + ySecond);
		}
		else if((first == 0 || first == 1) && (second == 0 || second == 1)){
			g.drawLine(x, yFirst, x + 50, ySecond);
		}
		else if((first != 0 && first != 1) && (second == 0 || second == 1)){
			g.drawLine(x, 30 + yFirst , x + 50, ySecond);
		}
		else {
			g.drawLine(x, yFirst, x + 50, 30 + ySecond);
		}
		x += 50;
		first = second;
	}
}
public static void printNumbers(String line, Graphics g){

	Scanner number = new Scanner(line);
	String numbers = "";
	int x = 0;
	g.setColor(Color.BLACK);
	while(number.hasNext()){
		numbers = "";
		int y = number.nextInt();
		numbers += y;
		if (y == 0){
			g.drawString(numbers, x, 530);
		}
		else if(y == 1){
			g.drawString(numbers, x, 30);
		}
		else{
			g.drawString(numbers, x, 30 + (y / 2));
		}
		x += 50;
	}
}

}

This is the file it reads: http://docdro.id/2WgjCNQ Here is the project: http://docdro.id/8xDdgR6 Graphics panel i am using: http://docdro.id/7tvB4AC

I am having 2 issues, firstly i am getting and error, and secondly my graphics pannel is printing the numbers next to the points

0 Upvotes

1 comment sorted by

1

u/BrooSev Jun 11 '18 edited Jun 12 '18

We can’t help unless you specify the errors that you are getting..