Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: problem with help menu.

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default problem with help menu.

    Hi all im new in java (1 month). I am reading a book and wanted to make my learning easy by making a help menu, but there is a problem i cant solve on my own.
    p.s. sorry about the language its Latvian, so i could understand it more easy.
    class Help {
    	void helpon (int what) {
     
    		switch(what) {
    			case '1' :
    				System.out.println("\"if\" veido: ");
    				System.out.println("if(nosacijums)kas notiek; ");
    				System.out.println("\telse kas notiek; \n");
    				break;
    			case '2' :
    				System.out.println("\"switch\" veido : ");
    				System.out.println("switch (nemainigais) { ");
    				System.out.println("\tcase 'attiecigais nosaukums' : un ko dara; ");
    				System.out.println("break; lai visu partrauktu\n");
    				break;
    			case '3' :
    				System.out.println("\"for\" veido: ");
    				System.out.println("for(kur sakas; kur beidzas; formula");
    				System.out.println("bet ir ari protams daudz dazadu veidu, ka manipulet ar \"for\" \n");
    				break;
    			case '4' :
    				System.out.println("\"while\" veido : ");
    				System.out.println("while(nosacijums) ko dara; ");
    				System.out.println("un dara tik ilgi, lidz paradas \"false\" pie izpildes \n");
    				break;
    			case '5' :
    				System.out.println("\"do-while\" veido : ");
    				System.out.println("do { nosacijums ko dara; ");
    				System.out.println("\twhile cik ilgi dara\n ");
    				break;
    			case '6' :
    				System.out.println("\"break\" veido : ");
    				System.out.println("... kods (kas ir aizverts(logiski pabeigts)) break; vai break label; ");
    				System.out.println("pec break atlikuso koda dalu(kas atrodas taja pasa bloka) vairs neizpilda\n");
    			case '7' :
    				System.out.println("\"continue\" veido : ");
    				System.out.println("nosacijums continue;");
    				System.out.println("pec continue izpildas tikai nodefinetas formulas vai ari neizpildas nenodefinetas\n");
    			}
    		System.out.println();
    	}
    	void showmenu() {
    		System.out.println("Sis ir palidzibas logs: ");
    		System.out.println("1. Lai iegutu info par \"if\" spied : 1 ");
    		System.out.println("2. Lai iegutu info par \"switch\" spied : 2 ");
    		System.out.println("3. Lai iegutu info par \"for\" spied : 3 ");
    		System.out.println("4. Lai iegutu info par \"while\" spied : 4 ");
    		System.out.println("5. Lai iegutu info par \"do-while\" spied : 5 ");
    		System.out.println("6. Lai iegutu info par \"break\" spied : 6 ");
    		System.out.println("7. Lai iegutu info par \"continue\" spied : 7\n");
    		System.out.println("spied 'q' lai izietu");
    	}
    	boolean isvalid(int a) {
    			if(a < '1' | a > '7'& a != 'q') return false;
    			else return true;
    	}
    }
    class HelpClassDemo {
    	public static void main(String args [])
    		throws java.io.IOException{
    		char b;
    		Help hlpobj = new Help();
     
    		for( ; ; ) {
    			do{
    				hlpobj.showmenu();
    				do{
    				b = (char) System.read.in();
    				} while(b == '\n' | b == '\r');
    			}while( !hlpobj.isvalid(b) );
    		if(b == 'q') break;
     
    		System.out.println("\n");
    		hlpobj.helpon(b);
    		}
    	}
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: problem with help menu.

    In your Demo class, it appears that you have two dos but only one while.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: problem with help menu.

    if(a < '1' | a > '7'& a != 'q') return false;

    Using ' ' means a char value.

    Typically, when saying if conditions are true, they use || or &&. I did hear that there was something that they would use
    | or & for, but I don't think that's what you need to do in this case.

    also, you have a as an int.

    That will create an error as it'll say "Cannot convert from int to char" or something like that.

  4. The Following User Says Thank You to javapenguin For This Useful Post:

    Toggo (December 11th, 2010)

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: problem with help menu.

    Is the stuff in the switch part of the program, or what you'd like the help menu to be doing? Those are some weird print outs.

  6. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: problem with help menu.

    In your switch, you probably should get rid of the ' ' around your numbers, as I think that's for using a switch with char variables, which you're not doing.

  7. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: problem with help menu.

    You do realize that do...while will do whatever is in the do brackets, even if the while condition is way false from the start, though it'll only do it once.

  8. #7
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem with help menu.

    Thanks for all those advices. But the main problem here is in this
     b = (char) System.read.in();
    part of code, cant really execute it because my jc returns an error message: input must be char or something.
    p.s. those weird print outs are because i am self-learning and this way i can learn really well. Making difficult tasks (for me) to become real.

  9. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: problem with help menu.

    Quote Originally Posted by Toggo View Post
    Hi all im new in java (1 month). I am reading a book and wanted to make my learning easy by making a help menu, but there is a problem i cant solve on my own.
    p.s. sorry about the language its Latvian, so i could understand it more easy.
    class Help {
    	void helpon (int what) {
     
    		switch(what) {
    			case '1' :
    				System.out.println("\"if\" veido: ");
    				System.out.println("if(nosacijums)kas notiek; ");
    				System.out.println("\telse kas notiek; \n");
    				break;
    			case '2' :
    				System.out.println("\"switch\" veido : ");
    				System.out.println("switch (nemainigais) { ");
    				System.out.println("\tcase 'attiecigais nosaukums' : un ko dara; ");
    				System.out.println("break; lai visu partrauktu\n");
    				break;
    			case '3' :
    				System.out.println("\"for\" veido: ");
    				System.out.println("for(kur sakas; kur beidzas; formula");
    				System.out.println("bet ir ari protams daudz dazadu veidu, ka manipulet ar \"for\" \n");
    				break;
    			case '4' :
    				System.out.println("\"while\" veido : ");
    				System.out.println("while(nosacijums) ko dara; ");
    				System.out.println("un dara tik ilgi, lidz paradas \"false\" pie izpildes \n");
    				break;
    			case '5' :
    				System.out.println("\"do-while\" veido : ");
    				System.out.println("do { nosacijums ko dara; ");
    				System.out.println("\twhile cik ilgi dara\n ");
    				break;
    			case '6' :
    				System.out.println("\"break\" veido : ");
    				System.out.println("... kods (kas ir aizverts(logiski pabeigts)) break; vai break label; ");
    				System.out.println("pec break atlikuso koda dalu(kas atrodas taja pasa bloka) vairs neizpilda\n");
    			case '7' :
    				System.out.println("\"continue\" veido : ");
    				System.out.println("nosacijums continue;");
    				System.out.println("pec continue izpildas tikai nodefinetas formulas vai ari neizpildas nenodefinetas\n");
    			}
    		System.out.println();
    	}
    	void showmenu() {
    		System.out.println("Sis ir palidzibas logs: ");
    		System.out.println("1. Lai iegutu info par \"if\" spied : 1 ");
    		System.out.println("2. Lai iegutu info par \"switch\" spied : 2 ");
    		System.out.println("3. Lai iegutu info par \"for\" spied : 3 ");
    		System.out.println("4. Lai iegutu info par \"while\" spied : 4 ");
    		System.out.println("5. Lai iegutu info par \"do-while\" spied : 5 ");
    		System.out.println("6. Lai iegutu info par \"break\" spied : 6 ");
    		System.out.println("7. Lai iegutu info par \"continue\" spied : 7\n");
    		System.out.println("spied 'q' lai izietu");
    	}
    	boolean isvalid(int a) {
    			if(a < '1' | a > '7'& a != 'q') return false;
    			else return true;
    	}
    }
    class HelpClassDemo {
    	public static void main(String args [])
    		throws java.io.IOException{
    		char b;
    		Help hlpobj = new Help();
     
    		for( ; ; ) {
    			do{
    				hlpobj.showmenu();
    				do{
    				b = (char) System.read.in();
    				} while(b == '\n' | b == '\r');
    			}while( !hlpobj.isvalid(b) );
    		if(b == 'q') break;
     
    		System.out.println("\n");
    		hlpobj.helpon(b);
    		}
    	}
    }
    Usually what's read in is a String.

    You could always make what's read in a String and have

    b = Stringthat'sreadin.charAt(0);

  10. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: problem with help menu.

    Also, I don't think the class System has a field called read.

    It has out, in, and err.

    Out is for output (System.out.println())
    In is for input(what you're reading in)
    Err is for error messages

    There's no read.

    Nor is there a method called read.

    When you read something in, at least for primitive and String types,

    you use a Scanner and have it read from a file.

    Scanner inFile = new Scanner("inFile.txt");

    char b;

    b= inFile.nextLine().charAt(0);

    To read in a bunch of stuff till you run out

    while (inFile.hasNext())
    {
    read in stuff and do stuff with it
    }

  11. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: problem with help menu.

    Also, I think that you need a constructor for your Help class.

    Simply add
    public Help()
    {
     
    }

  12. #11
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: problem with help menu.

    if(b == 'q') break;

    1.) break and continue statements are discouraged outside of switch structures.

    2.) This comes after you do...while loops so what's it going to break anyway?

    3.) Couldn't you add
    while(.... && p !='q') so that it'll only go through once if p == 'q'

    ?

  13. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: problem with help menu.

    Quote Originally Posted by javapenguin View Post
    1.) break and continue statements are discouraged outside of switch structures.
    Discouraged by who, and why?

    This comes after you do...while loops so what's it going to break anyway?
    The infinite loop

Similar Threads

  1. Menu Event Listeners
    By Gondee in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 16th, 2010, 03:08 PM
  2. Menu of Menus Help
    By Strivelli in forum Java Theory & Questions
    Replies: 10
    Last Post: June 5th, 2010, 02:40 PM
  3. popup menu
    By antonio69 in forum AWT / Java Swing
    Replies: 1
    Last Post: May 20th, 2010, 04:24 AM
  4. Help with Menu/BinSearch
    By jhar131 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 19th, 2010, 07:38 PM
  5. Problem while implementing a basic user interface menu
    By Rastabot in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 3rd, 2009, 04:38 PM

Tags for this Thread