Computer Science Canada

splitting up input

Author:  krishon [ Thu Sep 25, 2003 6:10 pm ]
Post subject:  splitting up input

i need help with a particular exercise. In the question, i need to read in a users name with a space separating the first and last name (which i can do). However, i'm to split up the first and last name and store them into separate variables. That's the part i have no clue how to do.......ne help will be appreciated

Author:  Tony [ Thu Sep 25, 2003 7:08 pm ]
Post subject: 

cant you just use substring?

Edit: new code

code:

import java.io.*;

public class substrings {

        public static void main(String[] args) {
                               
                InputStreamReader istream = new InputStreamReader(System.in);
                BufferedReader bufRead = new BufferedReader(istream);
               
                String text;
                String name1;
                String name2;
               
                System.out.println("enter your full name");
                try
                {
                text = bufRead.readLine();
                name1 = text.substring(0,text.indexOf(" "));
                name2 = text.substring(text.indexOf(" ")+1);
               
                System.out.println(name1);
                System.out.println(name2);
                }
                catch(Exception e)
                {
                        System.out.println("error occured");
                }
       
        }
}

Author:  krishon [ Thu Sep 25, 2003 7:21 pm ]
Post subject: 

ye that's wut i thought tooo, i know to use the substring, i jsut dun't know how to see where the space is....ur idea wuz wut i thought at the start...but the only problem like i said wuz to detect the space

Author:  rizzix [ Thu Sep 25, 2003 7:25 pm ]
Post subject: 

so problem solved right. i mean tony solved it for you. he detected the space and split the string.

Author:  krishon [ Thu Sep 25, 2003 7:42 pm ]
Post subject: 

yah...he did thx tony Very Happy

Author:  rizzix [ Thu Sep 25, 2003 7:53 pm ]
Post subject: 

here's an alternate way (lesser code) [wow you've got to love this language]:

code:

String input = "John Doe"; // lets suppose this is the input you get
String[] names = input.split(" ");

System.out.println(names[0]);  // First name
System.out.println(names[1]);  // Last name




works only with 1.4.x^ ( i haven't tested it )

Author:  krishon [ Thu Sep 25, 2003 7:54 pm ]
Post subject: 

its the string names [] an array (i haven't learned it yet, lol, we're on strings)

Author:  rizzix [ Thu Sep 25, 2003 7:55 pm ]
Post subject: 

Yes!! ... wow you reply fast.

Author:  Tony [ Thu Sep 25, 2003 7:55 pm ]
Post subject: 

Very Happy I've just posted substring way up there /\

but I like the new .split way even more 8)

oh man, JAVA pwnz Laughing

Author:  krishon [ Thu Sep 25, 2003 7:56 pm ]
Post subject: 

lol, yah...bout teh fast replyin...having msn on and having it notify can help Laughing

Author:  krishon [ Thu Sep 25, 2003 7:59 pm ]
Post subject: 

lol, tony if i used ur code, my teacher would know someone's helping, cuz i dunno try and catch Very Happy

Author:  krishon [ Thu Sep 25, 2003 8:02 pm ]
Post subject: 

hmm....while we're on the topic of java.....can someone write a tutorial about graphics...i dun't expect this if you are too busy, but it'll be great if u can write one Very Happy

Author:  Tony [ Thu Sep 25, 2003 8:03 pm ]
Post subject: 

how do you get terminal input then? cuz my IDE doesnt let me use input stream without try/catch block Confused

Author:  krishon [ Thu Sep 25, 2003 8:05 pm ]
Post subject: 

well our teacher taught us the GUI input....nice and ez, plus unlike turing I CAN GET THE VALUES FROM THE GUI BOX Very Happy, wow that's gotta be under the cons of turing Laughing

Author:  rizzix [ Thu Sep 25, 2003 11:33 pm ]
Post subject: 

so krishon, what do mean graphics... as in displaying images.. manipulating images... or GUI!! ?? Confused

Author:  Tony [ Thu Sep 25, 2003 11:58 pm ]
Post subject: 

I think he means .swing

actually I wouldn't mind a good explonation of that ether Laughing

Author:  krishon [ Fri Sep 26, 2003 5:07 pm ]
Post subject: 

most displaying and manipulating...my friend and i are think bout doin a fps (first person shooter) for a project....so i just wanna learn about displaying and manipulating...but wut we really need to know is how to make an environment...but yah...a tut on displayin and manipulating would be great!

Author:  Tony [ Fri Sep 26, 2003 5:12 pm ]
Post subject: 

FPS? Laughing you'd need to write up a 3D engine for that. Why not start with something a bit less graphics demanding?

Author:  krishon [ Fri Sep 26, 2003 5:13 pm ]
Post subject: 

i know...that's y i need to start nice and ez...and way b4 we start the fp's

Author:  krishon [ Sat Sep 27, 2003 9:12 pm ]
Post subject: 

