Page 1 of 5 123 ... LastLast
Results 1 to 20 of 81
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Pojahns newbie thread

    Status: ------

    Hello.
    First issue, I want to make a program that convert big letter to small. I have a method, but I get compile error.

    Java Code:
    class Pojahns
    
    {
    	public static void main (String[] args) throws Exception
    
    	{
    
    	java.util.Scanner	in = new java.util.Scanner (System.in);
    
    	int tal1 = in.nextInt ();
    	in.nextLine ();
    	int tal2 = in.nextInt ();
    	in.nextLine ();
    	int max = (tal1 > tal2 ? tal1 : tal2);
    
    	if (max >= 1 && max <= 10)
    	{
    		System.out.println ("Write three big letters");
    		char c1 = (char) System.in.read ();
    		System.in.read ();
    		System.in.read ();
    		char c2 = (char) System.in.read ();
    		System.in.read ();
    		System.in.read ();
    		char c3 = (char) System.in.read ();
    
    		char c4 = (char) c1 + 32;
    		char c5 = (char) c2 + 32;
    		char c6 = (char) c3 + 32;
    
    		System.out.println ("The small letters are: " + c4 + c5 + c6);
    	}
    
    	else
    	{
    		System.out.print ("You wrote " + tal1 + " och " + tal2 + "\n");
    	}
    
    	}
    }
    And the error messege

    Java Code:
    C:\New\Pojahns.java:27: possible loss of precision
    found   : int
    required: char
    		char c4 = (char) c1 + 32;
    		                    ^
    C:\New\Pojahns.java:28: possible loss of precision
    found   : int
    required: char
    		char c5 = (char) c2 + 32;
    		                    ^
    C:\New\Pojahns.java:29: possible loss of precision
    found   : int
    required: char
    		char c6 = (char) c3 + 32;
    		                    ^
    3 errors
    
    Tool completed with exit code 1
    How to fix this?
    Last edited by Pojahn_M; 03-16-2011 at 08:47 PM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What happens if you do (char)(c + 32)?

  3. #3
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    That fixed it, thanks.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your welcome. I may be wrong bit I believe the problem was that you were casting c to char then adding an int to it, which made it an int than setting a char to be an int.

  5. #5
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    sunde887 you are correct.

    New issue: I am going to create a simple program.
    You write a number, the program check if it is posetive or negative, if it is posetive, the program print that the number in negative, and then you write an number again and it chekcs again, and vise versa if the number is posetive, and you stop this endless loop if you write "0" (sans quotes).

    Example how it would work:

    Write a number:
    5
    5 is posetive.
    Write a number:
    6
    6 is posetive.
    Write a number:
    -6
    -6 is negative.
    Write a number:
    1
    1 is posetive.
    Write a number:
    0
    Program exit.
    I cant figure out how I could make this. If/else would not work, becuse they wont loop. A while or do-while loop doesnt have alternative to check if is is this or that. Same goes for a for-loop. But my book want me to use some of these. So can someone give me tipps, how I can make a loop with alternatives?
    Last edited by Pojahn_M; 03-06-2011 at 01:43 AM.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Do you know how to set up an infinite loop? The entire solution falls into using that and break and if else statements.

  7. #7
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    Alright, I took your suggestion, using a loop and else/if statement.
    But it wont loop, you write a number, the program print whether its posetive or negative, then it ends. Here is code:

    Java Code:
    class Pojahns
    
    {
    	public static void main (String[] args) throws Exception
    
    	{
    
    	java.util.Scanner	in = new java.util.Scanner (System.in);
    
    	System.out.print ("Write a number: ");
    	int tal;
    	System.out.println ();
    
    	do
    	{
    	tal = in.nextInt ();
    
    		if (tal > 0)
    		{
    			System.out.println (tal + " is posetive.");
    		}
    
    		else if (tal < 0)
    		{
    			System.out.println (tal + " is negative.");
    		}
    	}
    	while (tal == 0);
    
    	System.out.println ("You wrote \"0\", the program will exit.");
    
    	}
    }
    Also I cant figure out how break statement will be useful here.
    Most important, why wont it loop?

    EDIT: Never mind, I got it working with a for loop:

    Java Code:
    class Pojahns
    
    {
    	public static void main (String[] args) throws Exception
    
    	{
    
    	java.util.Scanner	in = new java.util.Scanner (System.in);
    
    	System.out.print ("Write a number: ");
    	int tal;
    	System.out.println ();
    
    	for ( ; ; )
    	{
    	tal = in.nextInt ();
    
    		if (tal > 0)
    		{
    			System.out.println (tal + " is posetive.");
    		}
    
    		else if (tal < 0)
    		{
    			System.out.println (tal + " is negative.");
    		}
    
    		else if (tal == 0)
    		{
    			System.out.println ("You wrote \"0\", the program will exit.");
    			break;
    		}
    
    	}
    
    
    
    
    	}
    }
    My first method, using do, could have worked if I changed while (tal == 0) to while (tal != 0). So yeah, both work in this case. :p
    Last edited by Pojahn_M; 03-06-2011 at 11:53 PM.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Im glad you worked it out, I was just about to post that your condition is wrong.

    You could also do something like
    Java Code:
    while(true){
      tal = in.nextInt();
      if(tal < 0){
        //print that its neg
      }
      else if(tal > 0){
        //print that its pos
      }
      else if(tal == 0){
        break;
      }
    }

  9. #9
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    Quote Originally Posted by sunde887 View Post
    Im glad you worked it out, I was just about to post that your condition is wrong.

    You could also do something like
    Java Code:
    [B]while(true)[/B]{
      tal = in.nextInt();
      if(tal < 0){
        //print that its neg
      }
      else if(tal > 0){
        //print that its pos
      }
      else if(tal == 0){
        break;
      }
    }
    thats an other way to make a endless loop:) Thanks for info. As you see I am new to java, and I love it, its so fun D:D

  10. #10
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    Java Code:
    class Testclass
    
    {
    	public static void main (String[] args) throws Exception
    
    	{
    
    	java.util.Scanner	in = new java.util.Scanner (System.in);
    
    	char tal1;
    
    	for ( ; ; )
    	{
    		[B]System.out.print ("Write a character: ");[/B]
    		tal1 = (char) System.in.read ();
    		int tal2 = (int) tal1;
    
    		if (tal2 == 113)
    		{
    			System.out.println ("Program exit");
    			break;
    		}
    		else if (tal2 == 81)
    		{
    			System.out.println ("Program exit");
    			break;
    		}
    		else
    		{
    			continue;
    		}
    
    	}
    
    	System.out.println ("Loop end");
    
    
    	}
    }
    This line in my code above:
    System.out.print ("Write a character: ");
    It get played 3 times(not the first time), I got now clue why. Solution?

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    how do you know it isn't being printed the first time? Show me some sample input/output of the program being executed.

  12. #12
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    Quote Originally Posted by sunde887 View Post
    how do you know it isn't being printed the first time? Show me some sample input/output of the program being executed.
    it is being printed, but not three times.

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    are you entering a value equal to (int)'s' ?

  14. #14
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    yeah, numbers. But never mind this, I have started with array(fixed) now.
    Last edited by Pojahn_M; 03-07-2011 at 09:43 PM.

  15. #15
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    I have a question about array. There is one part in this chapter that i dont understand. Help me explain what it does in these two situations:

    Example 1:

    Java Code:
    int v1 = new int[10];
    int v2 = new int[10];
    v1 = v2; //WTF does this mean?
    Example 2:

    Java Code:
    int[] v1 = { 1, 3, 5, 7, 9 };
    int[] v2 = { 2, 4, 6, 8, 10 };
    v1 = v2; //Same question here

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What do you think it does?

    Say you created a class of your own and did something similar
    Java Code:
    public class Car{
      Color col;
      public Car(Color col){
        this.col = col;
      }
      public static void main(String[] args){
        Car c1 = new Car(Color.red);
        Car c2 = new Car(Color.black);
        c1 = c2;
      }
    }
    What do you think that does?

    Also, what happens if you print the arrays after the thing you don't understand?

    Java Code:
    System.out.println("This is v1");
    for(int i = 0; i < v1.length; i++){
      System.out.print(v1[i] + ", ");
    }
    System.out.println();
    System.out.println("This is v2");
    for(int i = 0; i < v2.length; i++){
      System.out.print(v2[i] + ", ");
    }
    put this code after example 2 in your questions


    Also, if you did
    Java Code:
    int i = 3;
    int j = 4;
    i = j;
    What would be the new values of i and j?

    To help even more
    Java Code:
    int[] v1;
    int[] v2 = { 1, 3, 5, 7, 9 };
    v1 = v2;
    What would that do?
    Last edited by sunde887; 03-08-2011 at 11:20 PM.

  17. #17
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    I see, so v1 = v2 means that all the values from v2 get copied to v1 values.

    Java Code:
    int[] v1 = { 1, 3, 5, 7, 9 };
    int[] v2 = { 2, 4, 6, 8, 10 };
    v1 = v2
    
    //The new v1:
    //v1[0] is now 2
    //v1[1] is now 3
    //v1[2] is now 4
    //v1[3] is now 5
    //v1[4] is now 6
    Thx once again.

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Pretty much, however; I could be wrong(but I don't think I am) it really just makes the two things refer to the same object on the heap. If I am wrong I hope someone will correct me. Glad you figured it out, post back if you get stuck on anything else.

  19. #19
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    In this code:

    Java Code:
    class Pojahns
    
    {
    	public static void main (String[] args)// throws Exception
    
    	{
    		java.util.Scanner	in = new java.util.Scanner (System.in);
    
    		int a = 5;
    		int b = 2;
    
    		int biggest = max(a,b);
    
    		System.out.println (biggest);
    	}
    
    	public static int max (int number1, int number2)
    	{
    		return (number2 > number1) ? number2 : number1;
    	}
    }
    Can you please explain how the return works here?
    Hmm how should I explain my concern........
    It seems that the program return number1, and its being printed in System.out.println (biggest);. But how? biggest value is from max, not number1. I dont even know how to explain. Please help.

  20. #20
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If number 2 is larger it returns number 2 otherwise it returns 1.

    Try setting biggest to max(b, a); and see what happens.

Page 1 of 5 123 ... LastLast

Similar Threads

  1. Replies: 5
    Last Post: 04-27-2011, 02:13 AM
  2. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  3. Replies: 4
    Last Post: 06-07-2009, 12:59 AM
  4. Replies: 3
    Last Post: 06-07-2009, 12:14 AM
  5. Replies: 12
    Last Post: 05-26-2009, 01:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •