12 Replies - 216 Views - Last Post: 12 September 2012 - 03:34 PM Rate Topic: -----

#1 Animal7296  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-September 12

output problem

Posted 11 September 2012 - 07:55 PM

I have gotten my code almost done but the output is not what I am looking for. What this is supposed to do is output a chart of number, square, and cube.

Here is what I have so far. Any help would be very much appreciated.

//Exponent Chart Assignment
//Program to display Number-Square-Cubed chart
import java.util.Scanner; //Program uses scanner class


	public class SquareCubeChart
{
	public static void main(String[] args)
{
	Scanner input = new Scanner( System.in );
	
	int lownumber; //low number to start conversion
	int highnumber; //high number to end conversion
	
	System.out.print( "Enter low number to convert!" ); //User input for low number
	lownumber = input.nextInt(); // Recieve first number from user
	
	System.out.print( "Enter high number to convert!" );
	highnumber = input.nextInt(); // Recieve second number from user
	
	     for ( int number = 0; number <= 10; number++ );			
				System.out.println(lownumber);
				{
		  for ( int Square = 0; Square <= 10; Square++ );             
				System.out.print(lownumber*lownumber+"\t");
	  
		  for ( int Cube = 0; Cube <= 10; Cube++ );
		    System.out.print( lownumber*lownumber*lownumber+"\t" );
			 }
			 }
			 }
			 


Is This A Good Question/Topic? 0
  • +

Replies To: output problem

#2 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: output problem

Posted 11 September 2012 - 08:50 PM

Can you show us an example of the exact output you're expecting? Also, what errors are you getting from your code?
Was This Post Helpful? 0
  • +
  • -

#3 WannaJava?  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 27
  • Joined: 11-September 12

Re: output problem

Posted 12 September 2012 - 07:31 AM

Currently, if I take your program and run it, it prints this

Enter low number to convert!3
Enter high number to convert!7
3
9 27

What do you want your output to look like?

I would suggest getting rid of the ! points and putting in a : to make it look nicer.
The \t is for a tab, so that is where your 9 "tab" 27 is coming from.
Also, I would add some kind of print statement with the output to say you entered "3", "3 squared is 9", "3 cubed is 27"

If you can give us an idea of what you want it to look like, we can gladly help you.
Was This Post Helpful? 0
  • +
  • -

#4 Animal7296  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-September 12

Re: output problem

Posted 12 September 2012 - 10:52 AM

I want it to look like this:



Number Square Cube
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64

and so on.

I am not getting any errors at this point.
Was This Post Helpful? 0
  • +
  • -

#5 Animal7296  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-September 12

Re: output problem

Posted 12 September 2012 - 11:12 AM

Ok that didnt work, let me try this again




               number     square      cube
                  0          0          0
                  1          1          1
                  2          4          8
                  3          9         27
                  4         16         64



Was This Post Helpful? 0
  • +
  • -

#6 WannaJava?  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 27
  • Joined: 11-September 12

Re: output problem

Posted 12 September 2012 - 11:29 AM

In that case, you will have to do a for loop to print out all the number leading up to the number that was input. You can format it to print out like that by overriding your toString() method. So within the for loop, you need to print out i

something like
for (int i = 0; i < lowNumber; i++)
{
     System.out.println(i);
}
 


That is not all of it of course, you need a toString override, but that is how to get what you want I believe
Was This Post Helpful? 0
  • +
  • -

#7 WannaJava?  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 27
  • Joined: 11-September 12

Re: output problem

Posted 12 September 2012 - 11:41 AM

There is a way to do it without the toString() override, but that is best practice. You could honestly turn this code into 6 - 10 lines total. I don't want to give you the exact code, but if you would like to see it I can show you.

I guess one thing is...
are you just covering the range they enter so if
lownumber = 2
and
highnumber = 5
you only want to print out 2-5 squared and cubed?
Was This Post Helpful? 0
  • +
  • -

#8 WannaJava?  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 27
  • Joined: 11-September 12

Re: output problem

Posted 12 September 2012 - 11:47 AM

Enter low number to convert: 6
Enter high number to convert: 8
number squared cubed
6 36 216
7 49 343
8 64 512

