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: How do I output information from my Method to the user as a table?

  1. #1
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Post How do I output information from my Method to the user as a table?

    For my program, I'm trying to use data from a file that will take the numbers(which stand for Celsius) and use them to calculate standard deviation/convert to Fahrenheit. I've already created the methods for the calculation but I'm not sure what is the right way of outputting the information from those methods to the user when I run the code.

    I've tried:
    sDeviation(celsius);
    double fDeviation = sDeviation(celsius);

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
    public class ProceduralReview {
    	public static void main(String[] args) {
     
    		//making the arrays
    		int[] year = new int[165];
    		double[] celsius = new double[165];
     
    		File file = new File("tempsLA.txt");
    		Scanner input = null;
     
    		try {
    			input = new Scanner(file);
    		}//end try
    			catch (FileNotFoundException e) {
     
    			//In case file isn't found
    			System.out.println("File not found.");
    			e.printStackTrace();
    			System.exit(0);
     
    		}//end catch
     
    		int index = 0;
    		while(input.hasNextLine()) {
     
    			year[index] = input.nextInt();
    			celsius[index] = input.nextDouble();
    			index++;
     
    		}//end while
     
    		//Print table
    		System.out.println("Year\tCelsius");
    		printTable(year,celsius);
    		System.out.println("");
     
    		//Print Final Table
    		System.out.println("Showing all of the years where the temp is at least 1 standard deviation of the mean:" + "\n");
     
    		System.out.println("Year \t Celsius\tFahrenheit");
     
    	}//end main
     
    	private static void printTable(int[] year, double[] celsius) {
     
    	    //for loop
    	   for(int i = 0; i < year.length; i++) {
    				System.out.println(year[i] + "\t" + celsius[i]);
    			}
     
    	  }//end printTable
     
    	//Celsius to Fahrenheit conversion
    	public static double cfahrenheit(double celsius) {
    		double fahrenheit = (celsius*1.8)+32;
    		return fahrenheit;
    	}
     
    	//Deviation formula
    	public static double sDeviation (double celsius) {
    		double mean = celsius/165;
    		double subtractSquared = Math.pow(celsius - mean, 2);
    		double deviation = Math.sqrt((1/165)*subtractSquared);
    		return deviation;
    	}
     
    }//end class

    Input File:
    1849 8.819
    1850 7.087
    1851 7.549
    1852 8.236
    1853 8.159
    1854 7.849
    1855 7.836
    1856 7.994
    1857 8.437
    1858 9.045
    1859 8.03
    1860 7.709
    1861 7.809
    1862 8.897
    1863 8.384
    1864 9.535
    1865 8.959
    1866 8.428
    1867 9.338
    1868 8.063
    1869 9.362
    1870 9.384
    1871 8.815
    1872 8.818
    1873 10.568
    1874 8.311
    1875 8.375
    1876 7.728
    1877 9.938
    1878 9.284
    1879 7.396
    1880 7.606
    1881 8.618
    1882 6.33
    1883 7.263
    1884 8.336
    1885 8.756
    1886 9.057
    1887 9.055
    1888 6.222
    1889 8.008
    1890 6.042
    1891 8.851
    1892 9.398
    1893 9.818
    1894 6.605
    1895 7.968
    1896 10.485
    1897 8.826
    1898 6.652
    1899 9.593
    1900 10.292
    1901 9.339
    1902 8.793
    1903 9.664
    1904 9.047
    1905 10.932
    1906 9.567
    1907 7.686
    1908 9.791
    1909 9.737
    1910 7.654
    1911 9.742
    1912 9.767
    1913 7.178
    1914 9.549
    1915 8.715
    1916 6.825
    1917 6.502
    1918 8.821
    1919 9.998
    1920 9.648
    1921 8.14
    1922 6.756
    1923 9.389
    1924 8.914
    1925 9.199
    1926 9.309
    1927 9.242
    1928 10.072
    1929 7.568
    1930 8.516
    1931 9.858
    1932 6.38
    1933 6.98
    1934 10.589
    1935 8.869
    1936 10.372
    1937 4.016
    1938 10.532
    1939 9.064
    1940 10.396
    1941 10.018
    1942 9.78
    1943 9.631
    1944 9.051
    1945 8.913
    1946 9.076
    1947 8.066
    1948 10.912
    1949 4.463
    1950 6.463
    1951 8.987
    1952 7.125
    1953 11.592
    1954 8.716
    1955 6.942
    1956 9.755
    1957 7.027
    1958 10.225
    1959 10.934
    1960 7.533
    1961 10.846
    1962 9.274
    1963 8.111
    1964 8.293
    1965 9.838
    1966 7.919
    1967 9.493
    1968 8.795
    1969 9.637
    1970 9.581
    1971 8.99
    1972 7.915
    1973 7.562
    1974 8.081
    1975 9.223
    1976 10.462
    1977 8.989
    1978 9.901
    1979 7.271
    1980 10.684
    1981 10.788
    1982 7.891
    1983 10.333
    1984 10.851
    1985 8.371
    1986 12.113
    1987 7.957
    1988 8.97
    1989 8.158
    1990 9.464
    1991 9.281
    1992 9.292
    1993 8.568
    1994 10.553
    1995 9.119
    1996 10.328
    1997 9.437
    1998 9.699
    1999 10.527
    2000 10.772
    2001 8.398
    2002 9.022
    2003 13.492
    2004 9.588
    2005 9.87
    2006 9.846
    2007 8.391
    2008 8.196
    2009 11.882
    2010 10.09
    2011 10.432
    2012 11.581
    2013 8.813

    Output:
    Year	Celsius
    1849	8.819
    1850	7.087
    1851	7.549
    1852	8.236
    1853	8.159
    1854	7.849
    1855	7.836
    1856	7.994
    1857	8.437
    1858	9.045
    1859	8.03
    1860	7.709
    1861	7.809
    1862	8.897
    1863	8.384
    1864	9.535
    1865	8.959
    1866	8.428
    1867	9.338
    1868	8.063
    1869	9.362
    1870	9.384
    1871	8.815
    1872	8.818
    1873	10.568
    1874	8.311
    1875	8.375
    1876	7.728
    1877	9.938
    1878	9.284
    1879	7.396
    1880	7.606
    1881	8.618
    1882	6.33
    1883	7.263
    1884	8.336
    1885	8.756
    1886	9.057
    1887	9.055
    1888	6.222
    1889	8.008
    1890	6.042
    1891	8.851
    1892	9.398
    1893	9.818
    1894	6.605
    1895	7.968
    1896	10.485
    1897	8.826
    1898	6.652
    1899	9.593
    1900	10.292
    1901	9.339
    1902	8.793
    1903	9.664
    1904	9.047
    1905	10.932
    1906	9.567
    1907	7.686
    1908	9.791
    1909	9.737
    1910	7.654
    1911	9.742
    1912	9.767
    1913	7.178
    1914	9.549
    1915	8.715
    1916	6.825
    1917	6.502
    1918	8.821
    1919	9.998
    1920	9.648
    1921	8.14
    1922	6.756
    1923	9.389
    1924	8.914
    1925	9.199
    1926	9.309
    1927	9.242
    1928	10.072
    1929	7.568
    1930	8.516
    1931	9.858
    1932	6.38
    1933	6.98
    1934	10.589
    1935	8.869
    1936	10.372
    1937	4.016
    1938	10.532
    1939	9.064
    1940	10.396
    1941	10.018
    1942	9.78
    1943	9.631
    1944	9.051
    1945	8.913
    1946	9.076
    1947	8.066
    1948	10.912
    1949	4.463
    1950	6.463
    1951	8.987
    1952	7.125
    1953	11.592
    1954	8.716
    1955	6.942
    1956	9.755
    1957	7.027
    1958	10.225
    1959	10.934
    1960	7.533
    1961	10.846
    1962	9.274
    1963	8.111
    1964	8.293
    1965	9.838
    1966	7.919
    1967	9.493
    1968	8.795
    1969	9.637
    1970	9.581
    1971	8.99
    1972	7.915
    1973	7.562
    1974	8.081
    1975	9.223
    1976	10.462
    1977	8.989
    1978	9.901
    1979	7.271
    1980	10.684
    1981	10.788
    1982	7.891
    1983	10.333
    1984	10.851
    1985	8.371
    1986	12.113
    1987	7.957
    1988	8.97
    1989	8.158
    1990	9.464
    1991	9.281
    1992	9.292
    1993	8.568
    1994	10.553
    1995	9.119
    1996	10.328
    1997	9.437
    1998	9.699
    1999	10.527
    2000	10.772
    2001	8.398
    2002	9.022
    2003	13.492
    2004	9.588
    2005	9.87
    2006	9.846
    2007	8.391
    2008	8.196
    2009	11.882
    2010	10.09
    2011	10.432
    2012	11.581
    2013	8.813
     
    Showing all of the years where the temp is at least 1 standard deviation of the mean:
     
    Year 	 Celsius	Fahrenheit

    Desired Output:
    (Here I'd like to have it output the Fahrenheit as well which would come from the Fahrenheit method)

    Showing all of the years where the temp is at least 1 standard deviation of the mean:
    8.904157575757573
     
    Year 	 Celsius	Fahrenheit
    1849	8.819         50.0
    1850	7.087         50.0
    1851	7.549         50.0
    1852	8.236         50.0
    1853	8.159         50.0
    1854	7.849         50.0
    1855	7.836         50.0
    1856	7.994         50.0
    1857	8.437         50.0
    1858	9.045         50.0
    1859	8.03         50.0
    1860	7.709         50.0
    1861	7.809         50.0
    1862	8.897         50.0
    1863	8.384         50.0
    1864	9.535         50.0
    1865	8.959         50.0
    1866	8.428         50.0
    1867	9.338         50.0
    1868	8.063         50.0
    1869	9.362         50.0
    1870	9.384         50.0
    1871	8.815         50.0
    1872	8.818         50.0
    1873	10.568 51.022
    1874	8.311         50.0
    1875	8.375         50.0
    1876	7.728         50.0
    1877	9.938         50.0
    1878	9.284         50.0
    1879	7.396         50.0
    1880	7.606         50.0
    1881	8.618         50.0
    1882	6.33         50.0
    1883	7.263         50.0
    1884	8.336         50.0
    1885	8.756         50.0
    1886	9.057         50.0
    1887	9.055         50.0
    1888	6.222         50.0
    1889	8.008         50.0
    1890	6.042         50.0
    1891	8.851         50.0
    1892	9.398         50.0
    1893	9.818         50.0
    1894	6.605         50.0
    1895	7.968         50.0
    1896	10.485         50.0
    1897	8.826         50.0
    1898	6.652         50.0
    1899	9.593         50.0
    1900	10.292         50.0
    1901	9.339         50.0
    1902	8.793         50.0
    1903	9.664         50.0
    1904	9.047         50.0
    1905	10.932         50.0
    1906	9.567         50.0
    1907	7.686         50.0
    1908	9.791         50.0
    1909	9.737         50.0
    1910	7.654         50.0
    1911	9.742         50.0
    1912	9.767         50.0
    1913	7.178         50.0
    1914	9.549         50.0
    1915	8.715         50.0
    1916	6.825         50.0
    1917	6.502         50.0
    1918	8.821         50.0
    1919	9.998         50.0
    1920	9.648         50.0
    1921	8.14         50.0
    1922	6.756         50.0
    1923	9.389         50.0
    1924	8.914         50.0
    1925	9.199         50.0
    1926	9.309         50.0
    1927	9.242         50.0
    1928	10.072         50.0
    1929	7.568         50.0
    1930	8.516         50.0
    1931	9.858         50.0
    1932	6.38         50.0
    1933	6.98         50.0
    1934	10.589         50.0
    1935	8.869         50.0
    1936	10.372         50.0
    1937	4.016         50.0
    1938	10.532         50.0
    1939	9.064         50.0
    1940	10.396         50.0
    1941	10.018         50.0
    1942	9.78         50.0
    1943	9.631         50.0
    1944	9.051         50.0
    1945	8.913         50.0
    1946	9.076         50.0
    1947	8.066         50.0
    1948	10.912         50.0
    1949	4.463         50.0
    1950	6.463         50.0
    1951	8.987         50.0
    1952	7.125         50.0
    1953	11.592         50.0
    1954	8.716         50.0
    1955	6.942         50.0
    1956	9.755         50.0
    1957	7.027         50.0
    1958	10.225         50.0
    1959	10.934         50.0
    1960	7.533         50.0
    1961	10.846         50.0
    1962	9.274         50.0
    1963	8.111         50.0
    1964	8.293         50.0
    1965	9.838         50.0
    1966	7.919         50.0
    1967	9.493         50.0
    1968	8.795         50.0
    1969	9.637         50.0
    1970	9.581         50.0
    1971	8.99         50.0
    1972	7.915         50.0
    1973	7.562         50.0
    1974	8.081         50.0
    1975	9.223         50.0
    1976	10.462         50.0
    1977	8.989         50.0
    1978	9.901         50.0
    1979	7.271         50.0
    1980	10.684         50.0
    1981	10.788         50.0
    1982	7.891         50.0
    1983	10.333         50.0
    1984	10.851         50.0
    1985	8.371         50.0
    1986	12.113         50.0
    1987	7.957         50.0
    1988	8.97         50.0
    1989	8.158         50.0
    1990	9.464         50.0
    1991	9.281         50.0
    1992	9.292         50.0
    1993	8.568         50.0
    1994	10.553         50.0
    1995	9.119         50.0
    1996	10.328         50.0
    1997	9.437         50.0
    1998	9.699         50.0
    1999	10.527         50.0
    2000	10.772         50.0
    2001	8.398         50.0
    2002	9.022         50.0
    2003	13.492         50.0
    2004	9.588         50.0
    2005	9.87         50.0
    2006	9.846         50.0
    2007	8.391         50.0
    2008	8.196         50.0
    2009	11.882         50.0
    2010	10.09         50.0
    2011	10.432         50.0
    2012	11.581         50.0
    2013	8.813         50.0

    Here is where the value for Fahrenheit is computed:
    //Celsius to Fahrenheit conversion
    	public static double cfahrenheit(double celsius) {
    		double fahrenheit = (celsius*1.8)+32;
    		return fahrenheit;
    	}

    Here is where the lines should be printing out on the report:
    //Print table
    		System.out.println("Year\tCelsius");
    		printTable(year,celsius);
    		System.out.println("");
     
    		//Print Final Table
    		System.out.println("Showing all of the years where the temp is at least 1 standard deviation of the mean:" + "\n");
     
    		for(int i = 1; i < year.length; i++) {
     
    		}
    		System.out.println("Year \t Celsius\tFahrenheit");
     
    	}//end main
    Last edited by HyperRei; August 22nd, 2020 at 01:51 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do I output information from my Method to the user as a table?

    Please post any code here on the forum, not though a link. Be sure to wrap the code in code tags.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: How do I output information from my Method to the user as a table?

    Fixed/edited. The code should be there I believe.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do I output information from my Method to the user as a table?

    Ok, that's good. Now can you add some comments to the output that show where the problem is?
    Also show what the desired output is.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: How do I output information from my Method to the user as a table?

    All set I do believe I did it correctly. I also want to output the math/data from my Fahrenheit method which does the conversion of Celsius to Fahrenheit, but basically, a table to something that is shown is desired.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do I output information from my Method to the user as a table?

    Can you show a small sample exactly of what you want the program to print out?
    I do not see anything different in the Desired Output section you posted other than the word Fahrenheit on one line.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: How do I output information from my Method to the user as a table?

    I edited the Desired Output to show what I am trying to output. For Fahrenheit, it won't be 50.0 the whole way down of course, but rather, it would use the data from the Fahrenheit method in my code and output for the Fahrenheit. The program takes numbers from file input, calculates the new numbers(Fahrenheit) using the Fahrenheit method, then outputs those numbers under the Fahrenheit column. If that makes sense.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do I output information from my Method to the user as a table?

    Ok. Where in the code is the value for Fahrenheit computed?
    Where in the code are the lines printed out on the report?
    Can the computed value be added to the print statement so that it is in the print out?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: How do I output information from my Method to the user as a table?

    I edited the post to show the part of the code where Fahrenheit is computed. Also, I have included the code block that is supposed to print the desired table. Lastly, the computed value should be able to be outputted in a print statement. I'm just not sure how it's done which is why I'm here.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do I output information from my Method to the user as a table?

    Can you add the value to be printed to the print statement that is in the code?
      System.out.println(year[i] + "\t" + celsius[i]  + ""+  <ADD the value to print here>);
    Is The <ADD the value to print here> what returned by the cfahrenheit method?
    If so call that method there in the print statement with the celsius value that is to be converted.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: How do I output information from my Method to the user as a table?

    I could probably add it there I believe, but I haven't been taught how to nor do I know how to add the values from the method there.
    As to whether the <ADD the value to print here> added what you saw as an output for Fahrenheit the answer is no. I manually added the numbers under Fahrenheit to show an example of what I need outputting.
    How would I add the values in:
    System.out.println(year[i] + "\t" + celsius[i]  + ""+  <ADD the value to print here>);
    Based off the cfahrenheit method from my code, what would I type to add those values within the print statement?
    Last edited by HyperRei; August 20th, 2020 at 01:44 AM.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do I output information from my Method to the user as a table?

    You know how to call a method and assign the value it returns to a variable. You do it here:
    			year[index] = input.nextInt();
    In the same way call the cfahrenheit method and assign the value returned to a variable.
    Then add that variable to the print statement like you did with year[i] and celcius[i]
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] How do I output information from my Method to the user as a table?
    By HyperRei in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 19th, 2020, 05:29 PM
  2. Simple Table output program trouble
    By bruttix212 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 28th, 2018, 08:50 PM
  3. I'm trying to output my results in a table. Can you help?
    By alexempirePSG in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2014, 10:53 PM
  4. How to output information entered in JOptionPane Dialogue Boxes?
    By StaffordEmpire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 25th, 2014, 03:00 PM
  5. jdbc integration with Gui to show user information retrieved from mysql
    By harem_najat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 22nd, 2013, 07:25 PM

Tags for this Thread