• Post Reply Bookmark Topic Watch Topic
  • New Topic

Iteration in a string  RSS feed

 
Ray Zeigler
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Im trying to get my program to print some like the following. I need these to go to 1001
Case "1"
Application.FollowHyperlink "http:mypage/1/"
Case "2"
Application.FollowHyperlink "http:mypage/2/"
......
Case "1001"
Application.FollowHyperlink "http:mypage/1001/"

My only problem is printing the parentheses around the url and case number. Here is the code I've come up with that doesn't print with the parentheses. Can someone show me how to do this in my code?

 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are no parenthesis!

Do you mean quotes " s ?

If so simply escape the quote \" or use ""

WP
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why have you got the hypNum variable. Can't you just print the value of caseNum ?
 
Ray Zeigler
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My mistake I ment to say quotes. I tried double quotes but I got this error

Syntax error on token "caseNum", * expected

Is what i get from my IDE when I tried

System.out.println("Case"+" "+ ""+ ""caseNum"" );
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you want to print a literal quote, you need to escape it:

System.out.println("Case "+ "\"caseNum\"" );
 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to use the " character in your code, you must escape it. You escape characters by using a backward slash, so in your case the escaped quote will be like \". Try to insert this statement at your code to see what happens:
System.out.println("\"This is a comment\"");

Also, as Stuart said, there is no need for two ints, you can do the same with only one variable (though, a good reason to keep two variables is for code readability). And don't forget to add another "/" at the end of your String, because the code that you have right now produces urls without the ending slash.
 
Ray Zeigler
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gotcha ok. I got rid of that other int. Thanks for pointing that out.

So I got my previous one to work.

System.out.println("Case"+" "+ "\"caseNum\"");

This worked perfectly.
But this part did not. I tried the escape quote at the end but I still got a syntax error.

System.out.println("Application.FollowHyperlink "
+ "\"http://s1269.photobucket.com/albums/jj585/ridgewoodnj/" + caseNum\"");

 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This happens because you didn't put it inside quotes. So instead of \"" you should use"\"".
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ray Zeigler wrote:

When you get to caseNum, you're no longer in a String literal. So you need a + to add the next bit, and put it back in quotes. I've use code tags to quote your code, it makes it a bit clearer.

You might want to investigate the System.out.format() method, as that makes it easier to build up strings where you have to insert values.
 
Ray Zeigler
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahh tricky. Thanks soo very much for your help. Its very appreciated
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using a switch-case? Why not use a StringBuilder and insert the page number in the appropriate place in the text? Rather than 1000 cases for 1000 pages, you can have about 3 lines for any number of pages.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!