小编Jam*_*mes的帖子

字符串不被视为对象吗?

我不明白的是,当一个String实际上是一个对象时,为什么我在编译代码时遇到错误,而编译器则另有说法.我不知道为什么我一直收到此错误消息

  symbol:   method compareTo(Object)
  location: variable least of type Object
.\DataSet.java:17: error: cannot find symbol
   else if(maximum.compareTo(x) < 0)
Run Code Online (Sandbox Code Playgroud)

这是代码.我正在尝试使用类似的类来允许两个对象使用compareTo方法.在测试器中,我只是尝试使用基本的字符串对象进行比较.

public class DataSetTester
{
public static void main(String[] args)
{
    DataSet ds = new DataSet();
    String man = "dog";
    String woman = "cat";
    ds.add(man);
    ds.add(woman);
    System.out.println("Maximum Word: " + ds.getMaximum());


 }
}
Run Code Online (Sandbox Code Playgroud)

类:

public class DataSet implements Comparable 
{
 private Object maximum;
 private Object least;
 private int count;
 private int answer;

 public void add(Object x)
 {

   if(count == 0){
     least …
Run Code Online (Sandbox Code Playgroud)

java interface

2
推荐指数
1
解决办法
1425
查看次数

C中的冲突类型,为什么?

我正在尝试用C语言编写基本练习,使用Binary和Hex.我做了一个方法来打印2的倍数(2的幂)和一个单独的方法来打印出2的倍数的十六进制形式.

#include <stdlib.h>
#include <stdio.h>

const char one = 1;
const int bits = 31;

void    print2       ()
{
    unsigned int  u = (int)one;
    unsigned int j;

    printf("The powers of 2 are:\n");

    for(j=0;j<bits;j++)
    {
            unsigned int temp = u<<j;
            printf("%d\n",abs(temp));
            printhex(temp);
    }

    printf("\n\n");
}

void    printhex       (unsigned int   u)
{
    printf("0x%08X\n",u);
}

int main ()
{
    print2();
    return(EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)

我不明白的是为什么我从调用方法"printHex"得到错误的"冲突类型".我特别要求一个无符号整数,当我在参数中调用该方法时(我假设它是无符号整数"temp"),编译器不接受.

c binary hex type-conversion

2
推荐指数
1
解决办法
915
查看次数

为什么会出现NullPointerException?

我试图使用一个字符串并将其转换为int来比较第一个第一列和所有行中输入的字符串中的所有数字.当我输入一个数字时,我得到一个NullPointerException.问题是,当我觉得我已经正确地声明了所有对象时,我不明白为什么编译器会告诉我这个.请帮忙!

import java.util.ArrayList;

public class Decoder
{

    private int[][] zipdecoder;
    private ArrayList<Integer> zipcode;
    private String finalCode;
    private String bars;
    private int place;
public Decoder()
{
   int[][] zipdecoder = new int[][]{
       {1,0,0,0,1,1},
       {2,0,0,1,0,1},
       {3,0,0,1,1,1},
       {4,0,1,0,0,0},
       {5,0,1,0,1,1},
       {6,0,1,1,0,0},
       {7,1,0,0,0,0},
       {8,1,0,0,1,1},
       {9,1,0,1,0,0},
       {0,1,1,0,0,0}
       };
    zipcode = new ArrayList<Integer>();
}

public void insertToArray(String zip)
{
    int count = 0;

    for(int i = 1; i<zip.length()+1;i++)
    {
        String piece = zip.substring(count, i);

        int number = Integer.parseInt(piece);
        for(int j = 0;j<10;j++)
        {
            if(number == zipdecoder[j][0]){
            for(int a …
Run Code Online (Sandbox Code Playgroud)

java error-handling nullpointerexception

1
推荐指数
1
解决办法
83
查看次数

当我把它变成负数时,为什么这个值不会改变?

我正在尝试编写一个类Rational,它有一些与添加,减去等有关的方法.我想让它在构造函数中,我将值添加到私有变量并找到GCD来查找简化分数.我遇到的问题是我的if语句.我想检查object参数中的数字是否为负数,因此我使用if语句进行检查.唯一的问题是当我运行程序时,它没有给我一个负值,即我有Rational p = new Rational(-24,48)并且它只返回1/2.

public class TestRational {

    public static void main(String... args) {
        Rational p = new Rational(-24, 48);
    }

    public Rational(long a, long b){
        numerator = a;
        denominator = b;
        boolean isNegative = false;
        if (numerator*denominator < 0)
            isNegative = true;
        long gd = gcd(numerator, denominator);
        numerator /= gd;
        denominator /= gd;
        if (isNegative)
            numerator = -numerator;;
    }

    private long gcd(long p, long q){
        //checks to see if numerator greater than denominator
        if(p<q)
            return gcd(q,p);
        if(Math.abs(q) == …
Run Code Online (Sandbox Code Playgroud)

java object

0
推荐指数
1
解决办法
104
查看次数