Jump to content

Java Help

Go to solution Solved by Ghost,

There we go, all fixed. :)

I am doing an assignment for AP Computer Science and I can't figure out what I did wrong.

 

Here are the instructions: "Write an application that creates and prints a random phone number of the form XXX-XXX-XXXX. Include the dashes in the output. Do not let the first three digits contain an 8 or 9 (but don't be more restrictive than that), and make sure that the second set of three digits is not greater than 742."

 

When I run the code, I sometimes get an empty response. What should I do to my code to fix this problem? Thanks!

import java.io.*;import java.lang.Math;class randomphonenumber{     public static void main (String str[]) throws IOException {int a=8;int b=8;int c=8;while (a>7){a =(int)(Math.random()*10);}while (b>7){b =(int)(Math.random()*10);}while (c>7){c =(int)(Math.random()*10);}int d =(int)(Math.random()*10);int e=(int)(Math.random()*10);int f=(int)(Math.random()*10);int g=(int)(Math.random()*10);int h=(int)(Math.random()*10);int i=(int)(Math.random()*10);int j=(int)(Math.random()*10);if (d>7){d =(int)(Math.random()*10);}else if(d==7){  if(e>4){e=(int)(Math.random()*10);}  else if(e==4){if(f>2){f=(int)(Math.random()*10);}}  else{System.out.println(""+d+e+f);}}       else{System.out.println(""+a+b+c+"-"+d+e+f+"-"+g+h+i+j);}}     }
Link to comment
https://linustechtips.com/topic/288375-java-help/
Share on other sites

Link to post
Share on other sites

File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 9]
Error: Cannot make a static reference to the non-static field number
File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 10]
Error: Cannot make a static reference to the non-static field number
File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 11]
Error: Cannot make a static reference to the non-static field a
File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 12]
Error: Cannot make a static reference to the non-static field a
File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 12]
Error: Cannot make a static reference to the non-static field number
File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 12]
Error: Cannot make a static reference to the non-static field a
File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 14]
Error: Cannot make a static reference to the non-static field number
File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 14]
Error: The method parseInt(java.lang.String) in the type java.lang.Integer is not applicable for the arguments (int)
File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 15]
Error: Cannot make a static reference to the non-static field a
File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 16]
Error: Cannot make a static reference to the non-static field a
File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 16]
Error: Cannot make a static reference to the non-static field a
File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 17]
Error: Cannot make a static reference to the non-static field number
File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 17]
Error: Cannot make a static reference to the non-static field a
File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 18]
Error: Cannot make a static reference to the non-static field a
Link to comment
https://linustechtips.com/topic/288375-java-help/#findComment-3915974
Share on other sites

Link to post
Share on other sites

 

-snip-

 

 

Fixed, forgot I was running stuff from main.

 

Wait a sec, I have other errors in there for some reason.

Feel free to PM for any water-cooling questions. Check out my profile for more ways to contact me.

 

Add me to your circles on Google+ here or you can follow me on twitter @deadfire19.

Link to comment
https://linustechtips.com/topic/288375-java-help/#findComment-3915986
Share on other sites

Link to post
Share on other sites

Fixed, forgot I was running stuff from main.

 

Wait a sec, I have other errors in there for some reason.

Lol:

4 errors found:
File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 8]
Error: length cannot be resolved or is not a field
File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 10]
Error: The method parseInt(java.lang.String) in the type java.lang.Integer is not applicable for the arguments (int)
File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 12]
Error: The method parseInt(java.lang.String) in the type java.lang.Integer is not applicable for the arguments (int)
File: C:\Users\Vishnu Reddy\Documents\Dr. Java\randomphonenumber.java  [line: 15]
Error: The method parseInt(java.lang.String) in the type java.lang.Integer is not applicable for the arguments (int)
Link to comment
https://linustechtips.com/topic/288375-java-help/#findComment-3916009
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×