Results 1 to 20 of 81
Thread: Pojahns newbie thread
- 03-05-2011, 11:55 PM #1
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.
And the error messegeJava 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"); } } }
How to fix this?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
Last edited by Pojahn_M; 03-16-2011 at 08:47 PM.
- 03-05-2011, 11:59 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What happens if you do (char)(c + 32)?
- 03-06-2011, 12:01 AM #3
That fixed it, thanks.
- 03-06-2011, 12:04 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 03-06-2011, 01:40 AM #5
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:
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?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.Last edited by Pojahn_M; 03-06-2011 at 01:43 AM.
- 03-06-2011, 01:49 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Do you know how to set up an infinite loop? The entire solution falls into using that and break and if else statements.
- 03-06-2011, 11:46 PM #7
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:
Also I cant figure out how break statement will be useful here.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."); } }
Most important, why wont it loop?
EDIT: Never mind, I got it working with a for loop:
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. :pJava 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; } } } }Last edited by Pojahn_M; 03-06-2011 at 11:53 PM.
- 03-06-2011, 11:58 PM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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; } }
- 03-07-2011, 12:19 AM #9
- 03-07-2011, 03:48 AM #10
This line in my code above: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"); } }
System.out.print ("Write a character: ");
It get played 3 times(not the first time), I got now clue why. Solution?
- 03-07-2011, 04:14 PM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
how do you know it isn't being printed the first time? Show me some sample input/output of the program being executed.
- 03-07-2011, 06:12 PM #12
- 03-07-2011, 06:29 PM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
are you entering a value equal to (int)'s' ?
- 03-07-2011, 08:56 PM #14
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.
- 03-08-2011, 11:08 PM #15
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:
Example 2:Java Code:int v1 = new int[10]; int v2 = new int[10]; v1 = v2; //WTF does this mean?
Java Code:int[] v1 = { 1, 3, 5, 7, 9 }; int[] v2 = { 2, 4, 6, 8, 10 }; v1 = v2; //Same question here
- 03-08-2011, 11:17 PM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What do you think it does?
Say you created a class of your own and did something similar
What do you think that does?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; } }
Also, what happens if you print the arrays after the thing you don't understand?
put this code after example 2 in your questionsJava 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] + ", "); }
Also, if you did
What would be the new values of i and j?Java Code:int i = 3; int j = 4; i = j;
To help even more
What would that do?Java Code:int[] v1; int[] v2 = { 1, 3, 5, 7, 9 }; v1 = v2;Last edited by sunde887; 03-08-2011 at 11:20 PM.
- 03-08-2011, 11:26 PM #17
I see, so v1 = v2 means that all the values from v2 get copied to v1 values.
Thx once again.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
- 03-08-2011, 11:31 PM #18
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 03-16-2011, 08:54 PM #19
In this code:
Can you please explain how the return works here?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; } }
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.
- 03-16-2011, 09:04 PM #20
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
Newbie in Java -- Exception in thread "main" java.lang.NoClassDefFoundError:
By sagarkha in forum New To JavaReplies: 5Last Post: 04-27-2011, 02:13 AM -
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
[newbie] Exception in thread "AWT-EventQueue-0" java.lang.Error
By jon80 in forum New To JavaReplies: 4Last Post: 06-07-2009, 12:59 AM -
[newbie] Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException:
By jon80 in forum New To JavaReplies: 3Last Post: 06-07-2009, 12:14 AM -
[newbie] Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException
By jon80 in forum New To JavaReplies: 12Last Post: 05-26-2009, 01:48 PM


LinkBack URL
About LinkBacks

D:D

Bookmarks