The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
February 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

The problem is in your loop structures

Posted by Hiran on February 15, 2002 at 11:01 AM

You should create your column headers, and then have a loop for the rows that places the X. Here is some sample code:


import java.io.*;

public class Graph {

public static void main(String[] args) throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

private int x;
private int y;

do {
System.out.println ("Enter X (1-10, or -1 to quit):");
x = Integer.parseInt (stdin.readLine());
System.out.println ("Enter Y (1-10):");
y = Integer.parseInt (stdin.readLine());

//Setting up the column headers
if (x != -1){
System.out.println (" 1234567890");
System.out.print ('\n');

//Setting up the rows
for (int rows=1; rows < 11; rows++) {
System.out.print(rows);
//At the correct row
if (y == rows) {
//Check the column for the placement of X
for (int columns=1; columns < 11; columns++) {
//At the correct column
if (x == column) {
//Place the X
System.out.print("X");
} else {
System.out.print(" ");
}
}
System.out.print('\n');
}
}
} while (x != -1);
}
}


Some notes: As you can see, I made the variables private. I would suggest you make all you instance variables private or protected unless you need them to be public (or unless it's a constant). This is just so that others can't access your variables and change them. Secondly, I would break the code up into one or two methods (instead of all in the main method). Thirdly, I would catch the IOException, instead of throwing it. The only reason for this is so that if something does go wrong for any reason, the user won't have to wade through the non-user friendly verbose that the JVM will spew out if it encounters an IOException. By catching the IOException, you can do with it what you want, including outputing a user-friendly message. If you're not sure about how to catch an exception, you use the try-catch structure. The format in your case would be the following:

try {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
} catch (IOException e) {
//Here you would do what you want with the exception.
//The following outputs the technical verbose about the exception
System.out.println(e.toString());
//Or you can output your own user-friendly message
System.out.println("Unable to access the keyboard for input!");
}

Try structures also have a finally part to them, but I'm not too sure about how that part works. Fourthly (and lastly), use the HTML tags <pre>...</pre> or <code>...</code> to display formatted text (ie, your code with all the tabs, or the output that you want your program to display). Anyway, hope this helps. If you're not sure of my code and what I did, let me know, and I'll explain it.
Hiran

> Hi. I'm supposed to use nested for loops to place the input integer x in the correponding column #, and at the same time have the x print in the corresponding row of the input integer y.
> So x = 5, y = 3 would look like:
> 1234567890
>
> 1
> 2
> 3 x
> 4
> 5
> 6
> 7
> 8
> 9
> 0
> Here's my miserable attempt. What am I missing this time? You know this textbook really stinks.

>
> import java.io.*;

> public class Graph {
>
> public static void main(String[] args) throws IOException {
>
> BufferedReader stdin = new BufferedReader
> (new InputStreamReader(System.in));
>
> int x;
> int y;
>
> do {
> System.out.println ("Enter X (1-10, or -1 to quit):");
> x = Integer.parseInt (stdin.readLine());
> System.out.println ("Enter Y (1-10):");
> y = Integer.parseInt (stdin.readLine());
>
> if (x != -1){
> System.out.println ("\t1234567890");
> System.out.print ('\t');
>
> for (int column = 1; column <= 10; column++)
> if (x == column)
> System.out.println ('X');
> else
> System.out.print (' ');

> for (int row = 1; row <= 10; row++)
> if (y == row)
> System.out.println ("\n1:\n2:\n3:\n4:\n5:\n6:\n7:\n8:\n9:\n0:\n");
>
>
> System.out.println();
>
> }
>
> } while (x != -1);
>
> }
> }






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us