tony, may u repost teh first solution u had b4 u editted it? thx

Author:  Tony [ Sun Sep 28, 2003 1:16 am ]
Post subject: 

it was just some preudo code. I didnt remember the exact syntax at that moment and I edited it after I looked it up.

it was basically just the

text.substring(0,text.indexOf(" "));

line

Author:  krishon [ Sun Sep 28, 2003 9:29 am ]
Post subject: 

kk, thx

Author:  krishon [ Sun Sep 28, 2003 12:04 pm ]
Post subject: 

well heres wut i have so far...it still doesn't compile correctly.... Confused


code:
import javax.swing.JOptionPane;

class Nameshon
{
  private String firstName, lastName, name, initials;
  private char lastLetter;
 
  public void getName ()
  {
    name = JOptionPane.showInputDialog ("Put your full name with a space between your first and last name.");
   
    firstName = name.substring(0,text.indexOf(" "));
    lastName = name.substring(text.indexOf(" ")+1);
  }
 
  public void getLetter ()
  {
     lastLetter = nameCharAt(name.length()/2);
  }
 
  public void getInitials ()
  {
    initials = firstNameCharAt(1) + lastNameCharAt (1);
   
  }
 
  public static void main (String args [])
  {
    System.out.println ("Your name is " + name + ".");
    System.out.println ("Your first name is " + firstName + ".");
    System.out.println ("Your last name is " + lastName + ".");
    System.out.println ("The middle letter in your last name " + lastName + " is " + lastLetter + ".");
    System.out.println ("Your initials are " + initials + ".");
  }
}

Author:  rizzix [ Sun Sep 28, 2003 1:09 pm ]
Post subject: 

woah! there's something terribly wrong here!

1: the main method is the only method that is called when ur program is executed. so to get ur program to work you have to call the other methods inside the main method, else no method is ever called and the variables fristName, lastName never do get set.

2: the structure and design is completely wrong. don't worry everyone get it wrong the first time with OOP languages.


by policy i'm not allowed to do the work for you. but you'll never get it if you don't see the difference between a correctly designed code and one that is relatively incorrect.

here's how i'd do it:
code:

import javax.swing.JOptionPane;
 
class Nameshon
{
    private String name;

    Nameshon(String pname) {
        name = pname;
    }
  
    public String getFullName()  {
        return name;
    }
  
    public char getLetter ()
    {
        String lname = getLastName();
        return lname.charAt((int)lname.length()/2);
    }
  
    public String getInitials ()
    {
         return ((String) ("" + name.charAt(1) + name.charAt(name.length() - 1)).toUpperCase();
    }

    public String getFirstName() {
        return  name.substring(0, name.indexOf(" "));
    }

    public String getLastName() {
        return name.substring(name.indexOf(" ") + 1);
    }
  
  public static void main (String args [])
  {
    String result = JOptionPane.showInputDialog ("Put your full name with a space between your first and last name.");
    Nameshon name = new Nameshon(result);
    System.out.println ("Your name is " + name.getFullName() + ".");
    System.out.println ("Your first name is " + name.getFirstName() + ".");
    System.out.println ("Your last name is " + name.getLastName() + ".");
    System.out.println ("The middle letter in your last name is " + name.getLetter() + ".");
    System.out.println ("Your initials are " + name.getInitials() + ".");
  }
}


As you can see. you were treating the static method main as an instance method. You can't do that. Static methods cannot access (call) instance methods and variables. But instance methods can call and access static methods and variables.

Since a static method cannot access an instance method you'll have to treat it as though it was not part of that object. But exists within the reach of that object. In other words, the object can call it etc.. but not the other way round. This is why you need to create an object in the static method. and call it's methods.. etc..

Just compare the classes. You'll see a couple of other errors here and there.

NOTE: i haven't tested the class yet.

Author:  krishon [ Sun Sep 28, 2003 8:31 pm ]
Post subject: 

aha....yea.. Embarassed ...well u see how bad i am, lol...k, thx for the help

Author:  krishon [ Mon Sep 29, 2003 4:51 pm ]
Post subject: 

one more thing....you know in the code above where you put in result for the parameter, how come it won't work with any other things....does it only work when it says result?

Author:  rizzix [ Mon Sep 29, 2003 5:20 pm ]
Post subject: 

no. you can pass any String object as an argument irrespective of the variable name.

Author:  krishon [ Tue Sep 30, 2003 5:50 pm ]
Post subject: 

k, cuzi put like theName and it didn't work...so yah

Author:  tlivingston [ Tue Sep 30, 2003 7:59 pm ]
Post subject: 

its cause in the notes she has it as CharAt when it should be charAt ....
(little c in char)...thats the prob

Author:  krishon [ Wed Oct 01, 2003 7:40 pm ]
Post subject: 

yah...stupid notes, lol


: