Results 1 to 10 of 10

Thread: Transmit Int

  1. #1
    Jey
    Jey is offline Member
    Join Date
    Aug 2011
    Location
    Czech Republic
    Posts
    12
    Rep Power
    0

    Default Transmit Int

    Hi. My problem is smiling. Ive got 2 file *.class

    1st file

    Java Code:
    import java.io.IOException;
    public class Vyber {
      
        public static int x;
        
        public static void vyber() throws IOException {
            volba.vyberSi(x);
            System.out.println(x+" vyber");
            switch(x){
                case 1: radkove_orientovany_IO.zapisDoSouboru();
                case 2: bajtProud.bP();
                case 3: znakProud.zP();
                case 4: scanner.sc();
                case 5: desCarka.dC();
            }
            
        }
    }
    2nd file

    Java Code:
    import java.util.*;
    
    public class volba {    
        public static int vyberSi(int x){
           Scanner sc = new Scanner(System.in);
           x = sc.nextInt();
           System.out.println(x+ " volba");
           return x;
        }
    }
    I dont know how transmit int x. In class volba is x allright, but in class Vyber int x isnt the same number.

    Thanks for any response.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    The caller of the vyberSi method will receive from that method the value of x. It needs to store what is returned by that method into a local variable. Your code does not store the returned value.

  3. #3
    Jey
    Jey is offline Member
    Join Date
    Aug 2011
    Location
    Czech Republic
    Posts
    12
    Rep Power
    0

    Default

    Can I ask for writing code, please? I dont know where I have mistake.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    Where is the value of x that you want to be "transmitted"?
    From your code. Here is a sample of how you save the value returned by the nextInt() method in x:
    x = sc.nextInt();

    do the same for your method.

  5. #5
    Jey
    Jey is offline Member
    Join Date
    Aug 2011
    Location
    Czech Republic
    Posts
    12
    Rep Power
    0

    Default

    Ooops ... Thanks for reaction. Now I know wheres problem but I dont know how write that :-X.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    but I dont know how write that
    Please explain what you are trying to do?
    Where is the value of x that you want to be "transmitted"?

  7. #7
    Jey
    Jey is offline Member
    Join Date
    Aug 2011
    Location
    Czech Republic
    Posts
    12
    Rep Power
    0

    Default

    I want enter number (x) from class volba to class Vyber, where is Switch (Vyber). I want make a little menu. I can do it in one class, but I want to work with parameter passing.
    PS: Sorry for my (little) bad english

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    number (x) from class volba to class Vyber
    What does your vyberSi method do? Does it return the value of x that you want?

    You Call that method from the Vyber class to get the value of x.
    What does this line do? It calls the vyberSi method but does NOT save the returned value.

    volba.vyberSi(x);

  9. #9
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    I think that the point of confusion is...
    • Java does "pass by value."

    This means that volba can't change the value of the caller's variable 'x'. The 'x' in volba has nothing to do with the 'x' in Vyber. The two are not connected. Changing one does not change the other.

    So Vyber needs to do this:
    Java Code:
            x = volba.vyberSi(x);
    Once that works, some of the other things the code does (which do not help) could be cleaned out.

  10. #10
    Jey
    Jey is offline Member
    Join Date
    Aug 2011
    Location
    Czech Republic
    Posts
    12
    Rep Power
    0

Similar Threads

  1. how to transmit image file through USB communicaion
    By santhosh_el in forum NetBeans
    Replies: 0
    Last Post: 03-28-2009, 07:40 AM
  2. how to transmit image file through USB communicaion
    By santhosh_el in forum NetBeans
    Replies: 1
    Last Post: 03-28-2009, 05:28 AM
  3. How to Transmit data from one node to another
    By swimberl in forum Networking
    Replies: 2
    Last Post: 01-04-2008, 08:48 PM
  4. How would I transmit my password through java
    By silvia in forum Networking
    Replies: 1
    Last Post: 07-30-2007, 07:29 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
  •