Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Breaking a string into 3 variables.
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
zaczac




PostPosted: Tue Apr 13, 2010 7:47 pm   Post subject: Breaking a string into 3 variables.

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!.
Sponsor
Sponsor
Sponsor
sponsor
TerranceN




PostPosted: Tue Apr 13, 2010 8:21 pm   Post subject: Re: Breaking a string into 3 variables.

I'm not entirely sure what you are asking as I have never used the scanner class, I'm assuming it is getting input using System.in and splitting strings up with spaces. To get console input, you need to use a BufferedReader to read from an InputStreamReader that reads from System.in, then use the readLine method to get a line of input. To break a string where there are spaces, use the split method of the string class. This will use whatever string you pass to it, in this case a space, to break up the string (the one you called it from) into an array of strings and remove all the spaces (or whatever string you pass to it). Here is an example:

Java:
import java.io.*;

public class ConsoleInput
{
    public static void main (String[] args) throws Exception
    {
        System.out.println("Type in a sentence");
   
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
       
        String input = in.readLine();
        String[] inputTokens = input.split(" ");
       
        System.out.println("There were " + inputTokens.length + " token(s):");
       
        for (int i = 0; i < inputTokens.length; i++)
        {
            System.out.println(inputTokens[i]);
        }
    }
}


Hope that helps.
zaczac




PostPosted: Tue Apr 13, 2010 9:40 pm   Post subject: Re: Breaking a string into 3 variables.

Thanks!!! Thats almost everything i needed, But i need to take the user input and put each of the numbers entered into their own int, so from that one string of numbers (the three numbers the user puts in) I take the values and put them into their own int value.. sorry if that doesent make any sense
TheGuardian001




PostPosted: Tue Apr 13, 2010 10:40 pm   Post subject: Re: Breaking a string into 3 variables.

Well, once you've broken each of the numbers up into their own strings, you can use

code:

Integer.valueOf(String s);


to convert the Strings into ints.

If you care about error checking, Integer.valueOf can throw a NumberFormatException, if the string given is not a valid integer.
zaczac




PostPosted: Tue Apr 13, 2010 10:47 pm   Post subject: Re: Breaking a string into 3 variables.

Still a bit confused on how to "split" the single string into multiple ints, Here the program so far. What the program actually needs to do is.
Write Classes and methods to:
Accept a sequence of three numbers from a user in one string
Break the string into 3 number variables (if possible)
verify that only three valid numbers were entered;
determine weather the sequence is linear, exponential, fibonacci or none of these sequence types; display all six numbers.
Thanks for the help so far im getting more and more ideas on how to get this to work Very Happy


code:
// The "Sequences" class.
import java.io.*;
public class Sequences
{
    public static void main (String[] args)
        throws java.io.IOException
    {
        // Place your code here
        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
TerranceN




PostPosted: Wed Apr 14, 2010 3:26 pm   Post subject: Re: Breaking a string into 3 variables.

Ok, from reading this thread you already can split a single string into multiple strings, and you can change a single string into a singe int. The great thing about arrays is that each item in it can be treated as if it was its own variable.

Java:
String s = "1";
String[] a = {"1", "2"};

//  using this
int number = Integer.valueOf(s);

// is just as valid as using this
int anotherNumber = Integer.valueOf(a[0]);


Because of this, we can use Integer.valueOf on specefic elements in the array of strings (inputTokens) to get integers.
OmniC-Programmer




PostPosted: Wed Apr 14, 2010 6:42 pm   Post subject: Re: Breaking a string into 3 variables.

If I were given this problem,I would first make a for loop that starts at 0 and ends at 2 *3 numbers*. I would then make a second for loop starts at the start of the string, and ends at the end of the string. Next use the functions .equalsTo (String val.substring (int start, int end)) in an if condition and check if the substring is equal to " ". Then if it is equal create another for loop that starts at the 'start' value of the substring + 1 and ends at the end of the string. Next use the functions .equalsTo (String val.substring (int start, int end)) again in an if condition and check if the substring is equal to " ". Then if it is equal exit this for loop. If it isnt then make your array of 3 strings of index of the first loop equal it's self and the substring of the current for loop. I believe that is all you must do to get the 3 values, but who knows maybe someone else believes I am wrong?

Doing this you will find where all the values are, and skip the spaces. I'll leave it up to you to write the class Since I dont want to help cheat for you.
zaczac




PostPosted: Thu Apr 15, 2010 9:24 am   Post subject: Re: Breaking a string into 3 variables.

Alright i Finally finished it Thanks for every ones help! Smile
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: