| View previous topic :: View next topic |
| Author |
Message |
zaczac Master Cheater
Reputation: 0
Joined: 01 May 2007 Posts: 266 Location: Ontario, Canada
|
Posted: Tue Apr 13, 2010 8:01 pm Post subject: Java Noob Help |
|
|
I need to get input from the keyboard ( Three random numbers seperated by one or more spaces ) and store those into a string, then i need to break that string into 3 number variables and check if they are all valid numbers.
I can't use the scanner class because we use an older version of java at the school and we were told to use bufferedReader..
I can't seem to find a way to do this without the scanner class so any help is appreciated!. |
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Tue Apr 13, 2010 8:25 pm Post subject: |
|
|
| Slugsnack wrote: | | read the whole string, then parse it with Integer.parseInt(). catch NumberFormatException in a try()/except(). you can use the StringTokenizer class to split the string |
|
|
| Back to top |
|
 |
zaczac Master Cheater
Reputation: 0
Joined: 01 May 2007 Posts: 266 Location: Ontario, Canada
|
Posted: Tue Apr 13, 2010 9:34 pm Post subject: |
|
|
I think i finally got it finished but for some reason I cant use Java 1.4.2...
Everytime i click "run" it says execution finished but nothing will show up,
Problem goes away with 1.3.1 but then Im limited to what I can use..
any idea? |
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Wed Apr 14, 2010 6:51 am Post subject: |
|
|
| yes, post your code and i'll tell you your problem |
|
| Back to top |
|
 |
zaczac Master Cheater
Reputation: 0
Joined: 01 May 2007 Posts: 266 Location: Ontario, Canada
|
Posted: Wed Apr 14, 2010 8:57 am Post subject: |
|
|
I am using my older computer now and it works fine with 1.4.2 but still havent finished the program heres what I got so far >_>.
| Code: | // The "Sequences" class.
import java.io.*;
public class Sequences
{
public static void main (String[] args)
throws java.io.IOException
{
// Place your code here
int t1 = 1, t2 = 2, t3 = 3;
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
System.out.println ("Enter 3 Numbers with a space between each number.");
String input = br.readLine ();
String[] inputTokens = input.split (" ");
if (t2 - t1 == t3 - t2)
{
System.out.println ("The sequence is linear. The next three terms are " + (t2 - t1 + t3) + (1 + t2 - t1 + t3) + (2 + t2 - t1 + t3));
}
else
{
System.out.println ("The sequence is not linear.");
}
if (t1 + t2 == t3)
{
System.out.println ("The sequence is fibonacci. The next three terms are " + (t2 + t3) + (t2 + 2 * t3) + (2 * t2 + 3 * t3));
}
else
{
System.out.println ("The sequence is not fibonacci.");
}
if (t2 % t1 == t3 % t2)
{
System.out.println ("The sequence is exponential. The next three terms are " + (t2 % t1 * t3) + (t2 % t1 ^ 2 * t3) + (t2 % t1 ^ 3 * t3));
}
else
{
System.out.println ("The sequence is not exponential.");
}
} // main method
} // Sequences class |
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Wed Apr 14, 2010 10:10 am Post subject: |
|
|
| Code: | import java.io.*;
public class Sequences {
public static void main( String[] args ) throws java.io.IOException {
System.out.println("Enter 3 Numbers with a space between each number.");
BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
String input = br.readLine();
String[] inputTokens = input.split (" ");
int[] inputNums = new int[3];
final int numNewTerms = 3;
if( inputTokens.length == 3 ) {
for( int i = 0; i < 3; i++ ) {
try {
Integer.parseInt( inputTokens[i] );
} catch( NumberFormatException except ) {
System.err.println("One of the parameters is not a number");
System.exit( 1 );
}
inputNums[i] = Integer.parseInt( inputTokens[i] );
}
int linearDiff = inputNums[1] - inputNums[0];
float scaleDiff = ( float )inputNums[1] / ( float )inputNums[0]; // avoid approximations from integer division
if( inputNums[2] - inputNums[1] == linearDiff ) {
System.out.print("The sequence is linear. The next three terms are ");
for( int i = 1; i <= numNewTerms; i++ )
System.out.print( inputNums[2] + i*( linearDiff ) + ( ( i == numNewTerms ) ? ".\n" : ", " ) );
}
else
System.out.println("The sequence is not linear.");
if( inputNums[0] + inputNums[1] == inputNums[2] ) { // very very bad way for testing for fib sequence..
System.out.print("The sequence is fibonacci. The next three terms are ");
int lastTerm = inputNums[1];
int currentTerm = inputNums[2];
int temp;
for( int i = 0; i < numNewTerms; i++ ) {
temp = currentTerm;
currentTerm += lastTerm;
lastTerm = temp;
System.out.print( currentTerm + ( ( i == numNewTerms - 1 ) ? ".\n" : ", " ) );
}
}
else
System.out.println("The sequence is not fibonacci.");
if( ( float )inputNums[2] / ( float )inputNums[1] == scaleDiff ) {
System.out.print("The sequence is exponential. The next three terms are ");
int currentTerm = inputNums[2];
for( int i = 0; i < numNewTerms; i++ ) {
currentTerm *= scaleDiff;
System.out.print( currentTerm + ( ( i == numNewTerms - 1 ) ? ".\n" : ", " ) );
}
}
else
System.out.println("The sequence is not exponential.");
}
else {
System.err.println("3 numbers were not entered");
System.exit( 1 );
}
}
} |
save as Sequences.java. compile with :
| Code: | | javac Sequences.java |
run with :
just gonna assume here that you haven't been taught OOP and wanted 'C style' code |
|
| Back to top |
|
 |
zaczac Master Cheater
Reputation: 0
Joined: 01 May 2007 Posts: 266 Location: Ontario, Canada
|
Posted: Wed Apr 14, 2010 2:04 pm Post subject: |
|
|
what the fuck LOL, I just did this assignment in class, rushed and it looked nothing like this.. obv this is way better but thanks for that Ill try and fix mine up tomorrow if the teacher lets me and before we got this supply our real teacher just started teaching OOP. So I dont know much about it.. |
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Wed Apr 14, 2010 2:57 pm Post subject: |
|
|
| zaczac wrote: | what the fuck LOL, I just did this assignment in class, rushed and it looked nothing like this.. obv this is way better but thanks for that Ill try and fix mine up tomorrow if the teacher lets me and before we got this supply our real teacher just started teaching OOP. So I dont know much about it.. |
well what did yours look like |
|
| Back to top |
|
 |
zaczac Master Cheater
Reputation: 0
Joined: 01 May 2007 Posts: 266 Location: Ontario, Canada
|
Posted: Thu Apr 15, 2010 9:12 am Post subject: |
|
|
| Its not 100% done but it runs, ill post it this afternoon. |
|
| Back to top |
|
 |
|