• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how can i separate string?

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A college course can be described using three components: <Division title><Code><Course
description>, where  represents an empty space. Write a Java program that
obtains a course string, and then splits and prints the THREE components of the course.

for example ~
ABC 12345 Spanish Studies
the users can type what classes they want.

how can i seperate them to :

Enter course string: ABC 12345 Spanish Studies
Division: ABC
Course Code: 12345
Course Description: Spanish Studies

thank you so much.
 
jack parker
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i can just do the front part :

String concat = str.substring(0);
System.out.println("Division: " + concat);

but the part behind will show something like these:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2
at java.lang.String.substring(String.java:1958)
at java.lang.String.substring(String.java:1925)
at XXXXX.main(XXXXX.java:25)
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check the String class for any methods available to split a String.

Please read Naming Policy
 
Ranch Hand
Posts: 287
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming all the validation is done and user enter the course in the format given in the example


 
jack parker
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harsha Smith wrote:Assuming all the validation is done and user enter the course in the format given in the example




first of all, thank you so much
but how if "ABC 12345 Spanish Studies" type by the users (use printer)

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kyky ky wrote:but how if "ABC 12345 Spanish Studies" type by the users (use printer)

I believe the Scanner class is now the usual way to read user input.
 
jack parker
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:

kyky ky wrote:but how if "ABC 12345 Spanish Studies" type by the users (use printer)

I believe the Scanner class is now the usual way to read user input.



if i use scanner, is that the correct answer differ from the above one? thanks alot
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jack parker wrote:if i use scanner, is that the correct answer differ from the above one? thanks alot


Yes. In the code above the string to be split is included in the code (line 1)*. If you want to use a different string, you will need to change line 1 and recompile.
If you use a Scanner, the user types the input whilst the program is running. You can test different strings without recompiling the program.


*Note. That is currently a blank string. You need to change it to
 
Marshal
Posts: 79177
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can’t you tell String#split() to stop after a certain number of matches? There should be details in the documentation.
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the overloaded split method - String:split(String,int)
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per Campbell's suggestion

 
jack parker
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am sorry if i just suppose to use something which is very Beginner stuffs
such as substring or indexOf this kind of things
how i can i do this question thanks so much
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How far you are comfortable with the substring() and indexOf() methods?

A small example:



Can you try with the two methods to achieve your goal?
 
jack parker
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:How far you are comfortable with the substring() and indexOf() methods?

A small example:



Can you try with the two methods to achieve your goal?


thanks so much first

but i cant execute it successfully

i use the scanner method to grab user input instead of store the course, is that matter?

below is the error find by jgrasp

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1958)
at CourseSplitter.main(CourseSplitter.java:18)
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope this helps!
 
jack parker
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harsha Smith wrote:Hope this helps!




thank you so much

but if i use the scanner method to grab user input instead of store the course there are errors
if store the course first, not use the scanner method to grab user input it do works

i cant figure out why??
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
put this inside main method and see if it works as desired.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jack parker wrote:but if i use the scanner method to grab user input instead of store the course there are errors


Please TellTheDetails
The more you get working code, you tend to put aside the code you have tried and fail to address the Exceptions / Errors you experience.

You could have showed the Scanner code you tried and the errors you received. Please try to resolve the errors one by one and learn from it .
 
jack parker
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harsha Smith wrote:put this inside main method and see if it works as desired.


thank you so much!! it do works but i cant use it for my hw..
because the things in that are not suit my level...
however , thank you so much ! i saved it and I will learn it afterwards!
 
jack parker
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:

jack parker wrote:but if i use the scanner method to grab user input instead of store the course there are errors


Please TellTheDetails
The more you get working code, you tend to put aside the code you have tried and fail to address the Exceptions / Errors you experience.

You could have showed the Scanner code you tried and the errors you received. Please try to resolve the errors one by one and learn from it .



Scanner sc = new Scanner(System.in);
System.out.print("course: ");
String str = sc.nextLine();

String d = str.substring(0, str.indexOf(' '));
System.out.println(d);

String v = str.substring(str.indexOf(' '), str.indexOf(' '));
System.out.println(v);
error in the bottom of jgrasp:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -13
at java.lang.String.substring(String.java:1958)
at bb.main(bb.java:23)

thanks so much~
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the API for substring() method in String class - substring(int,int)

See when a IndexOutOfBoundsException is thrown by the method.

My guess is that you have entered a String that does not contain a blank character (' ') and hence the endIndex is -1.

Print the end index and check like below.



Well you have other errors (or invalid logic) following this one... First correct this error.
 
jack parker
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:Check the API for substring() method in String class - substring(int,int)

See when a IndexOutOfBoundsException is thrown by the method.

My guess is that you have entered a String that does not contain a blank character (' ') and hence the endIndex is -1.

Print the end index and check like below.



Well you have other errors (or invalid logic) following this one... First correct this error.



Thanks first but wt are the things below use for

int endIndex = str.indexOf(' ');
System.out.println("End Index -> " + endIndex) ;
String d = str.substring(0, endIndex);
System.out.println(d);

and i do first one and second one but there is a space in front of the output i dont get it~
for example, i type this
String a = str.substring(str.indexOf(' ',0), str.indexOf(' ',15));
System.out.println("Course : " + a);
it will give me something like that~ a: 12345
↑here are TWO spaces

and how can i grab "Spanish Studies" from "ABC 12345 Spanish Studies" by using the above methods
because sometimes the input will be CD 33333 Spanish Communication in everyday life
how can i grab"Spanish Communication in everyday life" by using the above methods from the above one? (the words could be so long or short)

THANKS
Sorry for so many questions
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you see java.lang.String API there will be overloaded methods for indexOf(). Choose the one you can use that will be helpful for you in different situations. And the substring() method also uses begin and end index arguments.

Below code takes the first and second occurrences of the space character, then forms substrings from the main input String. Read about the methods and understand why +1 is added to indices.

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Harsha Smith has already given you a similar solution. And please UseCodeTags while posting code henceforth.
 
Ranch Hand
Posts: 47
Netbeans IDE Eclipse IDE Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jack parker wrote:A college course can be described using three components: <Division title><Code><Course
description>, where  represents an empty space. Write a Java program that
obtains a course string, and then splits and prints the THREE components of the course.

for example ~
ABC 12345 Spanish Studies
the users can type what classes they want.

how can i seperate them to :

Enter course string: ABC 12345 Spanish Studies
Division: ABC
Course Code: 12345
Course Description: Spanish Studies

thank you so much.



This is also possible :



regards,
Ben
 
jack parker
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you all of you!!
i can solve the q now!!
John Jai . Harsha Smith. Joanne Neal . Campbell Ritchie . Ben Ooms!!
thanks all~~
 
reply
    Bookmark Topic Watch Topic
  • New Topic