I took your program and was able to do this with it. If that is what you are looking for. If you wanted the numbers centered, you would need to do a toString() override. Also it will be more efficient to turn it into one for loop that will do everything.
Was This Post Helpful? 0
  • +
  • -

#9 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: output problem

Posted 12 September 2012 - 11:52 AM

Add this before your very first for statement
System.out.println("Number\tSquare\tCube");



and also, change these lines:

for ( int number = 0; number <= 10; number++ ); 
      System.out.println(lownumber);



to this
 for ( int number = 0; number <= 10; number++ );			
				System.out.print(lownumber+"\t");


What I did is i changed your println to print, so your outputs will be printed in the same line. I also added "\t" in the print statement.
Was This Post Helpful? -1
  • +
  • -

#10 WannaJava?  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 27
  • Joined: 11-September 12

Re: output problem

Posted 12 September 2012 - 12:04 PM

View PostHopelessDev, on 12 September 2012 - 11:52 AM, said:

Add this before your very first for statement
System.out.println("Number\tSquare\tCube");



and also, change these lines:

for ( int number = 0; number <= 10; number++ ); 
      System.out.println(lownumber);



to this
 for ( int number = 0; number <= 10; number++ );			
				System.out.print(lownumber+"\t");


What I did is i changed your println to print, so your outputs will be printed in the same line. I also added "\t" in the print statement.



It would be easier to just do this
 System.out.print("number\t\tsquared\t\tcubed\n");
	    
	    for (int i = lownumber; i <= highnumber; i++)
	    {
	    	System.out.print(i+"\t\t");
	    	System.out.print(i*i+"\t\t");
	    	System.out.print(i*i*i+"\n");
	    }
    }



that is the code I produced that is 3 times more efficient
Was This Post Helpful? 0
  • +
  • -

#11 Animal7296  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-September 12

Re: output problem

Posted 12 September 2012 - 12:09 PM

View PostWannaJava?, on 12 September 2012 - 11:41 AM, said:

There is a way to do it without the toString() override, but that is best practice. You could honestly turn this code into 6 - 10 lines total. I don't want to give you the exact code, but if you would like to see it I can show you.

I guess one thing is...
are you just covering the range they enter so if
lownumber = 2
and
highnumber = 5
you only want to print out 2-5 squared and cubed?



Yes that is exactly what I want to do. I want the user to decide what numbers they want to use and do the output for them.
Was This Post Helpful? 0
  • +
  • -

#12 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: output problem

Posted 12 September 2012 - 12:14 PM

View PostWannaJava?, on 12 September 2012 - 12:04 PM, said:

View PostHopelessDev, on 12 September 2012 - 11:52 AM, said:

Add this before your very first for statement
System.out.println("Number\tSquare\tCube");



and also, change these lines:

for ( int number = 0; number <= 10; number++ ); 
      System.out.println(lownumber);



to this
 for ( int number = 0; number <= 10; number++ );			
				System.out.print(lownumber+"\t");


What I did is i changed your println to print, so your outputs will be printed in the same line. I also added "\t" in the print statement.



It would be easier to just do this
 System.out.print("number\t\tsquared\t\tcubed\n");
	    
	    for (int i = lownumber; i <= highnumber; i++)
	    {
	    	System.out.print(i+"\t\t");
	    	System.out.print(i*i+"\t\t");
	    	System.out.print(i*i*i+"\n");
	    }
    }



that is the code I produced that is 3 times more efficient

Ofcourse. I just thought of just showing him how to achieve the output format and let him handle the problem algorithm.
Was This Post Helpful? 1
  • +
  • -

#13 Animal7296  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-September 12

Re: output problem

Posted 12 September 2012 - 03:34 PM

Okay I think that I have it. It seems to be working right now and it even goes to numbers higher than I was expecting. I did use the code that WannaJava? provided however I got it to work just using hints from it first. Then I used his code to clean it up. Another thing that I did was not to copy and paste. I went through and attempted to learn a few things from it and then typed it out so that I could maybe learn something from it.

Thank all of you for your responses and help. It is greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1